Ah, the old days. Back when one could change the hostname of an Ubuntu server simply by editing the /etc/hostname file. That was way back with Ubuntu 16.04, when things were much simpler. Now, in the name of evolution, Ubuntu has changed a number of configurations admins have been accustomed to for years. This can be very frustrating, especially when one depends upon Ubuntu in a data center. You’ve been using the platform for years and, out of nowhere, numerous changes to how the operating system is administered are rolled out. One such change (that was fairly drastic in both form and function) was how to manually set a static IP address.

SEE: How to configure a static IP address in Ubuntu Server 18.04 (TechRepublic)

The changes don’t stop there. In fact, another very important configuration in Ubuntu has undergone a bit of an evolution. That change is the hostname. Now, before you lose your mind wondering why, this particular change isn’t nearly as dramatic as is the IP Address. In fact, the configuration of the hostname in Ubuntu 18.04 is, on the surface, the same. However, if you don’t take care of one very important issue, that change you’ve made to the hostname will automatically revert, the second you reboot that server. I discovered this when attempting to setup a server with the latest release of Ubuntu Server. No matter how many times I altered /etc/hostname, the change reverted upon reboot. After much hair pulling, I finally discovered the culprit.

I’m here to help you prevent hair loss.

Configuring the hostname

First, let’s configure the hostname. If you issue the command hostname, you can see the hostname of your server (Figure A).

Figure A

To change that hostname, open and edit the necessary file with the command sudo nano /etc/hostname. Change the hostname to meet your needs and then save/close the file.

Next open the hosts file with the command sudo nano /etc/host. Where you see your old hostname (i.e. localhost), change it to the new name. Save and close that file. Now, reboot your machine.

That was a test.

When the machine reboots, you should see your hostname has reverted to the original. Why is that? Because there’s a little setting, called preserve_hostname, tucked away in a particular configuration file, that dictates to the platform to either preserve or not preserve the hostname as set in /etc/hostname. To make that change, go back and alter your hostname (in both /etc/hostname and /etc/hosts) and then issue the command:

sudo nano /etc/cloud/cloud.cfg

In that file, look for the entry:

preserve_hostname: no

Change that entry to:

preserve_hostname: yes

Save and close that file. Now your newly configured hostname will remain, even after a reboot.

A small but frustrating change

Until you know about the preserve_hostname option, this change to the configuration of the Ubuntu Server hostname will certainly be one of frustration. I would suggest to Canonical, whenever such a change is made, it might behoove them to give some sort of indication in the standard configuration file. For example, in /etc/hostname it might have been smart to add this line:

# In order to preserve an altered hostname, set preserve_hostname to yes in
# /etc/cloud/cloud.cfg

Once the next LTS release comes out, that comment could be removed. It’d be a simple direction that would go a long way to prevent admins from growing frustrated with the implemented change.