How to make renaming of a FreeBSD server easier

I have always felt that renaming FreeBSD was too difficult. I posted about this on the FreeBSD forums, but the response was less than enthusiastic. http://forums.freebsd.org/showthread.php?t=3477

A few people said that servers don’t get renamed often, however, they only cited two companies. I just so happen to have worked as a Network Support Engineer and a Product Support Engineer for almost ten years, working with multiple companies a day. Getting asked how to rename a server or appliance was question that occurred multiple times a month. So many administrators asked how to rename a LANDesk Management Suite server that we had to created a community article that was quite popular and deflected many calls. Unfortunately a LANDesk Management Suite server could not be renamed and administrators were quite disappointed.

I use FreeBSD on VMWare all the time and I am constantly renaming my systems, because I use VMWare and clone one and then have to rename the clone.

Use cases for making renaming of FreeBSD easier

Here are some use cases for making the ability to rename a system easy.

Use case #1 – Imaging or drive cloning

Yes most corporations build a server once and clone it.

They purchase 100 identical hardware servers. They build one server to perfection. They then capture the drive (using and imaging utility).

Then they want to deploy the image and quickly change the name and IP address for the other ninety-nine servers.

NOTE: Companies such as mine (LANDesk) and other Computer Management companies have made the imaging process very slick and have the process automatically give the computers the correct name. We lay down the image, then mount the partition, then take steps to make the computer boot the first time with the correct new name. This is a feature not easily done in most *nix operating systems.

Use Case 2 – Providing an Appliance

If you create an appliance and want to sell it, then I guess this is similar to use case 1. Every client is going to get an appliance that has to be renamed for their company.

Use Case 3 – New IT naming convention

You now have to comply with a standard naming convention for your servers or you had one and it just changed.

Use Case 4 – Two companies merge and they both have similar server names

If one company buys another company and they merge their networks, they may have similar server names. For example, maybe they both have a FreeBSD server and they both cleverly named it “FreeBSD”.

A script to change the hostname on FreeBSD

In order to make it faster to rename a FreeBSD server, I wrote this script.

#!/bin/sh

# Debugging
# debug=true

# Declare functions
checkParams()
{
	if [ $1 -ne 1 ]; then
		showSyntax
	fi
}

showSyntax()
{
		echo 'Invalid syntax.'
		echo '  Usage:'
		echo '  '$0' server.name.com'
		exit 1
}

updateHosts ()
{
	file=/etc/hosts
	if [ $debug ]; then
		echo Updating $file file...
	fi

	fqdn=$1
	hostname=`echo $1 | cut -d . -f 1`

	echo -e "127.0.0.1\t$fqdn $hostname" > /tmp/hosts
	echo -e "127.0.0.1\t$fqdn $hostname" >> /tmp/hosts
	cat $file | grep -v "::1" | grep -v "127.0.0.1" | grep -v "#" >> /tmp/hosts
	mv -f /tmp/hosts $file

	if [ $debug ]; then
		echo Finished updating $file file...
	fi
}

updateRcConf ()
{
	file=/etc/rc.conf
	if [ $debug ]; then
		echo Updating $file file...
	fi

	echo hostname=$1 > /tmp/rc.conf
	cat $file | grep -v "hostname" >> /tmp/rc.conf
	mv -f /tmp/rc.conf $file

	if [ $debug ]; then
		echo Finished updating $file file...
	fi
}

updateApacheConf ()
{
	file=/usr/local/etc/apache*/httpd.conf
	if [ $debug ]; then
		echo Updating $file file...
	fi

	cp -f $file /tmp/httpd.conf
	sed -i .bak "s/ServerName[^:]*/ServerName $1/" /tmp/httpd.conf
	mv -f /tmp/httpd.conf $file

	if [ $debug ]; then
		echo Finished updating $file file...
	fi
}

# Run
checkParams $# $1
updateHosts $1
updateRcConf $1
updateApacheConf $1

Ideas for continually improving the renaming process

The hostname command could have a switch to try to rename the box permanently. It changes the hosts file, the rc.conf, and calls the port scripts in question.  The port scripts could be handled in either of the following ways.
  1. A folder could be created called /usr/local/etc/rename.d and each port could over the course of the next few years start building a script that is able to edit their configuration files and put i the correct name.
  2. Each port in their startup script, /usr/local/etc/rc.d/appscript.sh, could have a rename switch.
It won’t be simple, and it will take years for different ports to start supporting such a feature, but it could work for FreeBSD or Linux quite easily.

Leave a Reply

How to post code in comments?