Change the Hostname on Linux Permanently

Short answer
sudo hostnamectl set-hostname new-name
# replace the old name in /etc/hosts (Debian, Ubuntu, Mint use 127.0.1.1)
sudo sed -i 's/\bold-name\b/new-name/g' /etc/hosts
hostnamectl

No reboot needed. Open a new terminal to see the new name in your prompt. On a cloud VM, also set preserve_hostname: true so cloud-init doesn't revert it.

Check the current hostname

hostnamectl
hostname
cat /etc/hostname
grep -v '^#' /etc/hosts

Example output of hostnamectl:

 Static hostname: old-name
       Icon name: computer-laptop
         Chassis: laptop
      Machine ID: 3f1c...
  Operating System: Ubuntu 26.04 LTS
          Kernel: Linux 7.0.0-...

systemd keeps three names. The static hostname lives in /etc/hostname and is set at boot. The pretty hostname is a display name that may include spaces and capitals ("Anna's Laptop"). The transient hostname is what the kernel currently uses, normally equal to the static one but changeable by DHCP or mDNS. hostnamectl shows a "Transient hostname" line only when it differs.

Choose a valid name

  • Lowercase letters, digits and hyphens only: web-01, anna-thinkpad.
  • Maximum 63 characters, and it must not start or end with a hyphen.
  • Avoid underscores and dots. A dot makes it a fully qualified name (web01.example.com), which is fine on servers that have a real domain but confusing on desktops.

If you pass capitals or spaces to hostnamectl set-hostname, it stores them as the pretty name and derives a cleaned static name. Set them separately to be explicit:

sudo hostnamectl set-hostname anna-thinkpad
sudo hostnamectl set-hostname --pretty "Anna's ThinkPad"

Change the hostname (all distros)

hostnamectl works the same on Ubuntu, Debian, Mint, Fedora, Arch and openSUSE, since they all use systemd. It writes /etc/hostname and updates the running kernel hostname in one step.

sudo hostnamectl set-hostname new-name

Without systemd running (a container, a chroot, WSL without systemd), write the file directly and set the kernel name:

echo new-name | sudo tee /etc/hostname
sudo hostname new-name

GUI options exist too. GNOME: Settings → System → About → Device Name. On KDE Plasma, Xfce and Cinnamon, hostnamectl is the simplest route. openSUSE: YaST → Network Settings → Hostname/DNS, which writes the same file.

Update /etc/hosts

The hostname must resolve to an address on the machine itself. If it doesn't, sudo prints "sudo: unable to resolve host" and can pause for several seconds, and some services (mail, databases, Java apps) fail to start.

Ubuntu, Debian and Linux Mint

The installer adds a 127.0.1.1 line with the hostname. Change it to the new name:

127.0.0.1   localhost
127.0.1.1   new-name

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

If the server has a domain, list the FQDN first: 127.0.1.1 web01.example.com web01. For a machine with a static IP, you can use that address instead of 127.0.1.1.

Fedora, Arch and openSUSE

These usually ship a minimal /etc/hosts and resolve the local hostname with nss-myhostname (the myhostname entry on the hosts: line of /etc/nsswitch.conf), so changing the name needs no extra edit. Check:

grep ^hosts: /etc/nsswitch.conf
getent hosts "$(hostname)"

If getent prints nothing, either add myhostname to that line or add 127.0.1.1 new-name to /etc/hosts. Arch installs that skipped this step are the usual cause of "unable to resolve host" there.

Stop cloud-init from resetting the hostname

Cloud images (AWS, Azure, GCP, Oracle, DigitalOcean, many Proxmox and Multipass VMs) run cloud-init, which sets the hostname from instance metadata at every boot. If your change disappears after a reboot, check whether cloud-init is present:

cloud-init status 2>/dev/null
grep -rn hostname /etc/cloud/cloud.cfg /etc/cloud/cloud.cfg.d/ 2>/dev/null

Tell it to keep your name with a drop-in file, so package updates to cloud.cfg don't overwrite your setting:

echo 'preserve_hostname: true' | sudo tee /etc/cloud/cloud.cfg.d/99-preserve-hostname.cfg
sudo hostnamectl set-hostname new-name

