Configure password-free login for remote SSH servers

If you use SSH to log in to a remote machine, you normally need to enter a password. This also applies to any of the systems built on top of SSH, such as SFTP or SCP, and those which can use it as an optional underlying transport, such as Subversion.

To do secure logins from one machine to another without a password, you must create a public-key/private-key pair on the local machine, copy the public key to the remote machine, and put it in the right place over there, where ssh will expect to find it.

It’s easy to get this just slightly wrong. The files at the far end need to have the right permissions, for example, or ssh will still prompt you for a password. I do this regularly and still I often forget bits.

So here’s a simple little script called sshput which will set it up for you. If you have an account on a machine called wiggle, you should be able to run sshput wiggle, and thereafter log in without a password using ssh wiggle.

You can copy and paste it from below, or right-click as…’ or ‘Download…’ option.

# sshput <remotehost>
# Puts your local DSA public key into the .ssh/authorized_keys 
# on a remote machine.  This should allow you to login without
# needing a password.
# This software comes with no guarantees whatsoever, and is yours to 
# do with as you will. I'd be grateful if you feed any generally-useful 
# improvements back to me, for the benefit of others.
#                Quentin Stafford-Fraser


if [ $# -ne 1 -o "$1" = "-h" ]
    echo Syntax:
    echo "$0 [user@]<remotehost>"
    exit 1

if [ ! -r ${PUBKEY} ]
    echo Public key ${PUBKEY} not found.
    echo You can generate this by running
    echo "  ssh-keygen -t dsa"
    echo Then come back and run $0 again.
    exit 1

echo If you are prompted for a password, enter your password on the
echo remote machine.

cat ${HOME}/.ssh/ | \
  ssh $1 'mkdir -p -m 0700 ${HOME}/.ssh && \
    cat >> $HOME/.ssh/authorized_keys && \
    chmod 0600 $HOME/.ssh/authorized_keys'

if [ $? -eq 0 ]
    echo Public key installed on remote machine.
    echo You should now be able to connect with
    echo "    ssh $1"
    exit 0
    echo Sorry, an error occurred!
    exit 1