SSH – login without password

 

If you are in the mood of login to Linux without prodividing the user password, as for automating linux script, well you’re in the right page. In this following we will set up a sketch using user alice on host Earth who wants to log in on host Mars without providing any password. Let’s start. User barbara is a valid user on Mars.

How to do it

First log in on Earth as user alice and generate a pair of authentication keys. Do not enter any passphrase:

alice@Earth:~> ssh-keygen -t rsa
Generating public/private rsa key pair.
Enter file in which to save the key (/home/a/.ssh/id_rsa): 
Created directory '/home/alice/.ssh'.
Enter passphrase (empty for no passphrase): 
Enter same passphrase again: 
Your identification has been saved in /home/alice/.ssh/id_rsa.
Your public key has been saved in /home/alice/.ssh/id_rsa.pub.
The key fingerprint is:
3e:4f:05:79:3a:9f:96:7c:3b:ad:e9:58:37:bc:37:e4 alice@Earth

Now use ssh to create a directory ~/.ssh as user barbara on Mars. (The directory may already exist, which is fine):

alice@Earth:~> ssh barbara@Mars mkdir -p .ssh
barbara@Mars's password: 

Finally append amanda‘s new public key to barbara@Mars:.ssh/authorized_keys and enter barbara‘s password one last time:

amanda@Earth:~> cat .ssh/id_rsa.pub | ssh barbara@Mars 'cat >> .ssh/authorized_keys'
barbara@Mars's password: 

From now on you can log into Mars as barbara from Earth as alice without password:

alice@Earth:~> ssh barbara@Mars

Depending on your version of SSH you might also have to do the following changes:

  • Put the public key in .ssh/authorized_keys2
  • Change the permissions of .ssh to 700
  • Change the permissions of .ssh/authorized_keys2 to 640