Alternatively, set hostname: and fqdn: in your user-data so cloud-init applies the name you want. On some providers cloud-init also manages /etc/hosts from a template (manage_etc_hosts: true); in that case edit /etc/cloud/templates/hosts.debian.tmpl (or the matching template for your distro) or set manage_etc_hosts: false.

Set a fully qualified domain name (servers)

On a server with a real DNS name, you can store the short name as the hostname and let /etc/hosts or DNS supply the domain, or store the FQDN itself. Both are common; the important part is that hostname -f returns the full name and that it resolves.

sudo hostnamectl set-hostname web01
# /etc/hosts on Debian/Ubuntu
127.0.1.1   web01.example.com web01
# or, with a static IP
192.168.1.50 web01.example.com web01

Mail servers, Kerberos/Active Directory joins and some monitoring tools check hostname -f, so get this right before installing them. Desktops don't need a domain at all; a short name plus .local through Avahi is enough on a home network.

Things that still show the old name

  • Your shell prompt: open a new terminal or run exec bash.
  • The desktop session: some apps read the name once at login. Log out and back in. On X11 sessions (KDE Plasma's X11 session, Xfce) a mismatched hostname could break opening new windows; log out right after changing it.
  • mDNS name (new-name.local): restart Avahi with sudo systemctl restart avahi-daemon. If another device already uses the name, Avahi appends -2.
  • The router's device list: it shows the name sent with the DHCP request. Reconnect (nmcli connection up "<name>") or wait for the lease to renew.
  • SSH known_hosts and server certificates: SSH keys are tied to host keys, not names, so changing the name doesn't change host keys. Clients that connect by the new name just see a new entry. If you've set up an SSH server, nothing there needs to change.
  • Services with the name in their config: Postfix (myhostname), Samba (netbios name), monitoring agents, Kubernetes nodes. Search with sudo grep -rl old-name /etc.

Rename a machine on a network you manage

Changing the name on the machine is only half the job if other systems know it by name. Update the DNS record or DHCP reservation on your router, any /etc/hosts entries on other machines, and ~/.ssh/config host aliases. Clients that connect by the new name will see the same host key, so SSH won't warn. If you renamed a machine that has a static IP, the address doesn't change; only the name mapping does.

Verify

hostnamectl
cat /etc/hostname
getent hosts "$(hostname)"
sudo true     # should return instantly with no "unable to resolve host" warning
hostname -f   # prints the FQDN if you configured one

Reboot once on servers and VMs to confirm the name survives, especially where cloud-init is installed.

Common problems

SymptomCause and fix
sudo: unable to resolve host new-name/etc/hosts still has the old name. Edit the 127.0.1.1 line or enable myhostname in nsswitch.
Name reverts after rebootcloud-init, or a provisioning tool (Ansible, Puppet) setting it. Use preserve_hostname: true or change it in your automation.
Name changes when you join certain networksDHCP is setting a transient hostname. Setting a static hostname prevents this on NetworkManager systems; check hostnamectl for a "Transient hostname" line.
Could not set property: Access deniedRun it with sudo, or check polkit rules on locked-down systems.
System has not been booted with systemdYou're in WSL or a container; see that error page and edit /etc/hostname directly. In WSL, set hostname= under [network] in /etc/wsl.conf.

For how hostnames resolve alongside DNS on your network, see the networking basics guide.

Frequently asked questions

Do I need to reboot after changing the hostname?

No. hostnamectl set-hostname applies immediately. Open a new terminal to see it in your prompt; a few running programs keep the old name until restarted or until the next login.

What characters are allowed in a hostname?

Use lowercase letters a–z, digits and hyphens, up to 63 characters, not starting or ending with a hyphen. Put spaces or capitals in the pretty hostname instead.

Why does my hostname change back after a reboot?

On cloud and VM images cloud-init sets it from metadata at every boot. Set preserve_hostname: true in a file under /etc/cloud/cloud.cfg.d/. On some networks DHCP can also set a transient hostname.

What is the difference between static, pretty and transient hostnames?

The static hostname is stored in /etc/hostname and used at boot. The pretty hostname is a free-form display name. The transient hostname is a temporary one the kernel uses, which can come from DHCP or mDNS.

Spotted a mistake or a command that no longer works? Send a correction — pages are re-checked when reports come in.