Easy Passwordless Shell Logins

Summery
In brief if you have new ssh client and server applications on 2 clean systems, you should be able to authenticate yourself using these 2 commands.

ssh-keygen
ssh-copy-id user@server

Once you are authenticated you can hop around computers more easily and run unattended tasks like backups without passing passwords around.

Background
We just got a few more servers at work bringing the total up to 8. About 6 have similar login accounts so life is a bit easier, then again for the number of times you have to login to 8 servers a day typing passwords can be a pain.

When passwords are too short they are not secure enough, too long and you can’t remember them. So key exchange to the rescue. Basically you generate a pair of keys on your computer and add your public key to each of the servers you want to login without passwords.

I read a few guides about this and it worked for a while. Somewhere down the line the new servers refused to authenticate my keys. So I cleaned up and started from scratch.

Step 1: Clean up
If you are starting from new or got messed up along the way you should first clear up your ssh settings.

To check if you have ssh settings run ls -a in your home directory. If there is a .ssh directory run these commands to clear ssh settings. Be careful you will loose any previous authenticated sessions!

cd .ssh
rm *
cd ..
rmdir .ssh

Step 2: Generate the keys
To generate the ssh protocol 2 key set run ssh-keygen in your home directory. Just enter 3 times for the defaults. This is what it would look like;

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

Some servers may use ssh protocol 1. For that key set run ssh-keygen -t rsa1 in your home directory. Enter 3 times for the defaults. You would get something like this.

Generating public/private rsa1 key pair.
Enter file in which to save the key (/home/dilantha/.ssh/identity):
Created directory '/home/dilantha/.ssh'.
Enter passphrase (empty for no passphrase):
Enter same passphrase again:
Your identification has been saved in /home/dilantha/.ssh/identity.
Your public key has been saved in /home/dilantha/.ssh/identity.pub.
The key fingerprint is:

Step 3: Copy the keys
To copy the key most guides gave a manual method, but I found ssh-copy-id does the job. Maybe it was added after these guides were written.

To copy your ssh protocol 2 public key to the server run;
ssh-copy-id user@server
and enter your password, hopefully for the last time.

Now try logging in to you server using ssh user@server

If it doesn’t work try copying the ssh protocol 1 public key like this.
ssh-copy-id -i .ssh/identity.pub user@server

Let me know if there are any issues and I will try to improve this guide.