ssh on remote host without typing the password

When you connect to a remote host with SSH, you need to enter the password, which can be cumbersome on the long term when connections are frequent. Here is a solution to make the connection automatic.

First, if not already done, you will need to create a local public/private keys. You may choose between 2 versions using either “ssh-keygen” (SSH version 1) or “ssh-keygen -t rsa” (ssh version 2). We consider only the version 2 in this example, which generates 2 files called id_rsa and id_rsa.pub.

cd
cd .ssh
ssh-keygen -t rsa

The last command will ask you for a ‘passphrase’. You can enter an empty passphrase, which is not recommended though.

Second, you need to copy your id_rsa.pub file on the remote host, where you should also find a directory called .ssh in the home.
However, the file id_rsa.pub on the remote host must be renamed into authorized_keys2. Here the number 2 is related to the fact that we used the version 2 of SSH. If you used “ssh-keygen” without option (i.e. version 1) then you should rename id_rsa.pub into “authorized_keys“.

To copy the file on the remote host, type:

scp ~/.ssh/id_rsa.pub user@remotehost:/home/user/.ssh/authorized_keys2

Adapt the code above to your needs by replacing user, remotehost and /home/user path to your needs.

You can now connect to the host without password:

ssh user@remotehost
Please follow and like us:
This entry was posted in Linux and tagged , . Bookmark the permalink.

Leave a Reply

Your email address will not be published.