SSH is the most trusted protocol used in today’s world for remote logins and secure file transfers from one machine to another. There are three different ways of authetication when trying to login with SSH protocol:

  1. Password Based Authentication
  2. Key Based Authentication
  3. Host Based Authentication

We are going to Emphasize on Key-based authentication, which is one of the best ways of authentication if you are working with a lot of machines in your infrastructure. The various usage of the same could be:

  • Automated login with scripts
  • Password Less login to run a remote command
  • Automating Backups

The process for setting up password less login is pretty simple. First create the ssh keys, which is usually a pair of Public and Private keys, out of which we need to put Public key on the destination Machine and have to keep private key on our Machine. Detailed steps are given below

$ ssh-keygen -t dsa

After running this command, you will get an option to provide path where it will save the files, better to leave it untouched unless you really knows what you are doing.

After that you will get an option to use pass-phrase, which again depends on the usage. If you don’t need that, leave it blank and press enter twice. (But be careful, without a pass-phrase, if someone gets access to your private keys, they will have access to all the machines where you have setup your public keys)

Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
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/
The key fingerprint is:
b2:ad:a0:80:85:ad:6c:16:bd:1c:e7:63:4f:a0:00:15 user@host
The key's randomart image is:
+--[ RSA 2048]----+
| E. |
| . |
|. |
|.o. |
|.ooo o. S |
|oo+ * .+ |
|++ +.+... |
|o. ...+. |
| . .. |

Once this done, go to “.ssh” directory in your Home. In that directory you will find 2 new files.

$ cd ~/.ssh

$ ls -l
  ssh/id_rsa : identification (private) key
  ssh/ : public key

Public key is something which you have to copy to other machines, whom you want to access from your machine.

Private key, needs to be kept with you only. If someone gets access to your private key, then they can access all the machine, password less where your public key is stored.

Now, copy the Public key to the ~/.ssh/authorized_keys file on remote machine.

Either you can do it manually or better way is to use ssh-copy-id command. This command will use the ssh and will copy your public key to the remote machine. You can use the command like this:

$ ssh-copy-id -i user@machine

If you just want to do it manually then change the permission of the file to 600 after copying your public key to the machine.

$ chmod 600 ~/.ssh/authorized_keys

That’s it. Now you should be able to login to the remote Machine without being asked for any password.

If you enjoyed this post, make sure you subscribe to my RSS feed!!!!