Guides

Change the hostname on Ubuntu (and Debian or Rocky Linux) without a reboot

Ubuntu change hostname: sudo hostnamectl set-hostname NAME, update the 127.0.1.1 line in /etc/hosts and stop cloud-init reverting it. Debian and Rocky too.

Published ~7 min read
On this page

Short answer: on Ubuntu, run sudo hostnamectl set-hostname web01, then replace the old name on the 127.0.1.1 line of /etc/hosts with the new one. The change applies at once, with no reboot; a new shell shows it in the prompt. On a cloud image, also make sure cloud-init does not put the old name back at the next boot.

Ubuntu · Debian
hostnamectl                               # the current names
sudo hostnamectl set-hostname web01
sudoedit /etc/hosts                       # 127.0.1.1 old-name  ->  127.0.1.1 web01
hostname                                  # prints: web01
exec bash                                 # reload the prompt in this shell

The same commands work on Debian and on Rocky Linux, with one difference in /etc/hosts covered below. Option names and the three kinds of hostname follow man hostnamectl (systemd 252, as in Debian 12); the cloud-init behaviour was read from cloud-init 22.4.2’s own module documentation on Debian 12.

How do I change the hostname on Ubuntu, step by step?

  1. 1Look at the current name
    Ubuntu · Debian · Rocky
    hostnamectl
    Example output (shortened)
     Static hostname: ubuntu-2gb-ams-1
           Icon name: computer-vm
             Chassis: vm 🖴
      Virtualization: kvm
    Operating System: Ubuntu 24.04.3 LTS
              Kernel: Linux 6.8.0-85-generic
        Architecture: x86-64

    The plain hostnamectl (or hostnamectl status) only reads. A Transient hostname line appears only when the running name differs from the static one, and a Pretty hostname line only when one is set: on a Debian 12 KVM server checked read-only for this guide, neither line was there.

  2. 2Set the new name
    Ubuntu · Debian · Rocky
    sudo hostnamectl set-hostname web01

    This writes /etc/hostname and changes the running name in one go. Recent systemd versions, such as 252 in Debian 12, document the shorter sudo hostnamectl hostname web01; the older set-hostname spelling is still accepted. Use lowercase letters, digits and hyphens: man hostnamectl limits the name to valid DNS labels (dots only between labels, for a full name) and to 64 characters in total. Underscores and spaces are not allowed.

  3. 3Fix the 127.0.1.1 line in /etc/hosts
    /etc/hosts before
    127.0.0.1 localhost
    127.0.1.1 ubuntu-2gb-ams-1
    /etc/hosts after
    127.0.0.1 localhost
    127.0.1.1 web01

    Debian and Ubuntu map the machine’s own name to 127.0.1.1. Change that line rather than adding a second one. If the server has a full domain name, put it first and the short name after it: 127.0.1.1 web01.example.com web01. If the file starts with a comment about manage_etc_hosts, cloud-init rewrites it at boot; step 5 deals with that.

  4. 4Check it
    Ubuntu · Debian · Rocky
    hostname          # the running (transient) name
    hostname -f       # the full name, via /etc/hosts or DNS
    cat /etc/hostname # the static name, as stored
    sudo true         # no "unable to resolve host" warning = /etc/hosts is right

    The prompt of a shell that was already open keeps the old name until you start a new one (exec bash) or log in again. No reboot is needed.

  5. 5On a cloud image, keep cloud-init from undoing it

    See why the hostname changes back after a reboot. On most servers nothing needs doing, but one line makes it certain.

Static, transient and pretty hostname: what is the difference?

NameWhere it livesWhat sets itSet only this one
Static/etc/hostnameYou, with hostnamectl or by editing the file--static
TransientThe running kernel (what hostname and uname -n print)The static name at boot; a DHCP lease or NetworkManager only when no valid static name is set--transient
Pretty/etc/machine-infoYou; free text for people, such as "Web server 01"--pretty

Plain set-hostname changes all three. Passing a plain name such as web01 clears the pretty name, because it would only repeat the static one. If you want a descriptive label for people, set it separately:

Ubuntu · Debian · Rocky
sudo hostnamectl set-hostname --pretty "Web server 01 (Amsterdam)"

sudo hostname web01, the old way, changes only the transient name. It works until the next boot, which reads /etc/hostname again and brings the old name back. That is the most common reason a hostname “does not stick”.

Why does sudo say “unable to resolve host”?

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

sudo looks up the machine’s own name, and nothing answers for it: the /etc/hosts line still names the old host. sudo still runs the command after the warning, sometimes after a short delay. Correcting the 127.0.1.1 line from step 3 removes it. Whether you see it at all depends on /etc/nsswitch.conf: when its hosts: line lists myhostname, systemd’s module that always answers for the local name, the lookup succeeds even without the hosts entry. The Debian 12 cloud image checked for this guide had hosts: files myhostname dns, but not every install lists it, so fix the file either way. When the server cannot resolve other names either, the error is a different one, covered in temporary failure in name resolution.

