-
-
Save srics/8e59654004fc38bed9fe to your computer and use it in GitHub Desktop.
This file contains hidden or 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/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