On an Ubuntu machine running systemd, use sudo hostnamectl set-hostname new-name to change its hostname. Then check the saved name and local name resolution. This does not automatically rename DNS records, change an IP address or update every application that remembers the old name.
Use a distinctive short name such as lab-ubuntu. For a portable network label, stick to lowercase letters, digits and internal hyphens, with no spaces, leading/trailing hyphen or more than 63 characters.
Record the current name and environment
hostname
hostnamectl status
cat /etc/hostname
cat /etc/hostsSave the original name and a copy of relevant configuration before editing. The active kernel hostname, a saved static hostname and a human-friendly “pretty” hostname are related but distinct settings. Cloud images may manage the saved name differently or omit /etc/hostname.
Run these checks on the machine you mean to rename. If you are connected through SSH, confirm the destination before making a change. A prompt left open from an earlier login may keep displaying an old name even after the system setting changes.
Set the hostname on a systemd Ubuntu host
Replace lab-ubuntu with your chosen name:
sudo hostnamectl set-hostname lab-ubuntu
hostnamectl --static
hostnameCheck both results. On a normal systemd host the new name should be visible without a reboot. Open a fresh shell to check its prompt if necessary. A prompt is presentation, so use the commands above as the verification.
If hostnamectl reports that the system has not booted with systemd, stop following the normal-host branch. The systemd environment guide explains how a container or WSL shell differs from a full Ubuntu boot.
Check local name resolution
Inspect /etc/hosts for a mapping of the old hostname, often on a 127.0.1.1 line in a conventional Ubuntu setup. If you manage this file, edit the hostname on the relevant existing line:
sudoedit /etc/hostsPreserve localhost, IPv6 entries and unrelated mappings. Do not replace the whole file with a generic example. Then test the new name:
getent hosts lab-ubuntuThe result should be consistent with your intended local mapping. If sudo reports “unable to resolve host,” compare the current hostname with this mapping and the configured name-service sources. The name-resolution guide covers the wider diagnostic path.
Update names outside the machine separately
| Where the old name appears | What to update |
|---|---|
| DNS or router client records | Update the responsible DNS/DHCP system; a local hostname command does not edit those records. |
| SSH client configuration | Check HostName settings and aliases on the client. |
| Monitoring, certificates or applications | Follow each service's rename procedure and verify its identity configuration. |
Renaming a host should not require discarding its SSH host keys. If a client reports an unexpected identity change, verify the server fingerprint before modifying its known-host entry.
Cloud images, containers and WSL
Cloud-init and cloud-provider metadata can manage hostname settings at boot. Check that policy before manually changing a managed machine. Where your deployment intentionally preserves a manually selected hostname, cloud-init supports the following setting:
preserve_hostname: trueMerge it into the appropriate cloud configuration under your deployment policy; do not replace the complete cloud-init configuration. Hosted environments can also manage /etc/hosts, so inspect both policies.
For Docker, specify the container hostname through its runtime configuration when creating the container. For WSL, inspect the distribution's supported hostname and hosts-file generation settings. Do not assume hostnamectl can control the surrounding Windows machine.
Verify persistence or undo the change
After any planned restart, verify the hostname again and test resolution from the other machines that need it. To undo a manual change, restore the recorded hostname with hostnamectl and restore only the hosts-file edits you made. Check DNS and application changes separately.
Sources and verification
Reviewed September 11, 2026. An Ubuntu 24.04 container created with a specified hostname verified hostname reporting and local getent resolution, and reproduced hostnamectl failing without systemd. The normal-host rename, reboot persistence, cloud-init changes and WSL configuration are documentation-based steps; they were not executed on a production host or a fully booted systemd VM.
