Set hostnames from dhcp-server with dhclient on *buntu systems

From Rsewiki
Jump to: navigation, search

To make *buntu based systems set the hostname supplied by the dhcp-server use the following script named hostname

#!/bin/sh
# Filename:     /etc/dhcp/dhclient-exit-hooks.d/hostname
# Purpose:      Used by dhclient-script to set the hostname of the system
#               to match the DNS information for the host as provided by
#               DHCP.
#

if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
        return
fi

echo dhclient-exit-hooks.d/hostname: Dynamic IP address = $new_ip_address
hostname=$(host $new_ip_address | cut -d ' ' -f 5 | cut -d '.' -f 1)
echo $hostname > /etc/hostname
hostname $hostname
echo dhclient-exit-hooks.d/hostname: Dynamic Hostname = $hostname

The script should be put in the folder /etc/dhcp/dhclient-exit-hooks.d

Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox