How to Change the Hostname on Linux (hostnamectl, /etc/hostname & /etc/hosts)

Setting a server’s hostname is one of the first tasks any administrator performs after provisioning a new Linux machine. The hostname identifies your server on the network, appears in your shell prompt, and is used by mail servers, monitoring tools, and logging systems to distinguish one host from another. On modern Linux distributions, the cleanest way to change the hostname is the `hostnamectl` command, which applies the change immediately and makes it persist across reboots.

This guide walks through the modern `systemd` approach, the configuration files involved, the three hostname types, the difference between a fully qualified domain name (FQDN) and a short hostname, and the one `/etc/hosts` detail almost everyone forgets.

Key Takeaways
• Use `sudo hostnamectl set-hostname newname` to change the hostname on any `systemd`-based distribution (Ubuntu, Debian, CentOS, RHEL, Fedora, and others). The change is immediate and persistent.
• The static hostname lives in `/etc/hostname`; `hostnamectl` writes to it for you.
Always update `/etc/hosts` so the new name resolves locally, otherwise you will see `sudo: unable to resolve host` on every `sudo` command.
• `hostname newname` makes a temporary change that resets on reboot.
• Linux tracks three hostname types: static, transient, and pretty. View all of them with `hostnamectl`.

What is a hostname on Linux?

A hostname is the human-readable label that identifies a machine on a network. It can be a short name like `web01` or a fully qualified domain name (FQDN) like `web01.example.com`. The short hostname is the part before the first dot; the FQDN includes the domain that the host belongs to.

Linux distinguishes between two forms you will reference constantly:

  • Short hostname — `web01`. Convenient for shell prompts and quick identification.
  • FQDN — `web01.example.com`. Required for mail servers, TLS certificates, reverse DNS (rDNS), and anywhere the host must be uniquely addressable on the public internet.

The FQDN is not stored in a single dedicated file. Instead, it is resolved by combining the static hostname with the domain mapping you provide in `/etc/hosts` (and, optionally, DNS). That is why updating `/etc/hosts` is not optional housekeeping but part of correctly setting the name.

What are the three hostname types?

On `systemd`-based systems, `hostnamectl` exposes three distinct hostname values. Understanding them prevents confusion when the prompt shows one name and a tool reports another.

Hostname type Where it lives Survives reboot? Typical use
Static `/etc/hostname` Yes The canonical, persistent name (e.g. `web01`)
Transient Kernel / runtime only No Temporary name set at runtime or by DHCP
Pretty `/etc/machine-info` Yes Free-form UTF-8 label (e.g. `Danish’s Web Server`)

Run `hostnamectl` with no arguments to see all three at once, along with the machine ID, OS, kernel, and architecture:

“`bash hostnamectl “`

“` Static hostname: web01 Icon name: computer-vm Chassis: vm Machine ID: 3f2c9a… Boot ID: a17b4e… Virtualization: kvm Operating System: Ubuntu 24.04 LTS Kernel: Linux 6.8.0 Architecture: x86-64 “`

The static hostname is the one you almost always want to change. The transient hostname is what the kernel reports at runtime and is often set by DHCP; it falls back to the static value when nothing overrides it. The pretty hostname is a descriptive label that may contain spaces and special characters and is used by some desktop and management interfaces.

How do you change the hostname with hostnamectl?

On any modern distribution that uses `systemd` (Ubuntu, Debian, CentOS, RHEL, Fedora, AlmaLinux, Rocky Linux, openSUSE, and more), the `hostnamectl set-hostname` command is the recommended method. It updates `/etc/hostname`, applies the change to the running system immediately, and persists it across reboots, all in one step.

“`bash sudo hostnamectl set-hostname web01 “`

To set a fully qualified name, pass the FQDN:

“`bash sudo hostnamectl set-hostname web01.example.com “`

You can target a specific hostname type with a flag:

“`bash

sudo hostnamectl set-hostname web01 –static

sudo hostnamectl set-hostname “Danish’s Web Server” –pretty “`

There is no reboot required for the hostname change itself to take effect at the system level. New shell sessions and most services will pick up the new name immediately. (More on the shell prompt below.)

What files are involved in a Linux hostname change?

Two files do the heavy lifting. `hostnamectl` edits the first for you automatically; the second is your responsibility.

The /etc/hostname file

`/etc/hostname` holds the static hostname as a single line of plain text. After running `hostnamectl set-hostname web01`, the file contains exactly:

“`bash cat /etc/hostname “`

“` web01 “`

You can edit this file by hand, but a manual edit alone does not apply the change to the running system until the next reboot or until you also run a `hostname` command. This is why `hostnamectl` is preferred: it keeps the file and the live system in sync.

The /etc/hosts file (the part people forget)

`/etc/hosts` is the local name-resolution file. It must map your hostname (and FQDN) to a loopback or local IP address so the system can resolve its own name without a DNS lookup. On Debian and Ubuntu, the convention is to map the FQDN and short name to `127.0.1.1`; other distributions commonly use `127.0.0.1` or the server’s primary IP.

Open the file and update the relevant line:

“`bash sudo nano /etc/hosts “`

A correct entry looks like this:

