Renaming your Ubuntu machine can cause problems

Changing the hostname on Ubuntu is straightforward. You can do it through the GUI (Settings → System → About → Device Name)1: hostname-change or from the command line:

hostnamectl set-hostname newname

But there’s a catch. Both the GUI and hostnamectl update the static hostname (/etc/hostname), but they don’t touch the /etc/hosts file. A typical /etc/hosts entry looks like this:

127.0.1.1 myhostname.mydomain myhostname

The hostname maps to 127.0.1.1 so the system can resolve its own name locally.

However, when you change the hostname, this line is left unchanged and the new hostname no longer resolves to anything. While some applications won’t care, others may break in surprising ways.

I’ve personally experienced cases where this inconsistency caused problems. For example, a hostname change combined with a VPN kill-switch, caused the sudo command to hang for minutes, see my other post on this.

In another case, I changed the hostname mid-session, which caused my vim editor to throw an error: _IceTransSocketUNIXConnect: Cannot connect to non-local host myhostname everytime I used it in that session. This happened because hostnamectl doesn’t automatically restart running applications or re-initialize the user session. As a result, my $SESSION_MANAGER environment variable was still pointing to the old, now invalid, hostname. Logging out and logging back in fixed it, as this forced a clean start for the entire user session.

Conclusion

While changing a hostname on Ubuntu seems simple, the process is incomplete by design. hostnamectl and the GUI only update the static hostname, leaving the /etc/hosts file unchanged and running applications that depend on it misconfigure. This can cause subtle, difficult-to-diagnose problems.

A user-friendly operating system like Ubuntu shouldn’t create these hidden inconsistencies. The ideal solution is for the system to perform a single, atomic operation, changing both /etc/hostname and /etc/hosts at once to maintain a consistent state.

Until that’s implemented, the responsibility falls on the user. So after changing the hostname, remember to manually update /etc/hosts and reboot or log out and back in.


  1. Note that it will slugify your input: spaces, underscores, and most punctuation are replaced with dashes, and uppercase letters are lowercased to form the static hostname. For example, My fast_PC! becomes my-fast-pc, while the original is kept as the pretty hostname (hostnamectl --pretty). ↩︎