Monday, August 12, 2013

Remote login to Linux machine without password

How it works

The ssh-keygen tool stores the private key in $HOME/.ssh/id_rsa and the public key in $HOME/.ssh/id_rsa.pub in the user’s home directory. The user should then copy the contents of id_rsa.pub to the $HOME/.ssh/authorized_keys file in his or her home directory on the remote machine. It also asks for a passphrase. The passphrase may be empty to indicate no passphrase (host keys must have an empty passphrase), or it may be a string of arbitrary length. Instead of RSA, DSA can also be used. The steps to create authorization keys by using the ssh-keygen tool are as follows:

Some of the important options of ssh-keygen command are as follows:

ssh-keygen command options
description
-b bits
Specifies the number of bits in the key to create. The minimum bit length is 768 bits and the default length is 2048 bits.
-C comment
Provides new comment.
-p
Requests changing the passphrase of a private key file instead of creating a new private key.
-t
Specifies the type of key to create.
-q
quiets ssh-keygen. It is used by the /etc/rc file while creating a new key.
-N
Provides a new Passphrase.
-F
For ssh-keygen2, dumps the key's fingerprint in Bubble Babble format


Enter following command to generate public/private rsa key pair.

ssh-keygen -t rsa
Generating public/private rsa key pair.

Enter the path to store the keys.
By default keys will be stored at “/home/user/.ssh/” folder.

Enter file in which to save the key (/home/user/.ssh/id_rsa):
Created directory '/home/user/.ssh'.

Enter the passphrase to encrypt the private key.
                                                                                                                                   
The passphrase you will enter will be used for encrypting your private key. A good passphrase should be alphanumeric having 10-30 character length. Press enter if you don’t want to enter passphrase.

Enter passphrase (empty for no passphrase):

Re-enter the passphrase to confirm

Enter same passphrase again:
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
7e:64:77:08:42:ab:bb:6b:c6:82:cb:fc:9b:3e:95:f6 user@localhost

The private key was saved in .ssh/id_rsa file which is a read-only file.
The public key is saved in .ssh/id_rsa.pub file.

Copy the Public Key onto remote systems' ~/.ssh/authorized_keys file:

Now, you have to copy the public key onto a remote systems' ~/.ssh/authorized_keys file. Create /home/user1/.ssh folder at remote machine if it does not exist.

scp /home/user/.ssh/id_rsa.pub  215.72.60.264:/home/user1/.ssh/authorized_keys

ssh user1@215.72.60.264
Last login: Mon Aug 12 15:31:52 2013 from user.
[user1@localhost ~]$


Done.