Most guides will suggest one of the following ways to change the hostname:
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:
Set preserve_hostname to true as follows:
Save this file and run hostnamectl as follows:
hostnamectl set-hostname your-hostname
Now the hostname you set will be preserved after a reboot.