How to setup passwordless SSH Authentication in Linux ?

On Local Machine, from where you will try to login another machine, we need to use ssh-keygen to create ssh public/private keys. ssh-keygen generates, manages and converts authentication keys for ssh.

$ ssh-keygen 

Generating public/private rsa key pair.
Enter file in which to save the key (/home/devbee/.ssh/id_rsa):
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/devbee/.ssh/id_rsa.
Your public key has been saved in /home/devbee/.ssh/id_rsa.pub.
The key fingerprint is:
SHA256:oq5Hd9kjiJibI5tI9RvsM4InJ7nyRzeVR3nTjDY5kL8 devbee@laptop
The key’s randomart image is:
+—[RSA 2048]—-+
| .+ = |
| + O o |
| o + + |
| o . . |
| + ..oS+ . |
| + *.=.+ o E |
| .o=.* o . . |
|=*+=+oo |
|==O=ooo |
+—-[SHA256]—–+

This will create public and private ssh keys at your /home/myuser/.ssh directory.

$ ls -l /home/devbee/.ssh/
total 12
-rw------- 1 devbee devbee 1679 Aug 24 09:13 id_rsa
-rw-r--r-- 1 devbee devbee 395 Aug 24 09:13 id_rsa.pub
-rw-r--r-- 1 devbee devbee 442 Jan 31 2018 known_hosts

Copy the contents of ssh public key as, ( copy output of below command )

$ cat /home/myuser/.ssh/id_rsa.pub

and then first using your username and password, login to remote machine as,

$ ssh remote_username@remote_machine_ip 

[ Note: for adding key to remote machine, if you do not have access, you can send the key to your admin and ask them to append ]
Now, on remote machine, you need to append the copied ssh id_rsa.pub public key to authorized_keys as,

 $ vim /home/remote_user/.ssh/authorized_keys 

Append copied key, save and exit.

Now, exit from remote machine, and try login to remote machine from localmachine as,

 $ ssh remote_user@remote_ip_address 

This should login without asking any password. ( Since we have already authenticated using ssh public key )

Leave a Comment