Do you use let's encrypt?





8441 votes ~ 27 comments

 

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
  • dhcp3-client: provides the dhclient(8) framework for client side DHCP lease management, including various "entry" and "exit" script hooks. It should be part of a base installation.
  • hostname: provides /bin/hostname(1) which is used for querying and manipulating the hosts name. Again, this should already be part of a base installation.
  • bind9-host: this provides /usr/bin/host(1) which is used for simple DNS queries.
  • coreutils: needed in this case for the cut(1) and echo(1) commands. I'm not sure if this is included in a base install, but it might well be.

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 ;-)

 

 


Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by Anonymous (85.104.xx.xx) on Fri 13 Oct 2006 at 18:15
We used a simpler way to achive the same effect. Simply remove the lab machines hostname (put an empty /etc/hostname file). This way when the dhcp client starts it can set the hostname to a value that it received from dhcp server. For more detail see: /sbin/dhclient-script (the set_hostname function)

[ Parent | Reply to this comment ]

Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by daemon (155.232.xx.xx) on Fri 13 Oct 2006 at 22:08
[ View Weblogs ]
I thought about using it this way, but it's not that rare for our PC's get moved around, re-assigned (and thus re-registered). This script handles "dynamic" changes like this, whereas, to my understanding, the set_hostname function in `/etc/dhclient-script` would be more of a "once-off" thing (only being used if /etc/hostname is missing/blank).

Thanks for the tip though.

[ Parent | Reply to this comment ]

Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by Anonymous (67.185.xx.xx) on Wed 18 Oct 2006 at 21:22
why not just put a shutdown script that blanks out /etc/hostname on shutdown or reboot? Then if you reregister the MAC address and move the machine it'll pull a new hostname on every boot? I suppose its a bit of a burden to pull a hostname on every boot, but it can't be that much...

.02

[ Parent | Reply to this comment ]

Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by daemon (155.232.xx.xx) on Wed 18 Oct 2006 at 22:47
[ View Weblogs ]
Like I said in the article, there are probably many ways to do what I described. I just fomred the idea while flicking through some random manpages and it seemed quite elegant and solved an minor issue I'd been pondering for a while. So I thought I'd share it.

Your suggestion would ultimately provide the same outcome, but I liked the idea of leveraging my new found knowledge ;-)

As for pulling a hostname at boot time being a burden, I doubt that's really that much -- it's a small DNS lookup (and if DNS is down, we have bigger problems ;-), and the boxes don't really go down that often except for the occasional power outage...

Cheers.

[ Parent | Reply to this comment ]

Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by Anonymous (82.151.xx.xx) on Thu 5 Jul 2007 at 07:39
Replace

hostname=$(host $new_ip_address | cut -d ' ' -f 5)

with

hostname=$(host $new_ip_address | cut -d ' ' -f 5 | sed -e 's/\.$//')

because $(host ...) return host name with dot at the end, and this is incorrent name for host (command hostname didn't understant it)

[ Parent | Reply to this comment ]

Re: Automatic/Dynamic configuration of hostnames for rolling out labs
Posted by daemon (155.232.xx.xx) on Thu 5 Jul 2007 at 21:26
[ View Weblogs ]

Thanks for the patch.

I actually stumbled across this soon after writing the article, but never got around to updating it, so I'm glad it's now documented here.

Cheers.

[ Parent | Reply to this comment ]