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
- Login to Server1 (root in this case, you can use other userid with appropriate permissions)
- At the command prompt type #ssh-keygen –t rsa
- Press enter twice when it asks to enter the passphrase.
- New keys id_rsa & id_rsa.pub will be generated in /root/.ssh/ This is a default location, you can changed during key generation.
- Copy the contents of /root/.ssh/id_rsa.pub.
- Login to Server2
- Make backup copy of authorized.keys file.
- #cp /root/.ssh/authorized.keys /root/.ssh/authorized.keys.bkp
- Paste the contents in /root/.ssh/authorized.keys file.
- Save and close the file.
- Go back to Server1
- If everything go smooth, you should be able to login to Server2 without giving any password. #ssh Server1
- 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/id_rsa.pub.
The key fingerprint is:
The key’s randomart image is:
+–[ RSA 2048]—-+
| .. o.. |
| ..+ o + |
|. o.= + . |
|. oo o . o |
| =.. . S |
|. o. . o |
| . o |
| E o . |
| . o |