Skip to content

Instantly share code, notes, and snippets.

@ngyuki
Created November 18, 2014 13:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ngyuki/e63c70e6ec8a0863cdd4 to your computer and use it in GitHub Desktop.
Save ngyuki/e63c70e6ec8a0863cdd4 to your computer and use it in GitHub Desktop.
auto set hostname for centos
#! /bin/bash
#
# chkconfig: 2345 11 89
. /etc/init.d/functions
[ -x /sbin/ip ] || exit 1
[ "$EUID" != "0" ] && exit 4
case "$1" in
start)
case `hostname` in
""|localhost.localdomain)
ip=`/sbin/ip -4 -o route get 255.255.255.255 |\
/bin/grep -Eo 'src\s+\S+' |\
/bin/awk '{print $2}'`
if [ -n "$ip" ]; then
hostname "ip-`echo "$ip" | sed 's/\./-/g'`"
echo -n "Auto set hostname `hostname`"
success
echo
fi
esac
;;
stop|status|restart|reload|force-reload)
;;
*)
echo $"Usage: $0 {start|stop|status|restart|reload|force-reload}"
exit 2
esac
exit 0
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment