SSH Password-less authentication is very useful when you are automating tasks between your all remote servers. If you want to run any script or use SCP multiple times then this option will be very useful for you. Once this is implemented you will never ask for a password to log into that particular server. Of course, It is a secure way because you are giving authentication between your trusted servers only.
SSH Password-less authentication is also used in Ansible architecture to communicate managed nodes from Ansible Engine. I will discuss this in detail in Ansible articles.
There are 4 simple steps to implement this authentication. We are authenticating between SERVER1 to SERVER2.
SERVER1 — 192.168.39.1
SERVER2 — 192.168.39.2
Before starting let’s check connectivity between SERVER1 to SERVER2.
In the above picture, we can see that SERVER2 asks password to login from SERVER1.
Step 1: Generate SSH-Key in your base system. Which is SERVER1 in our case.
# ssh-keygen -t rsa
Step 2: Now create a .ssh directory on remote server (SERVER2) from SERVER1. Provide a password for SERVER2.
# ssh root@192.168.39.2 mkdir -p .ssh
Step 3: Copy the generated public key to the remote server(SERVER2) .
# cat .ssh/id_rsa.pub | ssh root@192.168.39.2 'cat >> .ssh/authorized_keys'
Step 4: Change permissions of remote server’s (SERVER2) .ssh/authorized_keys file.
# ssh root@192.168.39.2 "chmod 700 .ssh; chmod 640 .ssh/authorized_keys"
If you observed, this time SERVER2 did not ask for password because password-less authentication is already established in the last step. This time we just changed the key file permission.
Step 5: Login SERVER2 without giving the password.
# ssh root@192.168.39.2
That’s it. We successfully completed SSH password-less authentication. Now whenever you login to SERVER2 from SERVER1 it won’t ask password.