Linux Commands

How to Setup SSH without Passwords

SSH is used to remotely log into servers for running the commands and programs. You can log into remote systems via password authentication and via public key authentication. If you regularly use SSH to connect to remote servers, the public key authentication method is best for you. This method is a secure and password-less login method.

In this article, we will explain how to set up SSH without passwords in a Linux operating system. We will be using the command line Terminal application for this purpose. To open the command line Terminal, use the <Ctrl+Alt+T> keyboard shortcut.

We have explained the procedure mentioned in this article on the Ubuntu 20.04 system. More or less the same procedure can be followed in Debian and previous Ubuntu versions.

Follow the steps below to set up SSH without passwords on your Linux system.

Generate A New SSH Key Pair on Local Machine

The first step will be to generate a new SSH key on your local system. To do this, issue the following command in Terminal:

$ ssh-keygen -t rsa

Press Enter to accept all fields as defaults.

The above command will create the keypair, i.e., the public key and the private key. The private key is kept on the system, while the public key is shared. These keys are stored in the .ssh folder.

You can view the keypair generated by entering the following command:

$ ls –l .ssh

Copy Public Key to Remote Machine

In this next step, copy the public key to the remote system that you want to access from your local system without passwords. We will use the ssh-copy-id command that is by default available in most Linux distributions. This command will copy the public key id_rsa.pub to the .ssh/authorized_keys file in the remote system.

The syntax for ssh-copy-id is as follows:

$ ssh-copy-id remote_user@remote_IP

In our example, the command would be:

$ ssh-copy-id tin@192.168.72.136

On the remote system, you can verify the transfer of the public key by viewing the authorized_keys file.

$ cat .ssh/authorized_keys

Set the permission on the authorized_keys file on the remote system to 600. Use the following command to do so:

$ chmod 600 .ssh/authorized_keys

Set the permission on the .ssh directory on the remote system to 700. Use the following command to do so:

$ chmod 700 .ssh

Add Private Key to SSH Authentication Agent on Local Server

In our local machine, we will add the private key to the SSH authentication agent. This will allow us to log into the remote server without having to enter a password every time.

Here is the command to do so:

$ ssh-add

Login to Remote Server Using SSH Keys

After performing the above steps, try logging into your remote sever. This time, you will be able to log into your remote server without entering a password.

That is all you need to set up SSH login without passwords in a Ubuntu 20.04 system. Remember, you can share the public key with anyone, but never share your private key. Anyone with the private key will be able to log into any system having the matching public key.

About the author

Karim Buzdar

Karim Buzdar holds a degree in telecommunication engineering and holds several sysadmin certifications. As an IT engineer and technical author, he writes for various web sites. He blogs at LinuxWays.