Changing hostname on RaspberryPi

There seem to be lots of posts about changing your hostname on a RaspberryPi. The issue is that “something” keeps overriding the changes for you. This thing seems to be cloud-init, which appears to be installed by default now.

Digging about I found people turning off and removing cloud-init. I also found people trying to lean into cloud-init – which also attempted but it always reverted my change.

What I eventually found is that the user-data file in /boot/firmware doesn’t seem to be read correctly on boot. The system appears to read a cached version which is stored in /var/lib/cloud/instances.

My solution? I told cloud-init to purge its cache:

cloud-init -c

And then I rebooted the device. The change I was attempting to make appeared straight after the reboot. I have no idea if there is a better way of having the instance file refresh – not gone that far down the cloud-init rabbithole. But I wanted to get my method out there, as it might be a bit simpler.

FWIW, what I was attempting was to get the fully qualified domain name correctly reflected in the system. I did this by adding the following to the user-data file:

fqdn: <full name of the device>

Now when I run hostname -f, I get the correct response.

There you go – a different approach to changing a RaspberryPi hostname.