Skip to content

Instantly share code, notes, and snippets.

@nubbel
Last active August 14, 2019 13:26
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 nubbel/275db69dfb268a9796de35eced3c49ff to your computer and use it in GitHub Desktop.
Save nubbel/275db69dfb268a9796de35eced3c49ff to your computer and use it in GitHub Desktop.
#!/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
@rmeh01
Copy link

rmeh01 commented Aug 14, 2019

row 39 "end" -> "fi"

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment