Your server’s hostname is how it identifies itself on the network. Get it wrong and you’ll break mail delivery, SSL certificates, and any service that relies on reverse DNS. Here’s how to change it correctly.
What Is a Linux Hostname?
A hostname is a human-readable label assigned to a machine on a network. It maps to an IP address and gets used in everything from SSH prompts to system logs to SSL certificate validation.
Static, Pretty, and Transient Hostnames
Linux tracks three hostname types. The static hostname is the persistent one stored in /etc/hostname and loaded at boot. The pretty hostname is a free-form label that can include spaces and special characters, stored in /etc/machine-info. The transient hostname is a runtime value that can be set by DHCP or mDNS and overrides the static one temporarily.
For server environments, the static hostname is the one that matters.
How to Check Your Current Hostname
Two commands cover this. For a quick one-liner:
hostnameFor full details including all three hostname types:
hostnamectlThe hostnamectl output also shows virtualization type, OS, and kernel version, which makes it useful during initial server setup.
How to Change Hostname Permanently
Permanent hostname changes persist across reboots. You need this when setting up a new server, renaming a machine in your infrastructure, or fixing a hostname that was set incorrectly during provisioning.
Using hostnamectl
On systemd-based distros (Ubuntu 16.04+, CentOS 7+, Debian 8+), hostnamectl is the cleanest option:
sudo hostnamectl set-hostname new-hostnameReplace new-hostname with your desired name. No reboot required. The change takes effect in new shell sessions. Verify it:
hostnamectlEditing /etc/hostname Directly
You can also edit the hostname file manually:
sudo nano /etc/hostnameReplace the existing name with your new hostname, save, and reboot. Simple, but you lose the ability to set pretty or transient hostnames this way.
Using nmtui for NetworkManager Systems
On systems running NetworkManager, nmtui provides a menu-driven interface. Launch it:
sudo nmtuiNavigate to Set system hostname, enter your new name, and confirm. This method is particularly useful on desktop Linux installs or when you prefer avoiding the command line.
How to Change Hostname Temporarily
Temporary hostname changes don’t survive a reboot. Useful for testing or short-lived environments.
Using the hostname Command
sudo hostname temp-hostnameThis changes the transient hostname immediately. Check it with hostname. Once you reboot, the static hostname from /etc/hostname takes over again.
Updating /etc/hosts After a Hostname Change
Changing your hostname without updating /etc/hosts creates problems. Many local services resolve the hostname against /etc/hosts, and a mismatch causes slow DNS lookups and broken local network resolution.
Why /etc/hosts Must Match Your Hostname
Open the file:
sudo nano /etc/hostsFind the line that maps 127.0.1.1 (or 127.0.0.1 on some distros) to your old hostname and update it:
127.0.1.1 new-hostnameSave and close. You don’t need a reboot for this change to take effect.
Common Hostname Naming Rules
Hostnames have strict formatting requirements. Ignore them and you’ll get subtle failures in certificate validation, DNS, and inter-service communication:
- Maximum 253 characters total, with each label between dots limited to 63 characters.
- Only letters (a-z, A-Z), digits (0-9), and hyphens are allowed. No underscores, no spaces, no dots in the short hostname.
- Cannot start or end with a hyphen.
- Lowercase only is standard practice, even though the system accepts uppercase.
FAQ: Linux Change Hostname
Run: sudo hostnamectl set-hostname your-new-hostname. Then update /etc/hosts to match. The change persists across reboots without requiring one.
Not when using hostnamectl. The new hostname takes effect immediately in new shell sessions. Existing sessions still show the old hostname until you open a new terminal or re-login.
Run hostname for a quick check, or hostnamectl for full details including static, pretty, and transient values.
Use sudo hostnamectl set-hostname new-name on Ubuntu 16.04 and later. Then edit /etc/hosts to update the 127.0.1.1 entry. No reboot required.