Tuesday, April 19, 2011

Passwordless SSH login HOWTO

Hello Folks,

Its been a long time since my last post, but now I would try to be more consistent. In this post I am going to explain the step's to setup "Passwordless SSH" to a remote machine.

Quite often we feel the need to login to a remote machine (maybe to backup our daily work or to run few test's..) what ever maybe the reason, we endup entering the password of that machine. Today we will learn howto setup a "Passwordless SSH" to a remote machine.

Before we begin, there are few prerequisites that are needed to successfully perform the task at hand. The remote machine should have sshd (ssh server) installed on it. If not install it using the command

     > sudo apt-get install openssh-server

The above command works only on Ubuntu, on other OS's like fedora use "yum install" etc.

Now a days most of the OS's have openssh-client installed on them by default, if not present install it. This is to be present on the machine from which you are going to ssh to the remote machine.

Once these are done, follow the step's below to successfully perform passwordless ssh login.

1. Enter the command

     >ssh-keygen -t rsa
       Generating public/private rsa key pair.
       Enter file in which to save the key (/user/.ssh/id_rsa):
       Enter passphrase (empty for no passphrase):
       Enter same passphrase again:
       Your identification has been saved in /user/.ssh/id_rsa.
       Your public key has been saved in /user/.ssh/id_rsa.pub.
       The key fingerprint is:
       e2:8e:8a:91:f2:db:06:77:22:29:27:e0:4f:7a:3b:71 user@user-laptop
       The key's randomart image is:
       +--[ RSA 2048]----+
        |                           |
        |                           |
        |                           |
        |.                          |
        |o  .  . S                |
        |oo=ooE..            |
        |++++oo.            |
        |.= =.o                 |
        |. === .               |
       +--------------------------+

For more info on ssh-keygen go through its man page.

When it asks for the file just press enter and when it asks for the passphrase enter the passphrase else just press enter.

Now it would have generated two files id_rsa and id_rsa.pub in /user/.ssh directory. The id_rsa file is the private key which you must never disclose it to anyone.

2. Now copy the public key to the remoter machine. One of my favorite ssh command variant for this is

    > ssh-copy-id -i /user/.ssh/id_rsa.pub remote-user@remote-machine

That's it, now you can login into the remote machine without entering the password.


One of the common problem that we face while using ssh to login to the remote machine may be

@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
@    WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!     @
@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@@
IT IS POSSIBLE THAT SOMEONE IS DOING SOMETHING NASTY!
Someone could be eavesdropping on you right now (man-in-the-middle attack)!
It is also possible that the RSA host key has just been changed.
The fingerprint for the RSA key sent by the remote host is
af:b6:ab:a9:c5:5e:ab:bb:b8:1c:1c:d6:82:0c:ce:9b.
Please contact your system administrator.
Add correct host key in /user/.ssh/known_hosts to get rid of this message.
Offending key in /user/.ssh/known_hosts:34
RSA host key for remote-machine-ip has changed and you have requested strict checking.
Host key verification failed.


If you face this problem, then just delete the file /user/.ssh/known_hosts and you are good to go.

1 comment: