How to Configure Password less SSH using ssh-keygen in Linux

SSH (Secure SHELL) is an open-source and most trusted network protocol that is used to login to remote servers for the execution of commands and programs. It is also used to transfer files from one computer to another computer over the network using a secure copy (SCP) Protocol.

In this post, we will show you how to create a password-less authentication between servers. In this example, we are going to set up SSH password-less automatic login from server 192.168.1.57 as user geek to 192.168.1.59 with user admin. First login to 192.168.1.57 as user geek and create a public keys using ssh-keygen command.

$ ssh-keygen
ssh-keygen for passwordless ssh in Linux

Here are the key pairs, which were created with the ssh-keygen command.

$ ll .ssh
key pairs in .ssh directory

Now let us copy the public key to the 192.168.1.59 for the admin user. Run the following command to copy the public key to the destination server. First create .ssh directory on 192.168.1.59 server or if you run ssh-keygen command as admin user it will create the directory and authorized_keys file. Here i am going to create the directory.

$ ssh [email protected] mkdir -p .ssh
[email protected]'s password:

Now copy the public key using the following command.

$ cat id_rsa.pub | ssh 192.168.1.59 'cat >> ~admin/.ssh/authorized_keys && echo "copied"'
[email protected]'s password:
copied

Now set the correct permission for .ssh directory and .ssh/authorized_keys using the following command.

$ ssh [email protected] "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
[email protected]'s password:

Now test the login, You would be able to login from 192.168.1.57 as geek to 192.168.1.59 as admin without any password.

$ ssh [email protected]

That’s all about it. I hope you like it.