Changing the hostname (machine name) on a Linux system is a common administrative task. The hostname identifies your system on a network and is used for various networking configurations and services.
This guide explains multiple methods to change your Linux machine name safely and effectively on various distributions.
Understanding the Hostname
The hostname is a label assigned to a device connected to a network. It helps identify and differentiate systems within local networks or on the internet.
On Linux, the hostname is stored in several locations and is used by system services, network management tools, and even prompt displays in terminal shells.
“A meaningful hostname improves system management and makes network troubleshooting easier.”
Hostnames usually follow a simple convention: alphanumeric characters, dashes, and dots. They must not contain spaces or special characters.
| Hostname Rules | Description |
|---|---|
| Maximum Length | Up to 63 characters per segment (label) |
| Allowed Characters | Letters (A-Z, a-z), digits (0-9), hyphen (-) |
| Restrictions | No spaces or special characters; cannot start or end with a hyphen |
Checking Your Current Hostname
Before changing your hostname, it is important to verify the current name assigned to your machine. There are several commands that can help you retrieve this information quickly:
hostname– displays the current hostnamehostnamectl status– shows detailed hostname information including static and transient namescat /etc/hostname– outputs the static hostname saved in the system file
hostname
hostnamectl status
cat /etc/hostname
Each of these commands can provide insight into what your system currently recognizes as its name.
Changing Hostname Temporarily
If you want to change the hostname temporarily (until the next reboot), you can use the hostname command directly. This is useful for testing or temporary identification purposes.
sudo hostname new-hostname
Replace new-hostname with your desired hostname. This change will immediately affect your system, but it will revert after reboot.
Note: Temporary hostname changes do not persist across reboots. Use a permanent method to keep the hostname after restarting.
Changing Hostname Permanently
For a persistent hostname change, you need to update system files and configurations. The process varies slightly depending on your Linux distribution and the init system it uses.
Using hostnamectl (Systemd-based systems)
Most modern Linux distributions like Ubuntu 16.04+, Debian 9+, Fedora, and CentOS 7+ use systemd. The hostnamectl command provides a simple way to manage hostnames.
sudo hostnamectl set-hostname new-hostname
This command changes the static hostname immediately and ensures it persists after reboot.
To verify the change, run:
hostnamectl status
The output will show the new static hostname along with transient and pretty hostnames.
Modifying /etc/hostname and /etc/hosts Manually
If your system does not support hostnamectl, or you prefer manual editing, you can update the hostname in these files:
| File | Purpose |
|---|---|
/etc/hostname |
Stores the system’s static hostname |
/etc/hosts |
Maps hostnames to IP addresses locally |
Follow these steps:
- Open
/etc/hostnamein a text editor:
sudo nano /etc/hostname
Replace the existing hostname with your new desired hostname. Save and exit.
- Edit
/etc/hoststo update hostname mappings:
sudo nano /etc/hosts
Look for lines referencing your old hostname, especially those starting with 127.0.1.1 or 127.0.0.1, and replace the old hostname with the new one.
Example:
127.0.0.1 localhost
127.0.1.1 new-hostname
Save and exit the file.
Finally, reboot the system or run:
sudo systemctl restart systemd-hostnamed
Or simply:
sudo reboot
Using the hostname Command with /etc/hostname File
On older distributions without systemd, you can:
- Run
sudo hostname new-hostnameto set the hostname temporarily. - Update
/etc/hostnamemanually as described above for persistence.
Important: Failing to update /etc/hosts can cause issues with network services and system startup.
Understanding Hostname Types in systemd
When using hostnamectl, you might notice multiple hostname types:
| Hostname Type | Description |
|---|---|
Static |
The main hostname stored in /etc/hostname |
Transient |
Temporary hostname assigned by DHCP or mDNS |
Pretty |
Human-friendly hostname, can contain spaces and special characters |
You can set these individually with commands like:
sudo hostnamectl set-hostname "My Pretty Hostname" --pretty
sudo hostnamectl set-hostname new-hostname --static
sudo hostnamectl set-hostname temp-hostname --transient
Changing Hostname on Specific Linux Distributions
Different distributions may have slightly different procedures or tools. Here are some common examples:
Ubuntu
Ubuntu 16.04 and later use systemd. Run:
sudo hostnamectl set-hostname new-hostname
Edit /etc/hosts if necessary, then reboot or restart hostname service.
Debian
Debian 9 and newer also use systemd. Earlier versions might require manual editing.
For Debian 8 or earlier:
- Edit
/etc/hostnameand/etc/hosts. - Run
sudo /etc/init.d/hostname.sh startor reboot.
CentOS / RHEL
CentOS 7+ and RHEL 7+ use systemd. Use hostnamectl:
sudo hostnamectl set-hostname new-hostname
Older versions:
- Edit
/etc/sysconfig/networkand setHOSTNAME=new-hostname - Edit
/etc/hostsaccordingly - Reboot or restart network and hostname services
Common Issues and Troubleshooting
Sometimes hostname changes may not reflect immediately or cause networking issues. Here are tips to address common problems:
- Check the hosts file: Ensure
/etc/hostshas the new hostname mapped to127.0.1.1or127.0.0.1. - Restart hostname service: Use
sudo systemctl restart systemd-hostnamedon systemd systems. - Flush caches: Some applications or shells cache hostname; logging out and back in may help.
- Network Manager interference: On systems with NetworkManager, verify it does not overwrite hostname settings.
“Hostname mismatches often cause errors in SSH connections, mail servers, and other network-dependent services.”
Automating Hostname Changes in Scripts
When managing multiple machines or automating setups, you may want to script hostname changes. Below is a basic example using hostnamectl in a bash script:
#!/bin/bash
# Script to change hostname
if [ "$#" -ne 1 ]; then
echo "Usage: $0 new-hostname"
exit 1
fi
NEW_HOSTNAME=$1
echo "Setting hostname to $NEW_HOSTNAME"
sudo hostnamectl set-hostname "$NEW_HOSTNAME"
echo "Updating /etc/hosts"
sudo sed -i "s/^\(127\.0\.1\.1\s*\).*/\1$NEW_HOSTNAME/" /etc/hosts
echo "Hostname changed. Consider rebooting for full effect."
This script safely updates the static hostname and modifies the hosts file accordingly.
Best Practices When Naming Your Linux Machine
Choosing a good hostname is more than just a label. Consider the following recommendations:
- Keep it simple: Use lowercase letters, digits, and hyphens only.
- Be descriptive: Include relevant information like location, role, or environment.
- Avoid special characters: Spaces, underscores, and symbols can cause issues.
- Use consistent naming conventions: This helps when managing many systems.
Examples of good hostnames:
| Hostname | Description |
|---|---|
| webserver-nyc01 | Web server located in New York data center, unit 01 |
| db-prod-02 | Production database server number 2 |
| backup-london | Backup server located in London |
Summary
Changing your Linux machine name involves updating the hostname in system files and possibly using commands like hostnamectl. Temporary changes can be made with the hostname command but will revert after reboot.
For permanence, modify /etc/hostname, update /etc/hosts, and restart the appropriate services or reboot.
Following best practices and understanding your Linux distribution’s tools ensures a smooth hostname change process. Proper hostname configuration is critical for network operations, security, and system management.
Remember: Always back up configuration files before making changes to avoid system issues.