Setting up SSH Public/Private Keys


In this short article, I’m going to show you how to setup SSH Public/Private keys to allow you to SSH into a Linux server without needing to enter your password each time. This is not only a great convenience, but becomes useful for any scripting that utilizes SSH to transfer files from one server to another in an automated way.

On the local computer, enter the following command:

ssh-keygen -t dsa

You should see something similar to the following:

Generating public/private dsa key pair.
Enter file in which to save the key (~/.ssh/id_dsa):  (hit return)
Enter passphrase (empty for no passphrase):  (hit return)
Enter same passphrase again:  (hit return)
Your identification has been saved in ~/.ssh/id_dsa
Your public key has been saved in ~/.ssh/
The key fingerprint is:
(A long string of random characters will be displayed.)

This generates a folder and several files. Paste the content of ~/.ssh/ into the file ~/.ssh/authorized_keys on the remote host. There are two ways to do this. The first way is to copy it to the remote server with the following command:

ssh-copy-id user@remoteserver

Alternatively, you could first copy the file to the remote server with:

scp ~/.ssh/ user@remoteserver:/your/home/directory

Then login to your remote server via ssh and copy the file into your authorized_keys file with the following command:

cat >> .ssh/authorized_keys

And that’s all there is to it. With this simple setup, you should now be able to login to your remote server via SSH without the need to enter a password.