You can change an Ubuntu hostname with a single command. There are two things to watch for: /etc/hosts still points to the old name until you update it, and on cloud servers cloud-init may put the old name back after a reboot.
This guide covers the quick way, the permanent way, the GUI way, and the two problems that catch most people. It works on Ubuntu 20.04, 22.04, and 24.04.
Table of Contents
TL;DR
sudo hostnamectl set-hostname web01
sudo sed -i 's/^127.0.1.1.*/127.0.1.1 web01/' /etc/hosts
exec bash
The rest of this guide explains each step and what to do if something goes wrong.
Check the Current Hostname
Start by seeing what you have now:
hostnamectl
Example output:
Static hostname: ubuntu-server
Icon name: computer-vm
Chassis: vm
Machine ID: 3f2a9c...
Boot ID: 81d4e7...
Virtualization: kvm
Operating System: Ubuntu 24.04.1 LTS
Kernel: Linux 6.8.0-45-generic
Architecture: x86-64
If you just want the name itself:
hostname
Three Kinds of Hostname
systemd keeps track of three hostnames. Most of the time you only care about the first one.
| Type | Where it lives | What it’s for |
|---|---|---|
| Static | /etc/hostname | The real hostname. Survives reboots. |
| Transient | Kernel memory | What the system is using right now. It’s usually the same as the static name. DHCP or a manual hostname command can change it. |
| Pretty | /etc/machine-info | A friendly label like “Tom’s Laptop”. It can contain spaces and capital letters. Mostly used by desktops. |
When people say “change the hostname,” they mean the static one.
Method 1: Change the Hostname with hostnamectl (Recommended)
This is the standard way on any Ubuntu release that uses systemd.
sudo hostnamectl set-hostname web01
That one command writes the new name to /etc/hostname and applies it right away. You don’t need to reboot.
See also: Mastering the Linux Command Line — Your Complete Linux Training Course
Check it:
hostnamectl
Static hostname: web01
...
Your shell prompt still shows the old name, because the prompt was built when the session started. Open a new terminal, or reload the shell:
exec bash
Now your prompt should read user@web01.
Optional: Set a Pretty Hostname
On a desktop, you might want a friendly display name as well:
sudo hostnamectl set-hostname "Tom's Dev Laptop" --pretty
The static hostname stays web01. The pretty name only shows up in places like the Settings app.
Step 2: Update /etc/hosts (Don’t Skip This)
This is the step most people forget.
Ubuntu maps the machine’s own name to 127.0.1.1 in /etc/hosts. After you change the hostname, that line still points to the old name. Fixing it keeps hostname -f accurate and gives any program that looks up the local hostname the right answer.
You may also have seen this warning:
sudo: unable to resolve host web01: Name or service not known
Here’s why it happens. Every time sudo runs, it looks up the machine’s own hostname. It needs that name to match the host field in /etc/sudoers rules, such as alice web01=(ALL) ALL, and to write its logs. If the lookup fails, sudo prints this warning. The command still runs. If your DNS server doesn’t respond, sudo may also pause until the lookup times out.
On a default Ubuntu install, systemd-resolved usually answers lookups for the local hostname itself, so you may never see this warning. It shows up more often in containers, WSL, minimal images, or anywhere systemd-resolved isn’t handling name lookups. Either way, updating /etc/hosts makes the problem go away.
Open the file:
sudo nano /etc/hosts
Find the line with the old name:
127.0.0.1 localhost
127.0.1.1 ubuntu-server
Change it to the new name:
127.0.0.1 localhost
127.0.1.1 web01
If the machine has a fully qualified domain name, put the FQDN first and the short name after it:
127.0.1.1 web01.example.com web01
Save the file and test it:
hostname -f
ping -c 1 web01
hostname -f should print the FQDN, or the short name if you didn’t set one. The ping should get a reply from 127.0.1.1.
Method 2: Edit /etc/hostname Directly
If you prefer editing files, you can change /etc/hostname yourself:
echo "web01" | sudo tee /etc/hostname
This file only gets read at boot, so the change doesn’t apply until you reboot or run:
sudo hostname web01
You still need to update /etc/hosts as in Step 2. In practice, hostnamectl does all of this in one go, so this method only makes sense in minimal environments or scripts where hostnamectl isn’t available.
Method 3: Temporary Change with the hostname Command
Need a different name just for a test, without keeping it?
sudo hostname test-box
This only changes the transient hostname. As soon as you reboot, the name in /etc/hostname comes back.
Method 4: Change the Hostname from the Ubuntu Desktop
If you’d rather not use the terminal:
- Open Settings.
- Go to About. On Ubuntu 24.04 it’s under System → About.
- Click Device Name.
- Type the new name and confirm.
This sets the hostname through the same systemd service that hostnamectl uses. You should still update /etc/hosts as shown in Step 2.
Why Your Hostname Resets After Reboot (Cloud Servers)
You changed the hostname on an AWS, Azure, DigitalOcean, or other cloud instance. It worked. Then you rebooted, and the old name came back.
The cause is cloud-init. Ubuntu cloud images run it on every boot, and by default it sets the hostname from the cloud provider’s metadata. Whatever you set by hand gets overwritten.
To stop this, tell cloud-init to keep your hostname. Instead of editing the main config file, create a small drop-in file:
echo "preserve_hostname: true" | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
Then set the hostname as usual:
sudo hostnamectl set-hostname web01
Reboot to confirm it sticks:
sudo reboot
After the reboot, hostnamectl should still show web01.
Some images also let cloud-init rewrite /etc/hosts. If your edits there keep disappearing, check whether manage_etc_hosts is set:
grep -r manage_etc_hosts /etc/cloud/
If it’s set to true, cloud-init generates /etc/hosts from a template at /etc/cloud/templates/hosts.debian.tmpl. Either edit that template or set manage_etc_hosts: false in a drop-in file.
Hostname Naming Rules
Linux will accept some odd names, but other systems and network tools may not. These rules keep you out of trouble:
- Use lowercase letters (
a-z), digits (0-9), and hyphens (-). - Don’t start or end the name with a hyphen.
- Keep each part (label) to 63 characters or fewer.
- Don’t use underscores, spaces, or dots in the short name.
Good: web01, db-primary, k8s-node-3 Avoid: Web_Server, my server, -backup
After the Change: What Else Might Need Updating
Changing the hostname updates the system, but some software saved the old name in its own config:
- Postfix may have
myhostnameset in/etc/postfix/main.cf. - Monitoring agents (Zabbix, Prometheus node labels, Datadog) may report under the old name until you restart them.
- SSH known_hosts on other machines are keyed by hostname or IP. Clients that connect by the new name will see it as a new host the first time.
- Kubernetes nodes register with the cluster under their hostname. Renaming a node that’s already in a cluster isn’t something to do casually. Drain the node, remove it, and rejoin it instead.
Restart services that cache the hostname, or just reboot during a maintenance window.
Quick Troubleshooting
sudo: unable to resolve host The system can’t look up its own new name. Add it to the 127.0.1.1 line in /etc/hosts.
Hostname reverts after reboot cloud-init is overwriting it. Add preserve_hostname: true in /etc/cloud/cloud.cfg.d/.
Prompt still shows the old name The shell hasn’t reloaded. Run exec bash or open a new terminal.
hostname -f prints only the short name Add the FQDN to the 127.0.1.1 line in /etc/hosts, before the short name.
Could not set property: Access denied You ran hostnamectl without sudo.
Summary
On most Ubuntu machines, changing the hostname takes two commands and one file edit:
sudo hostnamectl set-hostname web01
sudo nano /etc/hosts # update the 127.0.1.1 line
On a cloud server, add one more step so cloud-init doesn’t undo it:
echo "preserve_hostname: true" | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
That covers it. If you’re still getting used to the Ubuntu terminal, our Linux command line training guide walks through the basics step by step.


