SSH Password-less authentication between servers (CentOS 6.x)

Sometime it is required to have password-less authentication between servers for various reasons. Automatic backups, remote script exestuation, server maintenance tasks etc. Here is how it can be done. Make sure OpenSSH is installed on both servers.

We will need to generate new keypair and copy public key to second server which we would want to login without password. Assume you are trying to login from Server1 to Server2

  1. Login to Server1 (root in this case, you can use other userid with appropriate permissions)
  2. At the command prompt type #ssh-keygen –t rsa
  3. Press enter twice when it asks to enter the passphrase.
  4. New keys id_rsa & will be generated in /root/.ssh/ This is a default location, you can changed during key generation.
  5. Copy the contents of /root/.ssh/
  6. Login to Server2
  7. Make backup copy of authorized.keys file.
  8. #cp /root/.ssh/authorized.keys /root/.ssh/authorized.keys.bkp
  9. Paste the contents in /root/.ssh/authorized.keys file.
  10. Save and close the file.
  11. Go back to Server1
  12. If everything go smooth, you should be able to login to Server2 without giving any password. #ssh Server1
  13. See below for the command sequence.

[root@server1 /]# ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/root/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /root/.ssh/id_rsa.
Your public key has been saved in /root/.ssh/
The key fingerprint is:
81:1d:30:2e:d8:5c:eb:d8:21:44:8d:3b:75:6e:7e:22 root@server1
The key’s randomart image is:
+–[ RSA 2048]—-+
|  ..  o..        |
| ..+ o +       |
|. o.= + .      |
|. oo o . o    |
| =..  . S       |
|. o. .   o      |
|    .   o        |
|       E o .     |
|        . o      |

Leave a Reply

Your email address will not be published. Required fields are marked *