10 Example of SSH command in Linux and UNIX

Hello guys, if you have been working in Linux machine then there is great chance that you have come across SSH and SCP commands which are essential to switch between different hosts in a Linux network. As a Java developer, I need to work on more than 30 to 40 servers which contains development server, UAT servers, production servers as well as server from different regions like Asia, Middle East, UK, and the USA. It's difficult to work between server if you don't how to go from one host to other or copy data between them like files and directory and that's where this SSH commands comes handy. The SSH is one of the essential Linux commands which allows you to log in to remote host using secure socket protocol. By default, the SSH program listens on port 22..

You need an SSH client like Putty to login to the remote host but you can use any client. Putty is like default because wherever I have worked, Putty is always there. Though, I have also used clients like WinSCP which is used to transfer files between Windows and Linux hosts. 

In order to connect to remote host, you need a username and password as well as access to that host, which is control using different mechanism. If the host allows you to connect using public key then you can simply generate a public and private key and give the public key to the person who has access to that host and he can add that into SSH config. Once that is done you can login to the remote host using SSH. 

But SSH is much more than that and even after working in Linux for close to 20 years, I am still finding things which I don't know and people all also very creative with respect to using SSH. One of my colleague was so good at creating bash script using SSH that a normal release which took hours  can be completed using minutes. 

So, there is no limit how you can use SSH. In this article, I will share 10 essential examples of SSH command which I believe every programmer and developer should know. This will enable you to use SSH more effectively then you are using now. And, if you want to learn more you can always look these Linux online courses and tutorials.




10 ways to use SSH command in Linux and UNIX

Here is list of essential SSH command and how you can use them in Linux to login to remote host using username and password or by using your public and private key. 

1. Connecting to a remote host using a password and current user

This command will connect to the remote host by logging in as a current user

$ ssh dev1234pgcm
The authenticity of host 'dev1234pgcm (10.62.33.22)' can't be established.
RSA key fingerprint is c1:7a:a9:6b:ef:4c:c0:6d:6c:2b:1f:b4:2b:80:eb:54.
Are you sure you want to continue connecting (yes/no)? yes
Warning: Permanently added 'dev1234pgcm, (10.62.33.22)' 
(RSA) to the list of known hosts.
pw
Permission denied, please try again.

Once you enter the correct password, you will be able to login to the host, provided you have access to that host. If you don't have access then you can not log in to the host.


2. Connecting to a remote host using a specific user and password

If you don't provide username then SSH by default uses the current user who is executing the command, but if you want to use any specific user you can provide the username as shown below:

$ ssh user1@host1.com

Now it will ask password for user1 and once you enter the correct password, you can log in to the specified host i.e. host1


3. Connecting to a remote host using a private key (without password)

SSH also allows a user to connect to a remote user using RSA private key, in this case, you don't need to type the password. This is the preferred way to do ssh/SCP from scripts. You can use the ssh -i option to provide the identity file containing the user's private key. The -i stands for identity file as shown below:

$ ssh -i /home/user1/.ssh/id_rsa user1@host1.com

In order for this to work, the public key of user1 should also exist in the authorized_keys in the host1.com and public key of host1.com should exist in the known_hosts on a client machine. This is also the way you configure passwordless login using SSH.




4. Executing a command on a remote host

You can execute a command on a remote host without physically logging into it using SSH command. This is very useful if you want to execute a particular command on multiple hosts using a script e.g. deploying a package to N number of hosts or getting the current version of your application from all your production hosts. 

Here is how you can execute a command on a remote host using SSH in Linux:

$ ssh user1@remotehost.com "ls -lrt /home/user1/app"

This will print the output of  'ls -lrt /home/user1/app' from the remotehost.com and login as user1. This way you can get versions of your application from multiple hosts without physically login into them.

If you don't want to login with another user, you can also do this to execute a command as a current user in the remote host:

$ ssh host1.com uptime

This will print the uptime from host1.com which tells from how long host1 is up.


5. Connecting to a specific port

By default, SSH protocol connects to port 22 because that's where the SSH program listens for connections, but while installing SSH, you can change this port (though I don't advise that). If you have to log in to a host which is listening on SSH connection on a different port then 22 then you can use the following command to login:

$ ssh remoteuser@remotehost -p 9066


6.  ssh-keygen example 

You can remove all keys related to a host by using ssh-keygen -R command. This is useful when the public key of a host changed due to re-installing SSH, or moving to a different IP address or OS re-installation.

$ ssh-keygen -R remotehost.com

This will remove entries for host1.com from known_hosts file in the client machine.




7. Connecting to a remote host using IP address

If you don't remember hostname but have IP address then also you can connect to the remote host as shown below:

$ ssh 14.43.13.21

This will try to login the current user to 13.43.13.21 host. You need to enter the password of current user to proceed further.


8. Copying a tar file to a remote host using SSH

You can also use ssh with tar to move a directory of files between two machines as an alternative to scp:

$ tar -cvf - images/dogs | ssh user@remotehost '(cd new_images; tar -xf -)'


9. Generating a key pair (public and private key, RSA or DSA)

If you don't have a pair or public and private key then you can generate using ssh-keygen -t command as shown below:

$ ssh-keygen -t dsa
Generating public/private dsa key pair.
Enter file in which to save the key (/home/user1/.ssh/id_dsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/user1/.ssh/id_dsa.
Your public key has been saved in /home/user1/.ssh/id_dsa.pub.
The key fingerprint is:
13:58:30:56:72:e7:bd:14:86:9f:42:aa:82:3d:f8:e5 user1@host1.com

You can use any encryption algorithm like DSA or RSA. You can also provide specific file name if you want. Just remember to keep the passphrase empty if you want to do password-less login, otherwise even if you use private key SSH will ask for this passphrase.

How SSH works in Linux




10. Switching to a particular location

By default when you log in to a host, if you don't provide any path then you will end up at the home directory. But, you can provide the cd command to switch to the directory you want when you login to the remote host:

$ ssh -t xxx.xxx.xxx.xxx "cd /destination_directory ; bash --login"


11. Ending your SSH session

You can either type exit, logout or just press Ctrl + D to terminate your SSH session and come back to the original host where you sshed to remote host.


That's all about  essential examples of SSH command in Linux. It's one of the most useful Linux commands for both Software Developers, System administrator, IT support professional or anyone who have to efficiently work on a network, where you have to login into multiple host as part of your work.


Other Linux and UNIX Tutorials for Beginners and Experienced
  • How to get an IP address from the hostname and vice-versa in Linux (command)
  • 10 examples of date command in Linux (examples)
  • 10 examples of the xargs command in Linux (examples)
  • 10 examples of tar command in UNIX (examples)
  • 5 Free Courses to learn Linux for Beginners (Free courses)
  • 10 examples of Vim in UNIX (examples)
  • 5 examples of sort command in Linux (examples)
  • How to create, update and delete soft link in UNIX (command)
  • 5 examples of kill command in Linux (examples)
  • 6 Free Courses to learn Bash scripting in-depth (free courses)
  • 10 examples of chmod command in UNIX (examples)
  • 10 examples of lsof command in Linux (examples)
  • 10 examples of curl command in Linux (examples)
  • 10 Books every Linux Power user should read (books)
  • 10 examples of cut command in Linux (examples)
  • 10 Best Linux Courses for Programmers and Developers (Courses)

Thanks for reading this article so far. If you like this Linux SSH tutorial and examples, then please share it with your friends and colleagues. If you have any questions or feedback, then please drop a note.

No comments :

Post a Comment