Automatic/Dynamic configuration of hostnames for rolling out labs

Posted by daemon on Wed 11 Oct 2006 at 11:20

Recently I was given the task of rolling out a number of PCs running Linux for a student lab. The roll out isn't complete yet, but I thought that this trick was so nice for lab based environments that I'd use it to try my hand at a debian-administration article.

First a bit of background though. All hosts allowed network access have have their MAC addresses registered, and are provided with "pseudo-static" IP addresses using DHCP reservations. This is a pretty simple way of trying to control network access in a university environment and works pretty well. Staff PC's are registered as and when they are setup/configured for the staff member, and the staff member can choose what they want their box to be called.

Student lab machines however, are a different story because we really want each machine to have a unique name (obviously) that follows a set pattern, usually something along the lines of "labname###.example.ac.za" where "labname" is the name of the lab, or lab complex, and "###" is the last octet of their IP address -- so a PC in the undergrad lab with the ip address 192.0.2.181 would have the hostname "UG181.example.ac.za".

In a scenario where PCs are imaged fairly regularly, this can cause issues where the DHCP reservations hostname doesn't match what the PC thinks it's called. This is where I found myself recently, and below is how I fixed it. As always, I'm sure that there are other, possibly better, ways to do the same thing, but this worked well for me.

Pre-requisites:

There are a few packages that will be needed to be installed on the PC (and in our case, included in the disk image for roll out). Some of these are probably included with a base installation, but just to be on the safe side, run this command:

# apt-get install dhcp3-client hostname bind9-host coreutils

The script

As a DHCP client, the ISC's dhclient is fairly robust, and provides a nice framework to tinker with in the form of entry and exit hook scripts. Reading `man 8 dhclient-script` should enlighten you to the basic capabilities that dhclient has, but debian has extended dhclient so that you can have a collection of simple scripts in a couple of directories, rather than have to maintain a pair of monolithic scripts. In true debian fashion, these are named similarly the original scripts, but with a ".d" appended. These directories can be found under /etc/dhcp3/ and are called from within /etc/dhcp3/dhclient-script, which passes various variables onto the hook scripts, such as $interface and $new_ip_address

My solution involved checking DHCP's idea of what the hostname should be, and applying that to the system, and here it is in all it's glory:

#!/bin/sh

# Filename:     /etc/dhcp3/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.
# Depends:      dhcp3-client (should be in the base install)
#               hostname (for hostname, again, should be in the base)
#               bind9-host (for host)
#               coreutils (for cut and echo)
#

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)

echo $hostname > /etc/hostname

hostname $hostname

echo dhclient-exit-hooks.d/hostname: Dynamic Hostname = $hostname

# And that _should_ just about do it...

Like I said near the top, there may be other, better, ways of doing this, but in our lab environment this seems to work very nicely. Once I have the roll-out complete, I hope to have at least one more article, but I'm not promising anything ;-)


This article can be found online at the Debian Administration website at the following bookmarkable URL:

This article is copyright 2006 daemon - please ask for permission to republish or translate.