Setting the hostname on an Ubuntu linux installation is pretty easy during the installation. However, for a standard image to be dropped onto a wide range of machines, it really needs to be unique and set via a script.
We like to use the serial number of the PC in question, though this is more or less practical depending on the brand of PC. Acer, for example, tend to have VERY long serial numbers – it may be unique, but its going to be harder to type than just an IP address. IBM tend to use reasonable 7 digit strings, which is is much more usable. Other brands can be longer or shorter.
Setting hostname itself seems easy – just run
hostname <newname>
(where sudo is used to run the command as root) in a terminal, and the hostname of the machine will change. You’d think we were done, but unfortunately not! This will only change the hostname until the next reboot. On startup, the contents of the file
/etc/hostname
is used to set the hostname. To update this, I like to set the hostname for the current sessions, then run
hostname > /etc/hostname
(as root) which willoverwrite the file with the current hostname. If running as part of a script on startup, this will set the hostname now, then update the file for future restarts. However, we still aren’t done. We need to update another file
/etc/hosts
with the details of the name for networking purposes. We need to add the hostname, and any domain name aliases too. The hosts file will probably look something like:
127.0.0.1 localhost localhost.domain.local
127.0.1.1 <hostname>
Update the hostname line, and add new aliases for the hostname for any domains that may be relevant. At this point, the system is renamed! However, this is all still pretty manual – ideally we need to script the process.
Now, to get the serial number, we can query the bios using the dmidecode command, and then process it usign the myriad of linux text handling commands.
dmidecode | grep “Serial Number” | head -n1 | sed -e ‘s/\tSerial Number: //g’
(as root, again) should return the serial number from the bios! We can combine this with the hostname command, as follows:
hostname $(dmidecode | grep “Serial Number” | head -n1 | sed -e ‘s/\tSerial Number: //g’)
(once again, as root.) This will set the hostname to the serial number.
I actually combine all of the commands discussed to form a single script -
hostname $(dmidecode | grep “Serial Number” | head -n1 | sed -e ‘s/\tSerial Number: //g’)
hostname > /etc/hostname
echo “127.0.0.1 localhost localhost.domain.local” > /etc/hosts.new
echo “127.0.1.1 ”$hostname” “$hostname”.domain.local” >> /etc/hosts.new
mv /etc/hosts.new .etc/hosts
This sets the hostname, then updates the hostname file. It then generates a complete hosts file line by line, and overwrites the old version. There are probably better ways of updating the text file directly, but this works effectively enough.
Finally, I set this script file (which I named hostname.sh) to run on system startup. Simply copy the file to
/etc/init.d
and run (as root)
update-rc.d hostname.sh defaults
where hostname.sh is the chosen name for your script. This will add the script to the startup scripts on the machine, where it will automatically be run as root.
Recent Comments