ubuntutorials
Ubuntu 26.04 · 24.04 · 22.04

How to Change the Hostname on Ubuntu

Rename an Ubuntu machine with hostnamectl, keep sudo quiet by fixing /etc/hosts, and stop cloud-init putting the old name back on the next reboot. Tested on 26.04, 24.04 and 22.04.

Verified
Tested end to end on real servers running Ubuntu 26.04 LTS (kernel 7.0.0-27), 24.04.4 LTS (6.8.0-134) and 22.04.5 LTS (5.15.0-185), today.

Type your own and every command below updates. Nothing leaves your browser.

Renaming an Ubuntu machine takes one command. Making the new name survive a reboot on a cloud server takes two more, and those are the ones usually left out.

Before You Start

  • A machine running Ubuntu 26.04, 24.04 or 22.04, logged in as an account with sudo access
  • A name for it. Letters, digits and hyphens are safe. No underscores, no spaces, and nothing starting with a hyphen

Step 1: See the Current Name

hostnamectl
 Static hostname: ubuntu-server
       Icon name: computer-vm
         Chassis: vm
Operating System: Ubuntu 24.04.4 LTS
          Kernel: Linux 6.8.0-134-generic
    Architecture: x86-64

The static hostname is the one kept in /etc/hostname, and it’s the one we’re about to change.

Step 2: Set the New Name

sudo hostnamectl set-hostname yourhostname

That writes /etc/hostname and tells the running system at the same time, which is why it replaced the old advice of editing the file and rebooting.

Your shell prompt carries on showing the old name until you log out and back in, because bash read it once when the session started. Nothing’s gone wrong.

Step 3: Point /etc/hosts at the New Name

sudo sed -i "s/^127\.0\.1\.1.*/127.0.1.1\tyourhostname/" /etc/hosts

Don’t skip this one. sudo resolves the machine’s own name every time it runs, and until this line agrees with the new hostname you get a warning in front of every single command:

sudo: unable to resolve host web01: Name or service not known

The command still runs. It just says that first, every time, until you fix it.

cat /etc/hosts
# Your system has configured 'manage_etc_hosts' as True.
# ...
127.0.1.1	yourhostname
127.0.0.1 localhost

# The following lines are desirable for IPv6 capable hosts
::1 localhost ip6-localhost ip6-loopback

127.0.1.1 is a Debian and Ubuntu convention for the machine’s own name, kept apart from 127.0.0.1 so that localhost stays meaning localhost.

Step 4: Stop cloud-init Putting It Back

Skip this on a desktop or a machine you installed yourself. On anything rented from a cloud provider it’s the step that matters, because cloud-init sets the hostname from the provider’s metadata on every boot, and your new name lasts exactly until the next restart.

sudo sed -i 's/^preserve_hostname: false$/preserve_hostname: true/' /etc/cloud/cloud.cfg
grep preserve_hostname /etc/cloud/cloud.cfg
preserve_hostname: true

Cloud images generate /etc/hosts from a template on each boot too, which is why that file opens with a comment telling you so. Once the hostname is preserved, the generated file follows it, and step 3 is what keeps things quiet in the meantime.

Verify It Worked

A hostname that’s only set in memory looks exactly like one that’s set properly, right up until the machine restarts. So restart it:

sudo reboot

Give it a minute, log back in, and ask again:

[ "$(hostname)" = "yourhostname" ] && echo "the new name survived the reboot"
the new name survived the reboot

If that prints nothing, the old name came back, and on a cloud server step 4 is where to look.

The other half is that the name still resolves, which is what stops sudo complaining:

getent hosts yourhostname
127.0.1.1       yourhostname

An address back means the lookup works. Nothing back means /etc/hosts lost the entry during the reboot.

Conclusion

The machine now calls itself what you told it to in its prompt, its logs and its own name lookups, and it’ll still be doing so after the next kernel update reboots it. If you’re setting a box up from scratch, adding a user and giving them sudo is the natural next job, and if you inherited it from somebody else, checking which Ubuntu release it’s actually running is worth thirty seconds first.

If the name reverts on your provider despite step 4, do drop me a line at [email protected] with the Ubuntu version and who you’re hosting with, and I’ll get it tested.