Please feel free to email me with any questions at with "blog" as the subject.

Search my blog:

Loading...

Sunday, 19 September 2010

How to set up passwordless ssh

If you have a server or remote machine that you ssh into a lot or want to run a backup script to, you probably have to type in your ssh password every time.

If its no-body but you that has access to your user account on the local machine you can set up passwordless ssh between the local machine and the server securely.

This is done with rsa keys.

First you need to make a public key on the local machine.

bash$ ssh-keygen -t rsa

use -t rsa unless you plan on accessing an older machine or a machine with an older version of OpenSSH.

Do NOT enter a passphrase.

Now you need to copy the public key to the remote machine's list of authorized keys.

bash$ ssh-copy-id -i ~/.ssh/id_rsa.pub USERNAME@SERVER

Of course, you should use your custom public key if you did not use the default name in the previous step.

After executing this, it will ask you for your password, this is just the ssh password to the remote machine for the username that you used.

Upon completion, there should be a file on the remote machine ~/.ssh/authorized_keys that contains the public key that you just generated.

You can test out if you were successful now by ssh'ing to the remote machine, you should no longer be asked for a password.


However this is potentially insecure as if someone managed to get hold of your public key they could get into your machine.

To prevent this you can edit the file .ssh/authorized_keys and add from="LOCAL-IP-ADDRESS" to the beginning, leaving a space before ssh-rsa and replacing LOCAL-IP-ADDRESS with your machines IP. This will of course only work with static IP's.

You can further restrict what can be done with passwordless ssh by adding any of the following options to the beginning of the authourised_keys file:

no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty


This will still not let you run passwordless ssh from cron. If you wish to do this I recommand THIS tutorial.

(Some content from patsissons on the ubuntu forums)