Thursday, February 17, 2011

Password free login to SSH using public keys

Many of us at some point in time have faced this situation of working remotely on a Unix/Linux box. The most preferred way is through SSH. But every time you login to a machine remotely you have to provide your password. How about logging in without password; using public keys ???

To demonstrate this method, I will be using 2 Linux machines.
1. Caffeine: This will act as ssh server.
2. Coffee: This machine will act as client.

Server Side Settings (Caffeine)
On the server we need to start the ssh daemon.  $ sudo /etc/init.d/sshd start

Start the SSH server
That's all we need to do on the server side !!

Client Side Settings (Coffee)
In order to login without password, first you need to create public-private key pairs.
In order to do this on linux (fedora or ubuntu ) execute   $ ssh-keygen

Create public-private key pair
Just hit and accept the default file name. Enter the pass-phrase or hit enter for none.
This step would have created 2 files as shown below in your  ~/.ssh folder.

Public/Private key files
Of the two files, id_rsa contains the private key and id_rsa.pub contains the public key.
After we have the public and private key files, we need to add these keys to the authenticating agent. Its the authentication agent who performs the authentication on our behalf. To add the keys execute $ ssh-add
If you have provided the password in the above step, it will ask for a password

ssh-add, keys with password
else it will not

ssh-add, keys without password
The next step is to copy and paste the public key from id_rsa.pub into Server's (Caffeine)   ~/.ssh/authorized_keys file on a new line. This step needs to be done once, either manually or by executing    $ ssh-copy-id user@machine. You will be prompted for your password.

** Important: The file permission for   ~/.ssh/authorized_keys must be 400, else it will not work. **

ssh-copy-id
Testing !!
That's all that is needed. You can now try to login to the server without passwords.
To login just type  $ ssh user@machine or like most of the cases if the user-name is same on both machines then just $ ssh machine

Login with user and machine names


If the username is same, just use machine name
----- I tried so hard and got so far, but in the end it doesn't even matter -----

No comments:

Post a Comment