Easy Remote Colonization

Posted on Mar 16, 2011

Now that I’m managing a few linux servers (personal, not work related), I’ve found it handy to have automatic login via ssh-keys. At first it wasn’t so bad copying keys up to the server, but when you have 5 or so, it becomes a bit tedious (especially when I can never remember things). I’ve found a script to automate the process from StackOverflow:

if [ -z "$1" ]; then
    echo "colonize.sh by Brian Enigma <brian@netninja.com>"
    echo "Function: copies ssh public keys to remote machines for passwordless login"
    echo "Usage: colonize.sh <username>@<servername or ip address>"
    exit 1
CONTENT=`cat ~/.ssh/$KEY`
echo "Attaching key to authorized_keys file"
ssh $1 "mkdir -p .ssh && chmod 700 .ssh && touch .ssh/$STORE \
    && chmod 644 .ssh/$STORE \
    && echo '$CONTENT' >> .ssh/$STORE \
    && rm -f $KEY"
echo "Complete!  You should be able to log in without a password now!"

Note that before this works, you must have generated your keys on your local box first:

ssh-keygen -t rsa