Changing the host name on Ubuntu 18.04

I cloned an Ubuntu 18.04 Server instance post which I wished to change the hostname of the new instance. What was supposed to be a simple operation of updating the hostname in the /etc/hostname and running sudo hostname didn't work as expected. The hostname used to revert on reboot. It took me quite some time to arrive at the right solution. An Ask Ubuntu [answer](https://askubuntu.com/a/1047635/4157) set me in the right direction though it did not work as-is. The reason is the shipment of [cloud-init](https://cloud-init.io/) in Ubuntu 18.04. I got it to work with the following steps -

🔗The Procedure

Step 1

Edit the cloud.cfg file present in /etc/cloud to set preserve_hostname to true.

sudo sed 's/preserve_hostname: false/preserve_hostname: true/' /etc/cloud/cloud.cfg

Step 2

Edit the /etc/hostname file and set the desired hostname in it.

sudo vi /etc/hostname

Step 3

Use hostnamectl to set the hostname. The new name would persist on reboots.

sudo hostnamectl set-hostname <new name>