Skip to content

Instantly share code, notes, and snippets.

@jbreams
Created September 8, 2014 20:03
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save jbreams/f9f5d6a7aafd26072cd3 to your computer and use it in GitHub Desktop.
Save jbreams/f9f5d6a7aafd26072cd3 to your computer and use it in GitHub Desktop.
AWS SSH Key Init Script
#!/bin/bash
#
# Init file for AWS Authorized Keyfile
#
# chkconfig: 2345 11 25
# description: AWS Authorized Keyfile
case $1 in
start)
touch /var/lock/subsys/awssshkey
;;
stop)
rm /var/lock/subsys/awssshkey
exit 0
;;
esac
if [ ! -d /root/.ssh ]; then
mkdir -m 0700 -p /root/.ssh
restorecon /root/.ssh
fi
# Get the root ssh key setup
ReTry=0
rm -f /root/.ssh/authorized_keys
while [ ! -f /root/.ssh/authorized_keys ] && [ $ReTry -lt 5 ]; do
sleep 2
curl -f http://169.254.169.254/latest/meta-data/public-keys/0/openssh-key \
> /root/.ssh/authorized_keys
[ $? -eq 0 ] && echo "Added ssh keys $(< /root/.ssh/authorized_keys)"
ReTry=$[Retry+1]
done
chmod 600 /root/.ssh/authorized_keys && restorecon /root/.ssh/authorized_keys
@jamiethermo
Copy link

You really want -s not -f, since the first curl will write a zero byte file and then the if while will exit. Also, you probably want this to loop forever until it gets keys.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment