Header Ads Widget

Responsive Advertisement

Generate unique server names for cloned virtual servers running linux


UPDATE:
With modern cloud providers this is no longer an issue especially when using IaC tooling such as terraform.

One of the great things about using cloud services is that you can launch as many servers as you require all from a single image.  But what happens when you need each server to have it's own unique hostname?

Whilst you could manually change the name on each machine - if you have launched more than a couple then this could take a while, if you've launched or hundreds this becomes an impossible task.

So the solution is to automate it.

The problem can be broken down into a number of tasks:

  1. How to ensure that a name is unique
  2. How to make sure it doesn't change every time a machine reboots
  3. Making it change on machine launch

How to ensure that a name is unique

Initially we thought that we could just generate a set of random letters and use that as the hostname.

MYSTRING=$(cat /dev/urandom | tr -dc 'a-zA-Z0-9' | fold -w 15 | head -n 1)

However one of the key point's to remember when launching virtual images is that if they are launched at the same time then they will have the same default SALT value for the RAND function - this will cause a number of them to have duplicate random names.

One of the unique properties of a machine will be it's ip address so that may be a good place to start
hostname -I will return the ip address as 10.240.0.97 but we can not use '.' as part of a host name
hostname -I |sed 's/\./-/g' will change it to look like 10-240-0-97
to the set the hostname we could
hostname -I |sed 's/\./-/g' > ~/.ThisMachineName
sudo hostname -b -F ~/.ThisMachineName

So this converts the ip adress into a suitable format and then stores it in a hidden file called .ThisMachineName

So lets put it all in a file called ChangeHostname.sh alter to allow us to change the output file name and location, then store it in our home directory.

#!\bin\bash
# Script to set hostname to unique name on startup
# April 2017 Marcus James Adams
#
file="/home/centos/.ThisMachineName"
hostname -I |sed 's/\./-/g' > $file
sudo hostname -b -F $file
hostnamectl set-hostname < $file (NEED TO CHECK THIS BIT WORKS)

Once you have saved the file change it perms to be 700 (check this) so that it can be executed.
$ chmod 711 ~/ChangeHostname.sh

How to make sure it doesn't change every time a machine reboots

If this was a windows machine we could just launch this script from a run once registry key and know that it would only ever execute once.  For now we will cheat and take advantage of the '.ThisMachineName' file that gets created as part of the rename.

NOTE: If you have previous ran or are re-imaging a machine that has this script, make sure that you have deleted '.ThisMachineName' before imaging.

Our revised code now looks like
#!/bin/bash
file="/home/centos/.ThisMachineName"
if [ -f "$file" ]
then
echo "This machine has already had it's hostname updated"
else
hostname -I |sed 's/\./-/g'> $file
sudo hostname -b -F $file
fi

Finally I want a bit extra in my hostname:
  1. I'd like to set a unique identifier at the front of my hostname so i can group all these machines ie "PROJ1"
  2. Just in case there are machines with the same ip (which there may be on multi VPC's/Vracks or containers) I'd like to sufix the ip adress with some random letters
Thats something I'll combeack to later

Making it change on machine launch

Under old versions of centos you would edit etc/rc.local or /etc/rc.d/rc.local and the job will run on startup. However these are no longer executed by default due to systemd-changes.
to still use those, you need to make /etc/rc.d/rc.local executable:

chmod +x /etc/rc.d/rc.local

Now edit /etc/rc.d/rc.local and add the following to the bottom of the file
/home/centos/ChangeHostname.sh

If the script is executable, it should be executed at boot