Ubuntu: Why can't I persistently change the hostname of my VM?

When you try to change the host name of your VM by editing /etc/hosts, you will notice that the hostname is reset on the next reboot. This happens because the VM is getting its configuration data via the cloud-init process.

In this case you can disable 'manage_etc_hosts' by editing /etc/cloud/cloud.cfg.d/10_etc_hosts.cfg and setting 'manage_etc_hosts: false'

Also setting 'preserve_hostname: true' in the /etc/cloud/cloud.cfg allows you to set the hostname to whatever you want.

Alternatively you can do all this when you first create the instance, by passing cloud-init parameters in the 'Configuration' → 'Customization Script' as shown below: