Persistently Change Hostname on Ubuntu Server

OK, this one drove me nuts because there is some conflicting information out there on the Interwebz.  I installed Ubuntu Server on a Raspberry Pi 4 w/ 8GB of RAM, and when I used Raspberry Pi imager it wouldn’t let me use my preferred hostname because of the inability to use “.”‘s in the hostname setting.  I used a temp hostname and then proceeded to put the image on the SD card.  I booted up and set my networking info via netplan and then changed the hostname using this tried and true, well tested method:
sudo hostnamectl set-hostname hostname

Then proceeded to change the hostname for the 127.0.0.1 entry in the /etc/hosts file using following command:
sudo nano /etc/hosts

I then rebooted the pi and to my disbelief the hostname reverted to what it was when I imaged the card.  After some quick research I got some rather bad info that the solution was simple and all I needed to do was edit the /etc/cloud/cloud.cfg file and change the setting:
preserve_hostname: false

to

preserve_hostname: true

And YES that did work for a while, that is until I did an update to the system and one package in particular warned me that it was installing an updated version of the cloud.cfg file from the maintainer.  I didn’t even think about, I selected yes, and dang it, the hostname reverted back to the temp hostname I had when I originally imaged the SD card.  Editing the cloud.cfg file directly is a bad idea in my opinion.  I am certain Canonical (maintainers of Ubuntu) probably update that file all the time and I don’t want to be playing whack-a-mole with my hostname every time I update the system.  Never fear, here is the solution…

The much better way to do this is to utilize the /etc/cloud/cloud.cfg.d/ folder, in which you can place config files that will overwrite the /etc/cloud/cloud.cfg settings. The files are processed in numerical order with files named with higher numbers executing later in the process (potentially overwriting earlier ones).  I created a file called “99_hostname.cfg” and populated it with the correct directive to override the offending setting in the /etc/cloud.cfg file using the following command (it must be done as root, a normal sudo won’t cut it, switch to root using sudo -i):

rflemen@rmftech:~$ sudo -i
[sudo] password for rflemen:
root@rmftech:~# echo "preserve_hostname: true" > /etc/cloud/cloud.cfg.d/99_hostname.cfg

And BOOM, don’t call it a comeback, my hostname stuck after a reboot (once I changed it again via the tried and true method I mentioned above).  The beauty is, because that config file was created by me, it won’t change. Canonical can update the /etc/cloud.cfg all they want and my script will always overwrite that setting to preserve the hostname.  That being said, this will work as long as Canonical doesn’t change that directive or the process around cloud.cfg.  I think the odds of that happening are low enough where I am not even worrying a bit about it.  Hope this saves someone some frustration, because there are a bunch of peeps out there who take the easy way out (or over engineer the solution). Those solutions may work but ultimately they aren’t the best or most resilient solution to use.

Thanks,
Rob

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply

Your email address will not be published. Required fields are marked *