Changing the hostname of a Linux server is a common administrative task that can help you better manage your networked machines. The hostname is the identifier your server uses on a network and can be crucial for identification, logging, and configuration purposes.
detailed instructions on how to change the hostname on various Linux distributions, covering both temporary and permanent methods. It also explains the underlying configuration files involved and some best practices.
What is a Hostname?
The hostname of a Linux server is a human-readable label that identifies the device on a network. It is used by various services and applications to refer to the server.
A hostname can be a simple name like
server1or a fully qualified domain name (FQDN) likeserver1.example.com.
Having a meaningful hostname makes system administration easier, especially when managing multiple servers.
Why Change the Hostname?
There are several reasons why you might want to change the hostname of your Linux server:
- Rebranding: Changing the hostname to align with new company naming conventions.
- Network organization: Ensuring hostnames reflect server roles or locations.
- Cloning or redeployment: Avoiding duplicate names on the network when duplicating servers.
- Correction: Fixing incorrectly set hostnames during installation.
Types of Hostnames
| Type | Description | Example |
|---|---|---|
| Static Hostname | The persistent hostname used across reboots. | server1 |
| Transient Hostname | A temporary hostname assigned, usually by DHCP or network services. | temp-host |
| Pretty Hostname | A free-form descriptive name, can include spaces and special characters. | My Linux Server |
Check Current Hostname
Before making any changes, it’s a good idea to check the current hostname of your system. You can do this with several commands:
hostname
hostnamectl status
cat /etc/hostname
The hostname command returns the current hostname, while hostnamectl status (available on systemd-based Linux) gives more detailed information. The file /etc/hostname typically contains the static hostname.
Changing Hostname Temporarily
You can change the hostname temporarily without editing any files. This change will last until the next reboot.
sudo hostname new-hostname
Replace new-hostname with your desired hostname. This command immediately changes the hostname but does not persist after reboot.
Note: This method does not update configuration files, so a reboot will revert the hostname to the previous static value.
Changing Hostname Permanently
For the hostname change to persist after reboot, you need to update system configuration files. The method varies slightly depending on your Linux distribution and init system.
Method 1: Using hostnamectl (systemd-based systems)
Most modern Linux distributions use systemd, which provides the hostnamectl command to manage hostnames.
sudo hostnamectl set-hostname new-hostname
This command sets the static hostname permanently. It updates the necessary files and informs systemd of the change.
| Command | Description |
|---|---|
hostnamectl set-hostname --static <name> |
Sets the static hostname only. |
hostnamectl set-hostname --transient <name> |
Sets the transient hostname only (temporary). |
hostnamectl set-hostname --pretty <name> |
Sets the pretty hostname (human-friendly with spaces). |
Method 2: Manually Editing Configuration Files
If your system doesn’t use systemd or you prefer manual configuration, you can edit the following files:
| File | Purpose |
|---|---|
/etc/hostname |
Contains the static hostname of the system. |
/etc/hosts |
Maps hostnames to IP addresses, update to reflect new hostname. |
Follow these steps:
- Open
/etc/hostnamein a text editor with root privileges:
sudo nano /etc/hostname
Replace the existing hostname with your new desired hostname and save the file.
- Edit
/etc/hoststo update any references to the old hostname:
sudo nano /etc/hosts
Look for lines like:
127.0.0.1 old-hostname
::1 old-hostname
Replace old-hostname with your new hostname. Save and exit.
Once these changes are made, reboot your system:
sudo reboot
After reboot, verify the hostname has changed:
hostname
Additional Tips and Best Practices
- Use valid hostnames: Hostnames should only contain letters (a-z), numbers (0-9), and hyphens (-). Avoid spaces and special characters.
- Hostname length: Keep hostnames under 63 characters to comply with DNS standards.
- Update DNS records: After changing your hostname, update any DNS or network configuration to reflect the change.
- Check running services: Some services may cache the hostname or require a restart to pick up the new hostname.
- Remote connections: If connected via SSH, updating hostname on the remote server won’t affect your session but might appear on your terminal prompt depending on your shell config.
Hostname in Network Configuration
Some Linux distributions or network managers may override your hostname based on DHCP or other network configurations.
| Service | Effect on Hostname | File or Config |
|---|---|---|
| DHCP Client | May update transient hostname dynamically. | /etc/dhcp/dhclient.conf |
| NetworkManager | Can manage hostnames, sometimes overriding manual settings. | /etc/NetworkManager/NetworkManager.conf |
| Cloud-init | Cloud instances may set hostname on boot. | /etc/cloud/cloud.cfg |
Make sure to check these if your hostname keeps reverting or is not persistent.
Changing Hostname on Popular Linux Distributions
Ubuntu / Debian
Ubuntu and Debian systems use systemd on recent releases and support hostnamectl.
sudo hostnamectl set-hostname new-hostname
Alternatively, edit /etc/hostname and /etc/hosts manually. After changes, reboot or restart the hostname service:
sudo systemctl restart systemd-logind.service
CentOS / RHEL / Fedora
These distributions also support hostnamectl. Use the same command to set the hostname:
sudo hostnamectl set-hostname new-hostname
Edit /etc/hosts as needed. To verify, use:
hostnamectl status
Arch Linux
Arch Linux uses systemd as well. Set the hostname using:
sudo hostnamectl set-hostname new-hostname
Edit /etc/hosts and /etc/hostname if manual adjustments are preferred.
Verify Hostname Change
After changing your hostname, verify it with following commands:
| Command | Output / Description |
|---|---|
hostname |
Displays the current hostname. |
hostnamectl status |
Shows detailed hostname information and system info. |
cat /etc/hostname |
Shows the static hostname stored in the configuration file. |
ping new-hostname |
Tests network resolution of the new hostname (if DNS is configured). |
Troubleshooting Hostname Issues
If hostname changes do not take effect as expected, consider the following:
- Check for cloud-init: On cloud instances, cloud-init may overwrite hostname on boot.
- NetworkManager: NetworkManager may reset hostname if configured to do so.
- Permissions: Ensure you run commands as
rootor withsudo. - Check /etc/hosts: Incorrect entries here can cause hostname resolution problems.
- Reboot or restart services: Sometimes a reboot or restarting related services is necessary.
Important: Avoid using spaces or special characters in hostnames to ensure compatibility.
Summary
Changing the Linux server hostname is a straightforward process but requires care to ensure persistence and consistency across services.
Key steps to remember:
- Check your current hostname with
hostnameorhostnamectl status. - Change hostname temporarily with
hostname new-hostnameor permanently withhostnamectl set-hostname new-hostname. - Manually edit
/etc/hostnameand/etc/hostsif necessary. - Reboot or restart services to apply the changes.
- Verify the hostname after reboot.
Following these guidelines will keep your Linux server’s identity clear and consistent within your infrastructure.