Solution: Can't Change Hostname on Ubuntu 18.04 LTS


I’m just posting this here in case someone ended up in the same boat as me.

I cloned a VM on VMware ESXi 6.5.
Tried changing the hostname using:

hostname newname

…and changing the hostname in /etc/hostname, but when I rebooted, the hostname went back to the old original VM name.

Tried this as well…

hostnamectl set-hostname newname

But that didn’t persist either.

What worked was this:

apt remove cloud-init cloud-initramfs-copymods cloud-initramfs-dyn-netconf

Turned out that these cloud features were jacking with the hostname. Removing this resolved the problem.

Maybe this is taking a sledgehammer to the solution, but it worked.

A more elegant resolution is to locate the setting in the cloud config that causes the cloud hostname to override the local hostname.

Also, this had nothing to do with VMware. The original VM needs to be updated so it doesn’t have this cloud junk in it so this problem will stay dead and not return like the ending of a cheesy horror show.


1 Like

yes, usually, I add the following in my user-data before creating a new VM for all cloud images (RHEL example, repalce yum with apt/apt-get etc):

# Remove cloud-init 
  - systemctl stop network && systemctl start network
  - yum -y remove cloud-init