Thursday, 6 December 2012

Virtual hostnames with Debian and dhcp

At $dayjob we have a large number of Linux Virtual Machines runnining on Hyper-V.

Most of these run Scientific Linux / Scientific Linux CERN, but we have a requirement to run debian hosts too.

Some observations that may help others:

Linux Integration Components
Even the latest MS ones (3.4 at last count) don't support debian / ubuntu. Hoever if you're running squeeze (6.0.x) then there's a 3.2 backport kernel available in debian backports that seems to work fine

Templates / DHCP hostnames
I noticed that our debian template host wasn't setting the hostname (assigned via dhcp) so using the example at http://nullcore.wordpress.com/2011/12/09/setting-the-system-hostname-from-dhcp-in-ubuntu-11-10/ I worked on a similar script that doesn't end up with a trailing period in the FQDN and appends to /etc/hosts if needed

lo:<pre>

#!/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.

# Based on http://nullcore.wordpress.com/2011/12/09/setting-the-system-hostname-from-dhcp-in-ubuntu-11-10/

# Do not update hostname for virtual machine IP assignments
if [ "$interface" != "eth0" ]
then
    return
fi

if [ "$reason" != BOUND ] && [ "$reason" != RENEW ] \
   && [ "$reason" != REBIND ] && [ "$reason" != REBOOT ]
then
        return
fi
hostname=${new_host_name}.${new_domain_name}

#echo dhclient-exit-hooks.d/hostname: Dynamic Hostname = $hostname
#echo dhclient-exit-hooks.d/hostname: Dynamic IP address = $new_ip_address

echo $hostname > /etc/hostname
/etc/init.d/hostname.sh

# and append to hosts
grep -q $hostname /etc/hosts
if [ $? -eq 1 ] ; then
     echo "$new_ip_address $hostname $new_host_name" >> /etc/hosts
fi
</pre>

Word of mouth Skye History

Many years ago we lived in the Old Manse in Waternish, Skye. If you look on the maps, you'll spot that unlike nearly all the other house...