There are a number of use cases where by logging in via SSH (without a password) is the best (or maybe only) option. For example, if you wanted to run an automated backup on a remote server that would upload files to another remote server via scp, you would need SSH to work without a password. This post will guide you through the process of setting up passwordless SSH, such that you will be able to use scp and other utilities that rely upon SSH for authentication without having to use a password.


Step 1: Generate a Private/Public Key Pair

Open up a terminal window on the machine from which you wish to access a remote machine.

Check to see if you already have an RSA key for the current user:

cd ~/.sshls -l

If the following files already exist, then you can skip this step:

-rw------- 1 user group 1502 Jul  18  2013 id_rsa-rw-r--r-- 1 user group  360 Jul  18  2013 id_rsa.pub

If you don't already have the files, run the following command:

ssh-keygen -f ~/.ssh/id_rsa -t rsa

When prompted to enter a pass phrase, just hit enter (both times). This will allow the use of the RSA key without a password.


Step 2: Copy the Public Key to the Remote Server

Use scp to transfer your public key file to the remote server that you wish to access:

scp ~/.ssh/id_rsa.pub user_name@ip_address:~/

Replace user_name with the username you usually use to SSH into the remote server. You'll be prompted for this user's password.

Replace ip_address with the IP Address of the remote server that has the database export on it.


Step 3: Add Public Key to Remote Server's Authorized Keys File

Secure shell (SSH) into the remote server:

ssh user_name@ip_address

Replace user_name with the username you usually use to SSH into the remote server. You'll be prompted for this user's password.

Replace ip_address with the IP Address of the remote server that has the database export on it.

Append the Public Key to the remote server's authorized keys file:

cat ~/id_rsa.pub >> ~/.ssh/authorized_keys

Set the appropriate permissions on the authorized keys file:

chmod 0644 ~/.ssh/authorized_keys

Delete the Public Key file, just to tidy up:

rm ~/id_rsa.pub

Step 4: Test

Open a new terminal window, and try to secure shell into the remote server again:

ssh user_name@ip_address

You should no longer be prompted for a password and the secure shell session should start right away.