Plant my key

Jul 16, 2009 • alan


Here’s a neat little script that I developed at “the oven place”. It “plants” my SSH key on a remote machine, so I can get in later without entering a password.

#!/bin/sh
user_at_machine=$1
# plant my SSH key on the target machine
cat $HOME/.ssh/id_rsa.pub | \
   ssh $user_at_machine \
      "if [ ! -d .ssh ] ; then mkdir -m 700 .ssh ; fi ; \
      cat >> .ssh/authorized_keys"

All of the action takes place in one single line (which I broke up here so it would wrap OK). It takes your public key from the machine you’re on, and it pipes it into an SSH session, which you’ll have to type your password into. On the remote machine, it creates a .ssh directory if it needs to, and then it appends your public key to an authorized_keys file.

The next time you log in to that remote machine, you will not need to enter your password.