Unable to change hostname in Ubuntu 18.04 server on Virtualbox

What I’ve tried:

  • sudo hostnamectl set-hostname ubuntu-1
  • echo ‘ubuntu-1’ > /etc/hostname

Sadly, whenever I reboot the hostname changes back to what it was originally.


This can be solved by changing preserve_hostname to true in /etc/cloud/cloud.cfg

To reiterate:

  1. Set preserve_hostname true in /etc/cloud/cloud.cfg
  2. Update hostnamesudo hostnamectl set-hostname ubuntu-1
  3. reboot