SSH logins using RSA key-pair authentication

As mentioned previously, I had to mirror a server for testing purposes across two networks through a computer in the middle. The best way I found was to do an rsync over ssh, but this requires a non-password authentication, hence I have to set up a RSA key-pair login.

Server setup

First, the ssh server must allow this authentication method. Make sure /etc/ssh/sshd_config has the following:


Protocol 2            # use ssh protocol 2!!

# Authentication

RSAAuthentication      yes
PubkeyAuthentication   yes
AuthorizedKeysFile      .ssh/authorized_keys

Restart your ssh server. You might want to start another ssh session first, in case there's something wrong and your ssh server can't restart.

Make sure you are the only user able to access ~/.ssh/authorized_keys by changing the permissions to 600.


[user@server] $ chmod 600 ~/.ssh/authorized_keys

Client side

The default location of the RSA key-pair is in ~/.ssh/id_rsa (private key) and ~/.ssh/id_rsa.pub (public key). This is set in /etc/ssh/ssh_config of your client computer, under IdentityFile.

In your own account, generate a RSA key-pair. You do not need a password passphrase. Store the keys in the location indicated in your ssh client configuration.


[user@client] $ ssh-keygen -t rsa

Change the permissions such that only you can read the files.


[user@client] $ chmod 600 ~/.ssh/id_rsa
[user@client] $ chmod 600 ~/.ssh/id_rsa.pub

Append your public key to the ssh server. Of course you can cut and paste, or you can do this:


[user@client] $ cat ~/.ssh/id_rsa.pub | ssh username@ssh.server "cat - >> ~/.ssh/authorized_keys"

Test your setup

If everything works, you should be able to login without typing in any passwords. Hooray!