Search code examples
ubuntu-18.04hostnamereboot

Why does my hostname and /etc/hostname get reset on reboot?


I want to change the hostname of my Ubuntu 18 server.

I tried the usual approach of:

  • sudo hostname new_name - for immediate change
  • Editing /etc/hostname - for persistent change

Then when I reboot, my hostname has changed back. Confusingly, the contents of /etc/hostname has also changed back.

I've modified other files in / and ~, and those changes survive reboot. So it's not a quirky disk issue.


Solution

  • On Ubuntu 18 it seems you must do things differently.

    sudo hostnamectl set-hostname new_name

    Also, if /etc/cloud/cloud.cfg exists, edit it to change preserve_hostname: false to true.