Skip to content

Instantly share code, notes, and snippets.

@afternoon
Created May 10, 2012 00:04
Show Gist options
  • Save afternoon/2649927 to your computer and use it in GitHub Desktop.
Save afternoon/2649927 to your computer and use it in GitHub Desktop.
Add your public key to authorized_keys on a remote host
#!/bin/bash
# Add yourself to the list of authorized ssh users on a host
host=$1
tmpfile=`mktemp /tmp/authorized_keys.XXXXXX` || exit 1
notice() {
echo -e "\033[33m# $1\033[0m"
}
notice "Adding you to authorized_keys for $host"
notice "You will be prompted for your password multiple times"
notice "Checking for .ssh dir..."
if ssh $host ls .ssh > /dev/null; then
notice "Found"
notice "Attempting to retrieve existing authorized_keys file..."
scp -pq $host:.ssh/authorized_keys $tmpfile
notice "Done"
else
notice "Missing"
notice "Creating .ssh dir..."
ssh $host mkdir -m 700 .ssh
notice "Done"
touch $tmpfile
fi
cat ~/.ssh/id_rsa.pub >> $tmpfile
chmod 600 $tmpfile
notice "Uploading new authorized_keys file..."
scp -pq $tmpfile $host:.ssh/authorized_keys
notice "Done"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment