Monday, 14 March 2016

Background

In this post we will see how to do a SSH key based authentication where you do not need to enter your password. For demo purpose I am going to start a SSH server on my Linux Ubuntu machine and then connect to it from my same linux machine without password.

Starting SSH server

First lets install openssh server.

sudo apt-get install openssh-server

Now lets take a backup of config file so that we have a good config too look at later (in case we mess things up ;) ) -

Execute the following commands -

sudo cp /etc/ssh/sshd_config /etc/ssh/sshd_config.factory-defaults

sudo chmod a-w /etc/ssh/sshd_config.factory-defaults

sudo gedit /etc/ssh/sshd_config

NOTE : sshd_config is the configuration file for the OpenSSH server. ssh_config is the configuration file for the OpenSSH client. Make sure not to get them mixed up.

Now simply restart ssh server -

sudo restart ssh

You can ssh into same machine using localhost just to test your ssh server setup -

Sure you can do the same from any other machine using password

But in this we want to do this without password -

Configuring ssh connection without Password

You need to execute following commands -

ssh-keygen

ssh-copy-id -i ~/.ssh/id_rsa.pub aniket@localhost

1st step creates a new public private key pair on your local machine. 2ms step copies your public key to the remote machine (localhost in this case) and from then on remote machine will remember your identity.

As you can see post copying the public key to servers authorized keys you no longer need password to connect to your SSH server.