Monday, May 6, 2013

ssh keys RSA password-less authentication

Quick steps to implement password-less authentication.

Notes:
1. Below steps are for a home environment and hence passphrase is not used.
2. DSA is more secured than RSA.

-On source server - homeserver1

Login to the user account for which  password-less authentication needs to be set , in this case oracle user.


[oracle@homeserver1 .ssh]$ ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/oracle/.ssh/id_rsa):  ##Hit enter for keys to be placed at default location or mention desired path.
Enter passphrase (empty for no passphrase):  ##Recommended to use a passphrase
Enter same passphrase again:
Your identification has been saved in /home/oracle/.ssh/id_rsa.
Your public key has been saved in /home/oracle/.ssh/id_rsa.pub.
The key fingerprint is:
7b:34:24:c6:f6:76:ad:ea:79:aa:71:81:fd:e5:cd:44 oracle@homeserver1.oracle

## Public and private keys are generated in default path /home/oracle/.ssh

[oracle@homeserver1 .ssh]$ ls -ltr
-rw-r--r--  1 oracle oinstall 235 May  7 00:20 id_rsa.pub
-rw-------  1 oracle oinstall 887 May  7 00:20 id_rsa


-On destination server - homeserver2

Login to the user account for which  password-less authentication needs to be set , in this case oracle user.

Perform same steps as done on homeserver1.

Copy id_rsa.pub generated on homeserver1 as authorized_keys on homeserver2

[oracle@homeserver2 .ssh]$ ls -ltr
total 24
-rw-r--r--  1 oracle oinstall 235 May  7 00:21 id_rsa.pub
-rw-------  1 oracle oinstall 883 May  7 00:21 id_rsa
-rw-r--r--  1 oracle oinstall 235 May  7 00:22 authorized_keys

Similarly copy id_rsa.pub generated on  homeserver2 as authorized_keys on homeserver1.

[oracle@homeserver1 .ssh]$ ls -ltr
-rw-r--r--  1 oracle oinstall 235 May  7 00:21 id_rsa.pub
-rw-------  1 oracle oinstall 883 May  7 00:21 id_rsa
-rw-r--r--  1 oracle oinstall 235 May  7 00:22 authorized_keys

Test the connection both ways..

[oracle@homeserver2 .ssh]$ ssh oracle@homeserver1
Last login: Tue May  7 00:24:37 2013 from homeserver2.oracle
[oracle@homeserver1 ~]$ 

[oracle@homeserver1 .ssh]$ ssh oracle@homeserver2
Last login: Tue May  7 00:23:31 2013 from homeserver1.oracle
[oracle@homeserver2 ~]$


Hence works !

ssh: connect to host port 22: No route to host

I had this error after i had implemented ssh rsa password less authentication( refer my next post for steps to implement ssh rsa password less authentication).

[oracle@homeserver1 .ssh]$ ssh oracle@homeserver2
ssh: connect to host homeserver2 port 22: No route to host

I had firewall enabled which had restriction to port 22 on server homeserver2.

Below are the commands to check the status and turn off firewall.

[root@homeserver2 ~]# /sbin/service iptables stop
Flushing firewall rules:                                   [  OK  ]
Setting chains to policy ACCEPT: filter                    [  OK  ]
Unloading iptables modules:                                [  OK  ]
[root@homeserver2 ~]# /sbin/service iptables status
Firewall is stopped.

After which i was able ssh to my remote server homeserver2 from homeserver1 successfully.

[oracle@homeserver1 .ssh]$ ssh oracle@homeserver2
Last login: Tue May  7 00:23:31 2013 from homeserver1.oracle
[oracle@homeserver2 ~]$