sshput

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 here and use your browser's 'Save as...' or 'Download...' option.

#!/bin/sh
# 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  http://www.qandr.org/quentin

PUBKEY="${HOME}/.ssh/id_dsa.pub"

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

if [ ! -r ${PUBKEY} ]
then
    echo
    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.
    echo
    exit 1
fi

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

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

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



Quentin