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 <new 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 set me in the right direction though it did not work as-is. The reason is the shipment of cloud-init 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>
Comments
Post a Comment