Linux : Automated login with SSH keys

By | December 27, 2011

There is a ton of reasons why you would use SSH keys to log into your Linux/Unix systems automatically :

– Faster than entering password
– More secure than password
– Automate login for backup using Rsync
– And a lot more !

Actually, this is pretty easy to do – so let’s start generating the keys.

First, log into the system you want to connect from and type the following command as the user you want to log from :

ssh-keygen -t rsa

Just hit enter when asking where the key should be written (leave at the default location) and hit enter again when asking entering a passphrase (if you enter a passphrase, you will be prompted to enter it every time you use the key… so a little useless for “automated login”!

Now, you got a pair key (~/.ssh/id_rsa and ~/.ssh/id_rsa.pub).

Connect into the system you want to connect to and have a look at directory named “.ssh” in the home directory of the user you are using for automated login (create and chmod 700 if does not exist).

Then, you need to copy the public key (id_rsa.pub) on the system you want to use automated login into the file named “authorized_keys” (located in ~userdir/.ssh).  You may either scp the file into “.ssh/authorizd_keys” or cut and paste into it.

vi .ssh/authorized_keys

Make sure “authorized_keys” file is chmod 600

You can now log automatically on the target system without password.  You can now disable password authentication in /etc/sshd_config for more security (be aware – do not lose your private key!).

Not working?  Make sure you set correct permission file and make sure the user you want to log with is authorized in AllowedUsers if set.