Last active
August 14, 2019 13:26
-
-
Save nubbel/275db69dfb268a9796de35eced3c49ff 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 | |
# dhclient change hostname script for Ubuntu | |
# /etc/dhcp/dhclient-exit-hooks.d/sethostname | |
# logs in /var/log/upstart/network-interface-eth0.log | |
set -x | |
export | |
if [ "$reason" = "BOUND" ] || [ "$reason" = "RENEW" ] || [ "$reason" = "REBIND" ] || [ "$reason" = "REBOOT" ]; then | |
echo new_ip_address=$new_ip_address | |
echo new_host_name=$new_host_name | |
echo new_domain_name=$new_domain_name | |
new_fqdn="$new_host_name.$new_domain_name" | |
echo new_fqdn=$new_fqdn | |
old_fqdn=$(hostname -f) | |
echo old_fqdn=$old_fqdn | |
if [ ! -z "$new_host_name" ] && [ "$old_fqdn" != "$new_fqdn" ]; then | |
# Rename Host | |
hostnamectl set-hostname $new_host_name | |
# Update /etc/hosts if needed | |
TMPHOSTS=/etc/hosts.dhcp.new | |
if ! grep "$new_ip_address $new_fqdn $new_host_name" /etc/hosts; then | |
# Remove the 127.0.1.1 put there by the debian installer | |
grep -vF '127.0.1.1 ' < /etc/hosts > $TMPHOSTS | |
mv $TMPHOSTS /etc/hosts | |
# Remove old entries | |
grep -vF "$new_ip_address " < /etc/hosts > $TMPHOSTS | |
mv $TMPHOSTS /etc/hosts | |
grep -vF " $new_host_name" < /etc/hosts > $TMPHOSTS | |
mv $TMPHOSTS /etc/hosts | |
if [ ! -z "$old_fqdn" ]; then | |
grep -vF " $old_fqdn" < /etc/hosts > $TMPHOSTS | |
mv $TMPHOSTS /etc/hosts | |
end | |
# Add the our new ip address and name | |
echo "$new_ip_address $new_fqdn $new_host_name" >> /etc/hosts | |
fi | |
# Recreate puppet certs | |
rm -rf /var/lib/puppet/ssl | |
service puppet restart | |
# Restart avahi daemon | |
service avahi-daemon restart | |
fi | |
fi | |
set +x |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
row 39 "end" -> "fi"