The difficulty in permanently changing a hostname stems from the fact that modern Linux systems use multiple, distinct mechanisms to store and apply hostnames. The traditional method of editing
/etc/hostname often conflicts with systemd’s
hostnamectl, which manages the runtime, static, and pretty hostnames independently. Without using the correct tool or understanding these layers, changes may appear to work temporarily but revert after a reboot because they were only applied to one part of the system state rather than all persistent configurations.
Understanding the Three Hostname States
To effectively manage your system's identity, you must understand that systemd divides the hostname into three separate categories. This separation is designed to allow flexibility, such as having a short name for network protocols while maintaining a more descriptive name for human users. However, it also means that a change made in one area does not automatically propagate to the others, leading to confusion when edits seem to disappear.
- Static Hostname: This is the default hostname used during boot. It is typically read from the
/etc/hostname file. When you see the prompt in a terminal, this is usually what is being displayed. Changing this value ensures that your machine has a consistent identity across reboots.
- Pretty Hostname: This is a free-form, user-friendly name that can include spaces, special characters, and Unicode. It is primarily used by graphical user interfaces and service discovery protocols like mDNS. It does not affect how the system identifies itself on standard TCP/IP networks.
- Transient Hostname: This is a volatile name assigned dynamically by the kernel or network configuration services, such as DHCP. If your router assigns a name via DHCP, it will override the static hostname until the lease expires or the interface goes down. This is often why manual edits seem to be ignored.
The Role of /etc/hostname
Historically, most Linux distributions relied entirely on the plain text file located at
/etc/hostname. In many older systems or minimal installations without systemd, editing this file and running a command to reload it was sufficient. However, on modern Debian-based or Red Hat-based systems, systemd takes over this responsibility. If you manually edit
/etc/hostname but do not inform systemd of the change, the system might still read the old value from its internal database. Conversely, if you use the proper tools, they update both the file and the internal state simultaneously.
Using hostnamectl for Permanent Changes
The recommended way to change the hostname on modern Linux distributions is through the
hostnamectl utility. This tool interacts directly with systemd and ensures that all relevant parts of the system are updated correctly. It eliminates the guesswork involved in knowing which file to edit or which daemon needs restarting.
To set the permanent static hostname, open your terminal and execute the following command, replacing
new-name with your desired identifier:
sudo hostnamectl set-hostname new-name
This single command updates the static hostname immediately and writes the change to
/etc/hostname. You can verify the change by running
hostnamectl status, which will display the current state of all three hostname types. Unlike manual file edits, this method handles the synchronization between the runtime kernel variable and the persistent storage files.
Handling Pretty Hostnames
If you wish to assign a more descriptive name that appears in graphical environments, you can use the
--pretty flag. For example:
sudo hostnamectl set-hostname "My Desktop PC" --pretty
Note that you should keep the quotes around names containing spaces. While this changes the visual label, it does not alter the network identity unless you also set the static hostname. Many administrators prefer to keep the static hostname simple (e.g.,
server01) and use the pretty hostname for identification purposes.
Common Pitfalls and Troubleshooting
Even after using
hostnamectl, users sometimes report that the hostname reverts. This is almost always due to network configuration services overriding the static setting. If your system uses NetworkManager or systemd-networkd, check your connection profiles. Some DHCP clients are configured to accept the hostname provided by the router and push it to the system as a transient hostname.
You can disable this behavior by checking your network manager settings. In NetworkManager, ensure that the option to request a hostname from DHCP is disabled for your specific connection profile. Additionally, check if any cloud-init scripts are present, as these are common in virtualized environments and will overwrite the hostname upon every boot based on metadata provided by the cloud provider.
For SSH connections, changing the hostname requires updating the client's known hosts file if you connect via IP address, or ensuring your DNS records are updated if you connect via the new name. Failure to update DNS can lead to connection issues similar to those described in
Troubleshooting kex_exchange_identification Error in Linux SSH Connection, where the server cannot properly identify itself to the client. Similarly, if you are managing remote sessions, ensure that your terminal emulator settings are not caching old environment variables, which can cause confusion about which hostname is currently active.
Another frequent issue arises with local shell prompts. Even after the system hostname is changed, your current terminal session may still show the old name. This is because the
$HOSTNAME environment variable was set when the shell started. You must either log out and log back in, or run
export HOSTNAME=$(hostname) in your current session to reflect the change immediately.
Verifying the Configuration
After making changes, it is crucial to verify that the system behaves as expected. Run
hostnamectl without arguments to see a summary. Check
/etc/hostname with a text editor to ensure the file content matches your expectation. Finally, test the persistence by rebooting the machine. If the hostname remains correct after a restart, the change is truly permanent. If it reverts, investigate DHCP leases, cloud-init configurations, or custom startup scripts that might be resetting the value.
Frequently Asked Questions
Why does my hostname change back after a reboot?
This usually happens because a network service like DHCP is assigning a transient hostname that overrides your static setting. Check your network manager configuration to ensure it is not requesting a hostname from the router, or inspect cloud-init scripts if you are in a virtualized environment.
Can I use spaces in my Linux hostname?
You can use spaces in the "pretty" hostname, but not in the static hostname. The static hostname must follow DNS naming conventions, which restricts characters to letters, numbers, and hyphens. Always use the
--pretty flag when setting names with special characters.
Do I need to restart any services after changing the hostname?
No,
hostnamectl applies the change instantly to the kernel and updates the configuration files. However, your current terminal session will not update until you start a new login shell. Applications that cached the old hostname may need to be restarted to recognize the new identity.
How do I change the hostname on an older Linux distribution without systemd?
On legacy systems, you must manually edit the
/etc/hostname file using a text editor like
nano or
vim. After saving the file, you may need to run
/etc/init.d/hostname.sh start or simply reboot the system to apply the changes.
Is it safe to delete the /etc/hostname file?
Deleting
/etc/hostname is generally not recommended as it may cause boot warnings or prevent the system from setting a default name. Instead, empty the file or replace its contents with a valid hostname. Modern systems rely on this file as the primary source of truth for the static hostname.