Ubuntu: Change the Hostname

The Short Version

sudo hostnamectl set-hostname pi-enviro

That is the modern, correct command. It writes /etc/hostname and notifies systemd, which is more than the older echo > /etc/hostname approach does.

You still need to fix /etc/hosts, and that is where the usual snippet goes wrong.

A photorealistic shot of a laptop showing a terminal prompt with a newly changed hostname, a Raspberry Pi beside it on a desk, warm lamp light, realistic photography, no legible text.

Why hostnamectl Rather Than Editing The File

The old advice is:

echo "pi-enviro" | sudo tee /etc/hostname

That sets the file and nothing else. The kernel's hostname stays as it was until a reboot, and systemd's view of the machine identity (/etc/machine-info) is untouched. hostnamectl does both, and it survives reboots because systemd reads the file at start rather than applying a temporary change.

sudo hostnamectl set-hostname pi-enviro
hostnamectl status

status confirms the static hostname, and shows whether a transient one is still in effect. If the output still shows the old name in the "Static hostname" line, the command did not run as root.

Fixing /etc/hosts, Properly

The common snippet is:

echo "127.0.1.1   $newhostname" | sudo tee -a /etc/hosts

-a appends. Run it twice and you have two conflicting entries for 127.0.1.1, which produces confusing behaviour in anything that resolves the local hostname — sudo slowdowns, Apache refusing to start with "could not reliably determine the server's fully qualified domain name", and mail servers rejecting HELO.

Do it with a replacement instead:

OLD=$(hostname)
sudo sed -i "s/^127\.0\.1\.1.*/127.0.1.1    $NEWHOSTNAME/" /etc/hosts

Or explicitly, if you have not changed anything yet:

sudo sed -i 's/^127\.0\.1\.1.*/127.0.1.1    pi-enviro/' /etc/hosts

Then check the file:

cat /etc/hosts

You want exactly one 127.0.1.1 line, carrying the new name.

A note on 127.0.1.1 versus 127.0.0.1: Debian and Ubuntu deliberately use 127.0.1.1 for the machine's own name so that localhost and the hostname are distinct. Do not "simplify" it by pointing your hostname at 127.0.0.1 — that works until something needs both to be different.

Verify Without Rebooting

hostname          # should print the new name in this session
hostnamectl       # static hostname
cat /etc/hostname
cat /etc/hosts

The current shell prompt will keep the old name until you start a new session — the prompt is generated at login. That is cosmetic, not a failure.

Then reboot once, for the sake of certainty:

sudo reboot

After the reboot, hostname should return the new name before you do anything else.

A photorealistic close-up of a terminal window showing the contents of an /etc/hosts file with a single 127.0.1.1 entry, a notebook with the hostname written on it, desk lamp light, realistic photography, no legible text.

A photorealistic shot of a terminal showing a hostname verification command and its output, a second window with an /etc/hosts file open, cool screen light in a dim room, realistic photography, no legible text.

Things That Break Afterwards

Anything with the old hostname hardcoded. Apache's ServerName, mail server configs (myhostname in Postfix), a monitoring agent's registration, a backup script writing to a path derived from the hostname. Grep for it:

sudo grep -r "$OLD" /etc --include="*.conf" -l

SSH known_hosts warnings. Your own client will complain that the hostname's key has changed if you connect using the new name and an old entry exists, or vice versa. That is expected and it is not a security incident — remove the stale line.

DHCP leases and DNS. If you are changing the hostname to match a DNS record, or you rely on DHCP to register the name, the new name will not resolve until the lease renews or someone updates the record. Renaming the machine does not rename it in anyone else's systems.

Services that were configured by hostname. A database with a granted host of oldname.local will refuse connections from the new name.

Doing It Non-Interactively

For provisioning scripts, set it in the same pass as the hosts entry:

NEWHOSTNAME='pi-enviro'
sudo hostnamectl set-hostname "$NEWHOSTNAME"
if grep -q '^127\.0\.1\.1' /etc/hosts; then
    sudo sed -i "s/^127\.0\.1\.1.*/127.0.1.1    $NEWHOSTNAME/" /etc/hosts
else
    echo -e "127.0.1.1  $NEWHOSTNAME" | sudo tee -a /etc/hosts >/dev/null
fi

The if grep guard is the part that prevents duplicate entries when the script runs twice — the same failure the one-liner from most guides walks straight into.

Don't trust the download button. Trust the hash.

If this was useful, More hands-on tech guides at TechGeek. No pressure — read it whenever it's useful.


Luke Keam
Spot a bug or want a custom utility built? I'd love to hear from you. Get in touch
Luke Keam | techgeek.biz