in

How to Setup Passwordless SSH to a Linux Server

- - 2 comments
How to Setup Passwordless SSH to a Linux Server: A lot of the stuff that we do on our Linux computers can be automated, especially things such as taking backups. It helps to have quick, easy, and secure access to your remote servers to place your backups there. This is where having a passwordless SSH setup comes in great use. It can also be quite useful if you connect to your servers on a regular basis.

Passwordless SSH access not only makes connecting to your servers easier but it is also a lot safer than typing in a password. Let's look at how to set this up. It will come in use for many of the tutorials that we will feature later on.

We will use two computers for this exercise. There's your local machine -it could be a laptop or a desktop. I'll refer to this as the "localcomp". Then there's the remote computer, which could again be a laptop or a desktop, and could be part of your local network or be located somewhere over the Internet. This will be referred to as the "remotecomp". Both computers must have Linux or UNIX installed so that we can use SSH on them. Although I'm using Ubuntu on my local computer and have Debian Linux installed on my remote shared server, these steps should work on most computers.

The first step you need to perform is to generate a key on your local computer. Execute the following command to do so.

localcomp# ssh-keygen -t dsa

The previous command would have created a new file in the .ssh directory in your home folder. Now copy the file containing the key that you generated from the local computer to the remote server using the SCP command. Replace "remotecomp" with the IP or the address of the remote machine.

localcomp# scp ~/.ssh/id_dsa.pub remotecomp:~/

SSH to your remote server to perform the next few steps.

localcomp# ssh calvin@remotecomp
Once you are logged into the remote server you need to copy the key file's contents into the file "~/.ssh/authorized_keys" which contains the authentication keys for all passwordless remote connections. Note the use of two > signs as opposed to one. This is a very important thing you need to make sure of before you execute the command.

remotecomp# cat ~/id_dsa.pub >> ~/.ssh/authorized_keys

Once you have done that all you need to do is to set the correct permissions for the key file on your remote server, and your setup should be complete. You can log out of your SSH session and return to your local computer.

remotecomp# chmod 644 ~/.ssh/authorized_keys

remotecomp# exit

Once you return to your local computer you can run a test to make sure things have been setup correctly. Try executing the following step. You should not be asked for the password this time over.

localcomp# ssh username@remotecomp
Once you have the passwordless SSH setup, there are a number of optimizations that you can do to your workflow. The process of connecting to the remote server has now become quick and secure. You can also SCP files without having to key in a password, which is particularly useful when you write a script that needs to SCP files.


This article was written by Sukrit Dhandhania for TechSource.

Do you want to become a TechSource article contributor or writer? Feel free to CONTACT US.

2 comments

  1. There's also this command:
    ssh-copy-id -i ~/.ssh/id_rsa.pub root@server1.domain.com
    which will combine three of the steps (scping the public key, appending it to the autorized_key file and setting the permissions) into one.

    ReplyDelete
  2. @alexkiousis thanks for the tip :)

    ReplyDelete