Change hostname Ubuntu 22.04

Most guides will suggest one of the following ways to change the hostname:

  • hostnamectl
  • hostname
  • /etc/hosts

You’ll find that if you’re running cloud-init, that none of these methods persist the hostname after a reboot. In order to persist the hostname change when using cloud-init, you have to edit the following file:

nano /etc/cloud/cloud.cfg

Set preserve_hostname to true as follows:

preserve_hostname: true

Save this file and run hostnamectl as follows:

hostnamectl set-hostname your-hostname

Now the hostname you set will be preserved after a reboot.