Why does the hostname change back after a reboot?

Four causes, in the order they are usually found:

  • It was set with hostname, not hostnamectl. Only the running name changed. Run sudo hostnamectl set-hostname and check cat /etc/hostname.
  • cloud-init set it again. Cloud images run cloud-init at every boot. By default it updates /etc/hostname from the provider’s metadata, but its documentation says that if you change /etc/hostname by hand after boot, cloud-init no longer modifies it. It still sets the name afresh when it believes it is running on a new instance, for example after the image has been redeployed or cloned. To pin your name in every case, set preserve_hostname:
Ubuntu · Debian · Rocky (with cloud-init)
echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo grep -i hostname /var/log/cloud-init.log | tail -n 5   # what cloud-init did at the last boot

A line ending in assuming user maintained hostname in that log means cloud-init saw your change and left it alone. If the provider’s user-data sets preserve_hostname itself, that value takes priority over local files.

  • /etc/hosts is regenerated. With manage_etc_hosts on, cloud-init writes the hosts file from a template at each boot, so your 127.0.1.1 edit disappears while the hostname itself survives. Edit the template instead; the steps are in what is an FQDN, which sets a full domain name the same way.
  • DHCP or NetworkManager supplied it. On desktops and some RHEL-family installs, the transient name comes from the network when the static name is empty or localhost. Setting a real static name with hostnamectl ends that.

Short name or fully qualified name?

Either is valid. Debian’s convention is the short name (web01) in /etc/hostname and the full name (web01.example.com) first on the hosts line, so hostname prints the short and hostname -f the full one. Many RHEL-family guides store the full name as the hostname instead. What matters for mail servers and TLS is that the full name resolves in public DNS and that its reverse record matches; that is a DNS job, not a hostname one, and the FQDN guide walks through it.

Debian and Rocky Linux: what is different?

Debian

Identical to Ubuntu, with one practical catch: a minimal Debian install may have no sudo. Run the commands as root (su - first) and leave out sudo. The 127.0.1.1 convention comes from Debian.

Rocky Linux, AlmaLinux, RHEL

hostnamectl set-hostname is the same. NetworkManager offers an equivalent, sudo nmcli general hostname web01. There is no 127.0.1.1 line by default; add the name to the 127.0.0.1 line, or map the server’s real IP address to it, full name first:

/etc/hosts on Rocky Linux (either form)
127.0.0.1   localhost localhost.localdomain web01
203.0.113.10 web01.example.com web01

What else uses the hostname?

  • Services that read the name at start-up keep the old one until they restart: mail servers, monitoring agents, log shippers. An installed Postfix even stores its own copy as myhostname; check it with postconf myhostname.
  • Cluster members. Kubernetes, Docker Swarm and database clusters often use the hostname as the node name. Rename the node in that tool’s own way first, or it may come back as a new, unknown member.
  • SSH clients. Changing the hostname does not change the server’s host key or its IP address, so known_hosts entries stay valid.
  • Ubuntu Desktop. The Device Name field in Settings, under About (System, About on Ubuntu 24.04), changes the same hostname.

Does the hostname change DNS or reverse DNS?

No. The hostname lives only on the server. Other machines find it through DNS: an A (and AAAA) record you create at your DNS provider, as in pointing a domain to a VPS, and a reverse DNS (PTR) record that belongs to whoever owns the IP address, which for a rented server is the provider. Mail providers check that the PTR name resolves back to the same address, so set all three to the same full name when the server sends mail. Reverse DNS and PTR records explains the checks.

Renaming the server belongs with the other first-login tasks. The full list, from a sudo user to the firewall, is in initial server setup, and checking the Linux version shows what else hostnamectl tells you about the machine.

Frequently asked

How do I change the hostname on Ubuntu?

Run sudo hostnamectl set-hostname NEWNAME, then edit /etc/hosts and replace the old name on the 127.0.1.1 line with the new one. The change takes effect at once without a reboot; open a new shell to see it in the prompt, and check it with hostnamectl.

Why does my hostname change back after a reboot?

Either it was set with the hostname command, which changes only the running name and not /etc/hostname, or cloud-init set it again from the provider's metadata, for example after the server was redeployed. Use hostnamectl, and on a cloud image add preserve_hostname: true in a file under /etc/cloud/cloud.cfg.d/.

How do I fix sudo: unable to resolve host?

The machine's own name is missing from /etc/hosts, usually right after a rename. Put the new hostname on the 127.0.1.1 line on Debian and Ubuntu, or on the 127.0.0.1 line on Rocky Linux. sudo still runs the command despite the warning, and the warning stops as soon as the file is right.

Does changing the hostname change reverse DNS?

No. The hostname exists only on the server. Forward DNS is an A record at your DNS provider, and reverse DNS is a PTR record set by whoever owns the IP address, normally in your hosting provider's panel. For a mail server, make all three name the same fully qualified domain name.

Related articles

All guides