Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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

This comment has been minimized.

Copy link

@jamiethermo jamiethermo commented Apr 27, 2017

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