Nothing new with that, but as I never remember the correct procedure, I decided to write this reminder for myself 🙂
So, let’s assume you are user A on your machine A (in my case it’s my laptop), and you want to connect as user B to machine B (in my case it’s a dedicated server). And you don’t want to be asked userB’s password each time you log in. This tutorial was made for Linux OSs and openSSH.
1. Generate a pair of authentication keys
As userA on machineA, generate a pair of authentication keys (a private key and a public key):
userA@machineA # ssh-keygen -t -rsa
Let all the default values, just press enter at each question. Two files should be created in /userA/home/folder/.ssh/:
- id_rsa is the private key that you must keep for yourself and never share
- id_rsa.pub is the public key, that you give to every remote machine where you want to log in with this technique
More info about the public-key/private-key cryptography approach at http://en.wikipedia.org/wiki/Public-key_cryptography
2. Check that the directory /userB/home/folder/.ssh/ exists
As userB on machineB, check that /userB/home/folder/.ssh/ exists. If not, create it.
userB@machineB # mkdir ~/.ssh/
3. Append your public key to userB’s authorized keys file
userA@machineA # cat ~/.ssh/id_rsa.pub | ssh userB@machineB 'cat >> ~/.ssh/authorized_keys'
This should be the last time you’re asked userB’s password.
If you want to do the same operation for other hosts, mind that you don’t have to redo step 1 for every machine. You can use the same id_rsa.pub file.