By using SSH keys, you can easily and securely connect to one or multiple servers without having to type your password every time. I’m assuming you already have and use openssh. I’m using following procedure on Red Hat, Debian and Ubuntu servers. You can also generate key pairs on Windows host with the puttygen.exe tool.
To generate a key of 4096 bits using RSA protocol version 2, type :
$ ssh-keygen -t rsa -b 4096
Generating public/private rsa key pair.
Enter file in which to save the key (/home/beset/.ssh/id_rsa): /home/beset/.ssh/id_beset (I suggest you use a more descriptive name if you think you will need more than one key pair)
Enter passphrase (empty for no passphrase): (I highly recommend you use a passphrase to protect your private key)
Enter same passphrase again:
Your identification has been saved in /home/beset/.ssh/beset.
Your public key has been saved in /home/beset/.ssh/beset.pub.
The key fingerprint is:
67:96:b1:b3:10:18:d9:eb:7f:fc:bf:87:a1:62:65:8c beset@carbon
The key's randomart image is:
+--[ RSA 4096]----+
| .o |
| .o. |
| . ... |
| .. + |
| .S Bo |
| .=Eo+ . |
| ..+ . o |
| + + . .|
| . o ..o+|
+-----------------+
Two files will be generated in your local ~/.ssh folder, in this example : id_beset (private key) and id_beset.pub (public key).
To make things work, you have to copy the content of the public key file in the ~/.ssh/authorized_keys of the remote user on the remote server.
You can copy the key manually, or use the command :
ssh-copy-id -i ~/.ssh/id_beset.pub myUser@remoteHost
If you want to specify a port, you have to use quotes :
ssh-copy-id '-p4456 -i ~/.ssh/id_beset.pub myUser@remoteHost'
You should now be able to connect without having to type your password. If it doesn’t work, you should look at the /var/log/auth.log (or /var/log/secure) for clues… Usually, you will have to check permissions of .ssh folder (700) and public key (600) on remote host.
Finally, if you use many key pairs, verify the authorized_keys file of the remote server since ssh-copy-id has the annoying habit of copying all public keys instead of only the specified one…
Have fun!