Skip to content

Instantly share code, notes, and snippets.

@srics
Forked from makuk66/cloudstack-sethostname.sh
Last active April 25, 2016 14:44
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 2 You must be signed in to fork a gist
  • Save srics/8e59654004fc38bed9fe to your computer and use it in GitHub Desktop.
Save srics/8e59654004fc38bed9fe to your computer and use it in GitHub Desktop.
#!/bin/sh
# Filename: /etc/dhcp/dhclient-exit-hooks.d/sethostname
# Purpose: Used by dhclient-script to set the hostname of the system
# to match the DNS information for the host as provided by
# DHCP.
# logs in /var/log/syslog
#
echo dhclient-exit-hooks.d/sethostname: reason = $reason, interface = $interface
if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
&& [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
return
fi
if [ $interface != "eth0" ]; then
return
fi
# for debugging:
echo dhclient-exit-hooks.d/sethostname: BEGIN
echo new_ip_address=$new_ip_address
echo new_host_name=$new_host_name
echo new_domain_name=$new_domain_name
oldhostname=$(hostname -s)
if [ $oldhostname != $new_host_name ]; then
# Rename Host
echo $new_host_name > /etc/hostname
hostname -F /etc/hostname
# Rename Host
echo $new_host_name > /etc/hostname
hostname -F /etc/hostname
# Rename Domain
domainname $new_domain_name
# Update /etc/hosts if needed
TMPHOSTS=/etc/hosts.dhcp.new
if ! grep "$new_ip_address $new_host_name.$new_domain_name $new_host_name" /etc/hosts; then
# Remove the 127.0.1.1 put there by the debian installer
grep -v '127\.0\.1\.1 ' < /etc/hosts > $TMPHOSTS
# Add the our new ip address and name
echo "$new_ip_address $new_host_name.$new_domain_name $new_host_name" >> $TMPHOSTS
mv $TMPHOSTS /etc/hosts
fi
fi
echo dhclient-exit-hooks.d/sethostname: END
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment