Linux Tip: Setting up key-based authentication.

linux-penguinIn this post I’ll explain how to use key-based SSH authentication in Linux.

This can be used either for non-password based authentication for scripts or other utilities, or for extra security when paired with a pass phrase key.

We’ll need to generate a key-pair, a public-key and a private-key. The public-key will be placed on the server, and you will log in with your private-key. You will need to enter a passphrase for security (or leave blank for password-less auth.)

First generate the key:

ssh-keygen -t rsa -b 1024 -C “your-email-address”

This will create a 1024 bit key using RSA in your current directory. You will need to transfer it to the server you wish to authenticate with by some means, you can use SCP if you have an SCP client like this:

scp -p id_rsa.pub user@server:~/

Then log in to the remote server, and put the file in the authorized_keys for the user you want to authenticate with.

mkdir ~/.ssh
chmod 700 ~/.ssh
cat ~/id_rsa.pub >> ~/.ssh/authorized_keys
chmod 600 ~/.ssh/authorized_keys
mv id_rsa.pub ~/.ssh

Sometimes you may have to delete the public key file on the local machine to be able to log in, e.g.

rm rsa.pub

Once you’re done, try to log in to the remote host, and if everything was done right, you should be in!

Related Posts

SociBook del.icio.us Digg Facebook Google Yahoo Buzz StumbleUpon

Tags: , , ,

Leave a Reply

You must be logged in to post a comment.