Monday, May 19, 2008

SSH authentication using keys

First you have to create a key. Although using a password to further protect your key is not obligatory, I personally very much recommend using a password:

$ ssh-keygen
Generating public/private rsa key pair.
Enter file in which to save the key (/home/user/.ssh/id_rsa):
Enter passphrase (empty for no passphrase): xxxxxxxxxxx
Enter same passphrase again: xxxxxxxxxxx
Your identification has been saved in /home/user/.ssh/id_rsa.
Your public key has been saved in /home/user/.ssh/id_rsa.pub.
The key fingerprint is:
xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx:xx user@host


Then you have to upload your public key to the remote host:

$cat /home/user/.ssh/id_rsa.pub |ssh user@host "cat >> .ssh/authorized_keys"

Before you connect to the remote host, you can use the following command:

$ ssh-add
Enter passphrase for /home/user/.ssh/id_rsa:
Identity added: /home/user/.ssh/id_rsa (/home/user/.ssh/id_rsa)


This way you don't have to provide the password for your key each time you connect to the remote host.

Great page with some more very useful ssh tricks here.

No comments: