SSH Command in Linux with Examples

SSH command

SSH command is helpful in logging to another host through command-line. It also helps in transferring the files or directories or some other data’ s from local host to remote host and vice-versa.

Syntax

ssh < port number> < remote host details>

Log into the remote host

The main use of the open SSH command is to log into the host. For logging into another host (i.e..,) the remote host, we need one user and the respective host' s IP address. Some hosts there may be restrictions in logging directly through root user, so better log into some other remote host' s user.

While logging to remote host the RSA key plays a major role there. If we are logging for the first time then it will ask a confirmation that it will automatically save the key to our local host. For next time it won’ t ask it will directly get into it.

Example

[user1@linuxhelp ~]$ssh user2@192.168.7.251                                                                            
The authenticity of host ' 192.168.7.50 (192.168.7.50)'  can' t be established.       
RSA key fingerprint is 34:9d:c5:68:43:f3:65:0d:ce:84:ee:66:cd:d5:3b:13.            
Are you sure you want to continue connecting (yes/no)? Yes
Warning: Permanently added ' 192.168.7.251'  (RSA) to the list of known hosts.
user2@192.168.7.251' s password:          

Since I have logged for the first time it ask for the confirmation and by next it won’ t ask. If the destination host is modified or formatted or something done, then we is trying to log into with the same RSA key, it will return a Warning message that to delete an old key manually.

Example

[user1@linuxhelp ~]$ ssh user2@192.168.7.251 
user2@192.168.7.251' s password:

The old key can be removed from the given location and we run again it will add the new key automatically with our confirmation.

Log into the remote with port number

In some remote hosts there may be assigned port numbers for some security reasons. So it may ask for the port number to log into it. For that -p option is used to mention the destination remote host’ s port number.

Example

[user1@linuxhelp ~]$ ssh -p21498 user2@192.168.7.251 
user2@192.168.7.251' s password:

Here, if the wrong port number is mentioned or forgot to mention the port number column itself, then it will return the Connection refused message.

SCP via SSH command

When the files are to be transferred from local host to the remote host it could be done by either SCP or rsync. Let we see how do the files transferred through SCP via ssh.

Example

[user1@linuxhelp ~]$ scp /home/user1/Desktop/newfile.txt user2@192.168.7.251:/home                                            /user/desktop                                                                            
user2@192.168.7.251' s password:                                                                                              newfile.txt                                       100%     0    0.0KB/s   00:00

Here, newfile.txt is transferred from local to remote host' s desired location via SSH command. So at first it will log into the remote host with password only after the confirmation it will start the transfer.

RSYNC via SSH command

As we seen SSH in SCP, the another command in transferring is rsync. Let’ s discuss about it.

Example

[user1@linuxhelp ~]$ rsync newfile.txt 
user2@192.168.7.251:/home/user2/Desktop/ 

In case if the remote host is blocked through port number use -e option with that -p option to notify port number.

Example

[user1@linuxhelp ~]$ rsync -e ' ssh -p21498'  newfile.txt user2@192.168.7.251:/home/user2/Desktop/
user2@192.168.7.251' s password: 

Thus the file can now be moved from local host to remote host. It will also retain a connection refuse message if the port number is not match.

Debug the client

Debug is nothing but checking for the connection establishment and what are the things which destruct the connection. For that we can use -v option for debugging the SSH client.

Example

[user1@linuxhelp ~]$ ssh -v  user1@192.168.7.251 
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013
debug1: Reading configuration data /etc/ssh/ssh_config
debug1: Applying options for *
debug1: Connecting to 192.168.7.50 [192.168.7.50] port 22.
debug1: Connection established.
debug1: identity file /home/user1/.ssh/identity type -1
debug1: identity file /home/user1/.ssh/identity-cert type -1
debug1: identity file /home/user1/.ssh/id_rsa type -1
debug1: identity file /home/user1/.ssh/id_rsa-cert type -1
debug1: identity file /home/user1/.ssh/id_dsa type -1
debug1: identity file /home/user1/.ssh/id_dsa-cert type -1
debug1: identity file /home/user1/.ssh/id_ecdsa type -1
debug1: identity file /home/user1/.ssh/id_ecdsa-cert type -1
debug1: Remote protocol version 2.0, remote software version OpenSSH_5.3

It will clearly show that where the connection is blocked and the reason behind that. If everything is normal then it will ask for the password of remote user.

Command execution from local host

Through SSH command we can display the contents of the respective directory in the remote host.

Example

[user1@linuxhelp ~]$ ssh user2@192.168.7.251 " ls Desktop"  
user2@192.168.7.251' s password:
file1.txt
file2.txt     newfile.txt
file3.txt

Once the SSH is enabled from the local host to remote is enabled it can be quit either by Control + D key combinations or by ' exit' command.

Example

[user1@linuxhelp ~]$ ssh user2@192.168.7.251 
user2@192.168.7.251' s password:
[user2@linuxhelp ~]$ or exit logout
Connection to 192.168.7.251 closed.

Version of installed SSH command

In Linux, OpenSSH requires the version of the other host sometimes to log into it.

Example

[user1@linuxhelp ~]$ ssh -V 
OpenSSH_5.3p1, OpenSSL 1.0.1e-fips 11 Feb 2013

Here, it shows the version number of the openSSH in our local host.

FAQ
Q
How Do I check the version of "ssh" package has been installed on the system?
A
Use the option of "-V" with "ssh" command to check the version of "ssh" package has been installed on the system. For syntax: "ssh -V".
Q
Linuxhelp Admire
A
You can use the following syntax to access the remote system with specific "port number" in "ssh" service. For syntax: "ssh user@0.0.0.0 -p12345".
Q
How to disable the "root" login access from "ssh" service in CentOS?
A
Open a configuration file on the system as using the following command.
1. For "vim /etc/ssh/sshd_config", where you can find the line of "PermitRootLogin"
2. Then you need to set the value of "yes" (or) "no" as per wish. (Ex: "PermitRootLogin yes").
Q
How to disable the "root" login access from "ssh" service in CentOS?
A
Open a configuration file on the system as using the following command.
1. For "vim /etc/ssh/sshd_config", where you can find the line of "PermitRootLogin"
2. Then you need to set the value of "yes" (or) "no" as per wish. (Ex: "PermitRootLogin yes").
Q
If I have modified the configuration, how to restart service of "SSH" in Linux?
A
If you do any modification in configuration, need to restart the ssh service using the following command. For syntax: "service sshd restart" (or) " systemctl restart sshd".