Common SSH Commands in Linux With Examples


Introduction

Secure Shell (SSH) is a protocol for secure remote access and other secure network services over an insecure network. It is widely used by system administrators and developers to securely manage remote servers and perform other network operations. In this article, we'll explore some of the more common SSH commands used on Linux, with examples of their use. Whether you're a seasoned veteran or just starting out with SSH, this article will provide you with invaluable insight into the power and versatility of these essential commands. So, let's dig in and find out the crucial SSH commands that every Linux user should know.

ssh

The ssh command is used to securely log into a remote machine and execute commands on that machine. The basic syntax of the command is “ssh user@host”, where user is the username on the remote machine and host is the address or hostname of the remote machine.

$ ssh user@192.168.43.120
user@192.168.43.120's password:
Last login: Wed Feb  10 09:07:23 2023 from 192.168.43.120
[user@remote_machine ~]$

scp

The scp command is used to securely copy files between two machines. The basic syntax of the command is “scp source_file user@destination_host:destination_path”, where source_file is the file to copy, user is the username on the remote machine, destination_host is the address or hostname of the remote machine, and destination_path is the location on the remote machine where the file will be copied.

$ scp file.txt user@192.168.43.120:/home/user/file.txt
user@192.168.43.120's password:
file.txt    100%  16KB
100.1KB/s   00:00

ssh keygens

The ssh-keygen command is used to generate a public/private key pair for use with SSH authentication. This allows for secure authentication without the need for a password. The basic syntax of the command is “ssh-keygen”, which will generate a key pair in the default location (~/.ssh/id_rsa).

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:5Mv3q4WniFfrk4ZR+xP2vRnKjV7QQcKWnF1XMvCEjgk user@local_machine
The key's randomart image is:
+---[RSA 2048]----+
|    oo.o++       |
|   o  o=..       |
|  o o o o o      |
|  o o o o o      |
|   o o o S       |
|    o + o        |
|     .. o        |
|     o o         |
|     o           |
+----[SHA256]-----+

ssh-copy-id

The ssh-copy-id command is used to copy a public key to a remote computer for use with SSH authentication. The basic syntax of the command is “ssh-copy-id user@host”, where user is the username on the remote machine and host is the address or hostname of the remote machine.

$ ssh-copy-id user@192.168.43.120
user@192.168.43.120's password:
Now try logging into the machine, with "ssh 'user@192.168.43.120'", and check in:

ssh-keyscan

The ssh-keyscan command is used to collect the public keys of remote hosts. This information is then used to populate the “~/.ssh/known_hosts” file, which is used to verify the identity of the remote host. The basic syntax for ssh-keyscan is “ssh-keyscan hostname”, where hostname is the name or IP address of the remote host.

$ ssh-keyscan domain.com
# domain.com:22 SSH-2.0-OpenSSH_7.2p2 Ubuntu-4ubuntu2.2
domain.com ssh-rsa AAAAB3NzaC1yc2EAAAADAQABBAABAQDG7AsPp7uvIc9sGoA7V16MgA/z8DyzxQHsiJmVgcgEKbF7cxr8rvBjKPAyHg0yw1G0H8KjzQg9seYFZcLjKsgxRbKf72GtCQgwQqT3qrxv1TZhYzZbLcO7xlz74CJp7jKzjOIW7eLgsfVRuV7A/I6Q2W6Gx/vU2Qu6/ej6U5W44MvkvgfrE9ZLn0Nupq3V7CipysIjKV7wEBCjM8b7IuRv1ZVjQ2fLgSJe6Q+oxvUeH6U5j6F8Wgf97+zZHX9BBjLUnrC/pj/aAaJN/w18r+zN0++2fNjH/9Y0L/NtOkKtAZ16Rde1y7tQsOmj3T/zjB0mZplzSGbxRKjJksRcfMt7sEt

ssh-add

The ssh-add command is used to add private keys to the SSH authentication agent. This agent is used to store private keys in memory, allowing them to be used without having to enter a password each time. To add a key to the Authentication Agent, just run “ssh-add path/to/private_key”. This can be a convenient way to manage multiple private keys, as they can all be added to the Authentication Agent and used as needed.

$ ssh-add ~/.ssh/id_rsa
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)

Conclusion

These are just a few of the many common SSH commands used in Linux. Understanding and using these commands can greatly improve the security and efficiency of remote operations. Additionally, there are many other options and variations of these commands that can be used for even more advanced operations. As with any powerful tool, it is important to fully understand the capabilities and limitations of these commands in order to use them effectively and safely.

Updated on: 19-Apr-2023

30K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements