Creating passwordless SSH between Linux servers

By | 14th June 2021

In this post, we will see how to establish a passwordless SSH connection between two Linux machines.

This can be done by using the Public and Private keys. Once the keys are set up, the authentication will be done using these keys instead of the password.

Let us consider there are two Linux machines/servers ServerA and ServerB. We will establish the passwordless connection from ServerA to ServerB

Here are the steps you need to follow to setup the passwordless SSH

Step 1 – Creating the key

Log in to the ServerA and get into the .ssh directory. Now run the below command and hit enters multiple times

ssh-keygen -t rsa

This will create two files meaning two keys. id_rsa and id_rsa.pub. The one with “.pub” extension is the public key

Step 2 – Setup public key in target server

Log in to the ServerB and check for the presence of “authorized_keys” file under the ~/.ssh folder. If it is not there, create it. After that copy the contents of id_rsa.pub file from the ServerA and put it in the authorized_keys file of ServerB

user@ServerA $ cd ~/.ssh
user@ServerA $ cat id_rsa.pub

.....
user@ServerB $ cd ~/.ssh
user@ServerB $ vim authorized_keys

.....

After you copied the contents of the public key from ServerA to the authorized_keys file in ServerB, you will be able to now connect ServerB from ServerA without any password.

user@ServerA $ ssh user@serverB
#The above command will log the user into the ServerB without any password