Created
November 18, 2014 13:15
-
-
Save ngyuki/e63c70e6ec8a0863cdd4 to your computer and use it in GitHub Desktop.
auto set hostname for centos
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#! /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