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
🔗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>