“` 127.0.0.1 localhost 127.0.1.1 web01.example.com web01 “`

If your server has a static public or private IP that other hosts use to reach it, map the FQDN there as well:

“` 203.0.113.10 web01.example.com web01 “`

The gotcha that bites almost everyone: after you change the hostname, you must update `/etc/hosts` to map the new name to `127.0.1.1` (or your IP). If you forget, the new hostname has no local resolution, and the system cannot look up its own name. The symptom is unmistakable: every time you run a command with `sudo`, you get a warning like this:

“` sudo: unable to resolve host web01: Name or service not known “`

The command still runs, but the warning appears on every invocation because `sudo` tries to resolve the local hostname and fails. The fix is simply to add the new hostname to `/etc/hosts`. Always treat `hostnamectl set-hostname` and the `/etc/hosts` edit as a single two-step operation, never one without the other.

How do you make a temporary hostname change?

If you only need to change the hostname for the current session, for example to test something, use the legacy `hostname` command:

“`bash sudo hostname web01-test “`

This sets the transient hostname instantly, but it is not persistent. After a reboot, the system reverts to the static hostname defined in `/etc/hostname`. The `hostname` command does not touch `/etc/hostname` or `/etc/hosts`, so use it only for short-lived changes.

How do you verify the hostname change?

After making the change, confirm it took effect using several commands. Each reports a slightly different view.

“`bash

hostnamectl

hostname

hostname -f

hostname -s

hostname -d “`

For `hostname -f` to return the correct FQDN, the `/etc/hosts` entry must list the FQDN before the short name on the same line, exactly as shown earlier. If `hostname -f` returns only the short name or an unexpected value, re-check the ordering in `/etc/hosts`.

Why does my shell prompt still show the old hostname?

A common point of confusion: you run `hostnamectl set-hostname`, everything reports the new name, but your terminal prompt still shows the old one. This is expected. The shell captured the hostname when the session started, so your current session keeps the old prompt.

To refresh the prompt, simply log out and log back in, or open a new SSH session or terminal. The new session reads the updated hostname and displays it. A full reboot is not necessary just for the prompt, but a re-login is. If you have many background processes or daemons that cached the old name, a reboot is the cleanest way to guarantee every component picks up the change.

Methods at a glance

Here is how the three approaches compare:

Method Command Persistent? Updates /etc/hostname? Notes
hostnamectl `sudo hostnamectl set-hostname web01` Yes Yes (automatic) Recommended on all `systemd` distros
/etc/hostname (manual edit) Edit file, then reboot Yes You edit it directly Needs reboot or `hostname` to apply live
hostname (temporary) `sudo hostname web01-test` No No Resets on reboot; transient only

In all three cases, remember to update `/etc/hosts` so the new name resolves locally.


Run your server identity your way with DarazHost

Changing a hostname or setting a custom FQDN requires full root access to a server you genuinely control. On a DarazHost VPS or dedicated server, you get complete root-level control to set your hostname and FQDN exactly as you need, whether that is for a mail server, host identification, reverse DNS (rDNS), or TLS certificate provisioning. Our reliable infrastructure gives you ownership over your server’s identity, and our team is available 24/7 to help with DNS, rDNS, and configuration questions. When you need a Linux environment where `hostnamectl`, `/etc/hostname`, and `/etc/hosts` are all yours to configure, DarazHost provides the foundation.


How do you change the hostname on older, non-systemd systems?

A small number of legacy systems do not use `systemd` and therefore lack `hostnamectl`. On those, edit `/etc/hostname` directly and apply the change with the `hostname` command, then update `/etc/hosts`:

“`bash echo “web01” | sudo tee /etc/hostname sudo hostname web01 sudo nano /etc/hosts # add the new name “`

Some older distributions store the hostname in `/etc/sysconfig/network` (older RHEL/CentOS) instead of, or in addition to, `/etc/hostname`. On any system released in recent years, however, `hostnamectl` is available and is the correct tool.

Frequently asked questions

Does changing the hostname require a reboot? No. `hostnamectl set-hostname` applies the change to the running system immediately and persists it. You only need to re-login to update your current shell prompt, and a reboot is only useful if cached daemons need to pick up the new name.

Why do I get “sudo: unable to resolve host” after changing the hostname? Because the new hostname is not listed in `/etc/hosts`, so the system cannot resolve its own name locally. Add a line mapping the new hostname (and FQDN) to `127.0.1.1` or your server IP, and the warning disappears.

What is the difference between a hostname and an FQDN? The hostname is the short label (e.g. `web01`). The FQDN is the fully qualified name including the domain (e.g. `web01.example.com`). The FQDN is required for mail servers, rDNS, and TLS certificates.

What are the static, transient, and pretty hostnames? The static hostname is the persistent name in `/etc/hostname`. The transient hostname is a runtime-only value (often set by DHCP) that resets on reboot. The pretty hostname is a free-form descriptive label. Run `hostnamectl` to view all three.

Will `hostname newname` survive a reboot? No. The `hostname` command sets only the transient hostname for the current session. To make a persistent change, use `hostnamectl set-hostname` or edit `/etc/hostname`.

About the Author

Leave a Reply