Skip to content

Instantly share code, notes, and snippets.

Created June 11, 2013 15:27
Show Gist options
  • Save anonymous/5757819 to your computer and use it in GitHub Desktop.
Save anonymous/5757819 to your computer and use it in GitHub Desktop.
Update local hostname entry in /etc/hosts when an interface is brought up
#!/bin/bash
if [[ "$1" == eth* ]]; then
echo "this part will be executed right after an interface is up."
# find out the ip
IP=$( ifconfig | grep "inet " | head -n 1 | awk '{print $2}' | awk 'BEGIN {FS = ":"} ; {print $2}' )
# delete the existing lines in the file
FQDN=`hostname`
HOSTNAME=`hostname -s`
sed -i "/${HOSTNAME}/d" /etc/hosts
# add the correct line
echo "${IP} $HOSTNAME $FQDN" >> /etc/hosts
fi
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment