Wednesday, February 16, 2011

Auto login to SSH using public key authentication

While most of the automation jobs require to connect to another box through an automated script.
To make such scripts work, you need to have SSH public key authentication for authenticating SSH remote box without password (i.e without manual intervention).

SSH Public key authentication:
Here goes the steps for doing so...

1. Login to the SSH server (on which you want to run the automated jobs).
2. Now generate Key pair using ssh-keygen utility (this is an in-built utility for all SSH servers)
        $ ssh-keygen -t rsa
        Generating public/private rsa key pair.
        Enter file in which to save the key (/users/sathish/.ssh/id_rsa):
        Created directory '/users/sathish/.ssh'.
        Enter passphrase (empty for no passphrase):
        Enter same passphrase again:
        Your identification has been saved in /users/sathish/.ssh/id_rsa.
        Your public key has been saved in /users/sathish/.ssh/id_rsa.pub.
        The key fingerprint is:
        e3:89:d0:b2:b1:79:a6:65:1c:7a:0d:d8:23:17:42:f9 sathish@linux-term1
        $

         Don't give any passphrase when it prompts for it, simply put enter.

3. Once the public key is generated, you need to copy it to the ssh box to which you want to login without password.
        $ cat /users/sathish/.ssh/id_rsa.pub
        ssh-rsa AAAAB3NzaC1yc2EAAAABI=ixK16UTVD4FGBHd7/mHuPSV7MuELA5Jk2+8ce5tjtZzy7Kh+jD7g/QHHBm/IYGigYY4Y04p3pJSqJKqvmOg1VAyG7dsgF0= sathish@linux-term1
        $
4. Login to the SSH server to which you have to connect without password from the above server.
5. Now, go to "cd ~/.ssh" directory. (If the directory doesn't exist, create it)
6. You'll find a file called "authorized_keys2", if you don't find such file, create it.
7. Copy the above public key and append in the authorized_keys2 file.
8. Change permissions of authorized_keys2 file.
    chmod 600 authorized_keys2


 Now you should be able to connect to the remote ssh server without password.


Until next post,
Sathish Manthani

No comments:

Post a Comment