How to SSH Login without password using key pair With Examples

In last post we saw how to use Expect Script for login to remote server using SSH. The problem with Expect script is that your password is written in a normal text file and can be compromised. Similar problem arises with password because if someone knows your password, he can easily login to your remote server.

SSH provides a more secure way to login using SSH Public Key authentication that doesn’t require password. This method has two levels of security because it also requires a passphrase, so hacker will need both of these to login to remote server. In this post, we will learn how easily we can setup public key authentication between your local machine and remote server.

Generate SSH Key Pair

First of all we need to generate the public and private keys that will be used for SSH authentication purpose. We can generate these using ssh-keygen. The private and public key needs to be generated at the local machine.

For better security purpose, you should never leave passphrase empty. Once the SSH key pair is generated we are ready to move to next step.

Setting up Remote Server with Public Key

Once the public key is generated (/Users/pankaj/.ssh/id_rsa.pub), the next task is to copy it over to the remote server. You can use ssh-copy-id for copying the public key to the remote server but it’s not available in OpenSSH. So you will have to either SFTP the public key or you can just copy paste it to the authorized_keys at the remote server. Also we need to change the permissions on the ssh directory and authorized_keys file.

After you are done with above steps, you can login to the remote server without using password.

Important Points

By admin

Leave a Reply

%d bloggers like this: