Sunday, December 25, 2011

SSH Login Without Password

You can login to remote without password, these is very useful when you want to schedule remote copy process.
This is easy to setup, just follow the below steps...

Scenario:- You want to schedule copy process from Server A using user account ram to Server B to krish home directory.

Step 1:- Login to System B using krish account.
             krish@ServerB$pwd
             /export/home/krish
Note:- On solaris system user home directory will be under /export/home
           On linux system user home directory will be under /home

Step 2:- Generating public/private key pair using ssh-keygen
            krish@ServerB$ssh-keygen -t rsa
            Generating public/private rsa key pair.
            Enter file in which to save the key (/export/home/krish/.ssh/id_rsa):
            [Enter]
            Created directory '/export/home/krish/.ssh'.
            Enter passphrase (empty for no passphrase): [Enter]
            Enter same passphrase again: [Enter]
            Your identification has been saved in /export/home/krish/.ssh/id_rsa.
            Your public key has been saved in /export/home/krish/.ssh/id_rsa.pub.
            The key fingerprint is:
            62:65:7f:32:ff:33:a4:ae:5f:f5:14:b1:da:5d:7b:24 krish@ServerB

           krish@ServerB$ ls -la .ssh/
           total 8
           drwx------   2 krish    other        512 Dec 25 17:39 .
           drwxr-xr-x   3 krish    other        512 Dec 25 17:38 ..
           -rw-------   1 krish    other        887 Dec 25 17:39 id_rsa
           -rw-r--r--   1 krish    other        223 Dec 25 17:39 id_rsa.pub

Note:- Here -t is the type of algorithm (rsa or dsa)

Step 3:- Now we have to copy public key to Server A, ram home directory under .ssh folder
          krish@ServerB$cd .ssh
          krish@ServerB$pwd
          /export/home/krish/.ssh
          krish@ServerB$scp id_rsa.pub ram@ServerA:/export/home/ram/.ssh/

Step 4:- Login to Server A as ram user & copy content inside id_rsa.pub to authorized_keys
          ram@ServerA$cd .ssh
          ram@ServerA$pwd
          /export/home/ram/.ssh
          ram@ServerA$ls -la

          total 6
          drwxr-xr-x   2 ram      other        512 Dec 25 18:02 .
          drwxr-xr-x   3 ram      other        512 Dec 25 18:01 ..
          -rw-r--r--   1 ram      other        223 Dec 25 18:02 id_rsa.pub
          ram@ServerA$cat id_rsa.pub
          ssh-rsa                 AAAAB3NzaC1yc2EAAAABIwAAAIEA1oGmYkRu9/mijvJPSeYprcvDZfJzM21o/BmfSkoLn4rwkSNGg1KIdmXyzKiQcu3kKHNjMe9f6nL8gKOD5Wa2GWfoeac/CPEkD/Q/laS8oP4XwXCHsYO/lLiJ8w3JPg/fzDimUPLcLR50S7jFziP/dzMJTx8aUnAhmc+pQiUfLw8= krish@ServerB
          ram@ServerA$cat id_rsa.pub > authorized_keys
          ram@ServerA$cat authorized_keys

ssh-rsa AAAAB3NzaC1yc2EAAAABIwAAAIEA1oGmYkRu9/mijvJPSeYprcvDZfJzM21o/BmfSkoLn4rwkSNGg1KIdmXyzKiQcu3kKHNjMe9f6nL8gKOD5Wa2GWfoeac/CPEkD/Q/laS8oP4XwXCHsYO/lLiJ8w3JPg/fzDimUPLcLR50S7jFziP/dzMJTx8aUnAhmc+pQiUfLw8= krish@ServerB

Step 5:- Testing from Server A to Server B
ram@ServerA$scp test.txt krish@ServerB:/export/home/krish

Note:- without asking the password, it will copy the file test.txt from ServerA to ServerB