How to Change the Hostname in Linux: A Complete Guide
Your Linux machine's hostname is its identity on a network — the name other devices and services use to recognize it. Whether you're setting up a server, reorganizing a home lab, or just inherited a machine with a generic name like localhost, knowing how to change the hostname cleanly is a fundamental Linux skill.
The process is straightforward, but the right method depends on your Linux distribution, whether the change needs to survive a reboot, and how deeply the hostname is woven into your system's configuration.
What Is a Hostname in Linux?
A hostname is a human-readable label assigned to a machine. Linux actually recognizes three types:
| Hostname Type | Description | Example |
|---|---|---|
| Static | The traditional hostname stored in system files | webserver-01 |
| Pretty | A free-form display name (can include spaces/capitals) | John's Web Server |
| Transient | A temporary name assigned by the kernel or DHCP | dhcp-192-168-1-10 |
For most purposes — especially on servers — you're working with the static hostname. That's the one that persists across reboots and appears in your shell prompt.
Method 1: Using hostnamectl (Modern Systemd Systems) 🖥️
On any Linux distribution running systemd — which includes Ubuntu 16.04+, Debian 8+, CentOS 7+, Fedora, and most modern distros — hostnamectl is the cleanest and most reliable tool.
Check your current hostname:
hostnamectl This displays your static, pretty, and transient hostnames, plus OS and kernel info.
Change the static hostname:
sudo hostnamectl set-hostname new-hostname Replace new-hostname with your chosen name. The change takes effect immediately — no reboot required. You'll see it reflected in new terminal sessions.
To set a pretty hostname separately:
sudo hostnamectl set-hostname "My Development Server" --pretty hostnamectl writes to /etc/hostname automatically, so the name persists after reboot.
Method 2: Editing /etc/hostname Directly
On older systems or minimal installs without systemd, you can edit the hostname file manually.
Open the file:
sudo nano /etc/hostname The file typically contains a single line with the current hostname. Replace it with your new name, save, and exit.
Apply the change without rebooting:
sudo hostname new-hostname This command sets the transient hostname immediately. The /etc/hostname change takes effect permanently on the next reboot.
Method 3: Using the hostname Command (Temporary Change)
If you only need a hostname change for the current session — for testing or scripting purposes — the hostname command works without touching any files:
sudo hostname temporary-name This change does not survive a reboot. Once the system restarts, it reads from /etc/hostname again. This is useful for network troubleshooting or staging environments where you don't want permanent changes.
Don't Forget /etc/hosts ⚠️
This is the step many guides skip — and skipping it causes problems. Your /etc/hosts file maps hostnames to IP addresses locally. If you change the hostname without updating this file, some applications and services may fail to resolve the machine's own name.
Open the file:
sudo nano /etc/hosts Look for a line like this:
127.0.1.1 old-hostname Change it to:
127.0.1.1 new-hostname Leave the 127.0.0.1 localhost line untouched. This edit ensures local name resolution works correctly after the hostname change.
Hostname Naming Rules You Should Know
Linux hostnames follow RFC 952 and RFC 1123 conventions. Violating these can cause subtle network and service issues:
- Allowed characters: Letters (
a–z,A–Z), numbers (0–9), and hyphens (-) - Not allowed: Underscores, spaces, or special characters in static hostnames
- Length limit: 253 characters total; each label between dots limited to 63 characters
- Case: Technically case-insensitive, but lowercase is the universal convention
A hostname like prod-db-01 is valid. Something like prod_db_01 or Prod DB as a static hostname can cause unexpected behavior in DNS and network services.
How Distribution and Environment Affect the Process
The steps above are consistent, but several factors shape which approach is right for a given setup:
Distribution age matters. Systems running older init systems (SysVinit, Upstart) don't have hostnamectl. You'll rely on direct file edits and the hostname command. Systems running systemd can use all three methods, but hostnamectl is preferred because it handles all three hostname types atomically.
Cloud and containerized environments behave differently. On cloud VMs (AWS EC2, Google Cloud, Azure), the hostname may be managed by cloud-init or a DHCP client. Changes made via hostnamectl can be overwritten at reboot if cloud-init is configured to reset it. You may need to modify /etc/cloud/cloud.cfg and set preserve_hostname: true to make changes stick.
Containers are a separate case entirely. Docker containers inherit their hostname from the container runtime. Changing it inside the container works for that session, but the persistent hostname is controlled by the --hostname flag at container creation, not by system files.
Network-dependent services need attention. Services like Kerberos, mail servers (Postfix/Sendmail), SSL certificates, and Active Directory joins can be sensitive to hostname changes. On systems running these services, a hostname change may require additional reconfiguration steps beyond the OS level.
Verifying the Change
After making your changes, confirm everything took correctly:
hostnamectl # Shows all hostname types (systemd systems) hostname # Shows the active transient hostname cat /etc/hostname # Confirms the persistent static hostname Open a new terminal session to see the updated prompt. If your prompt still shows the old name, the shell session hasn't refreshed — a new session or reboot will reflect the change.
The technical steps for changing a hostname are consistent across most Linux systems, but whether a simple hostnamectl command is sufficient — or whether you're working around cloud-init, containerization, or sensitive network services — depends entirely on what your system is running and what it's connected to.