How to Change Hostname in Linux
(How to Change Hostname in Linux) The Linux hostname needs changing for both networking purposes and system administration as well as identification functions. A Linux system receives its name through the hostname designation as a crucial piece of information for network machine identification. This guide presents several Linux hostname change methods through the execution of hostnamectl commands and nmtui, nmcli, and manual editing operations Screen captures alongside multiple commands are included in the guide to ensure the readers receive assistance with understanding the process.
Understanding Hostname in Linux
A hostname is a label assigned to a system to identify it within a network. It is stored in the following locations:
/etc/hostname
– Contains the static hostname of the system./etc/hosts
– Maps hostnames to IP addresses.- Kernel memory – Temporary hostname used until reboot.
Types of Hostnames in Linux
- Static hostname – Defined in
/etc/hostname
. - Transient hostname – Set temporarily in RAM, lost after reboot.
- Pretty hostname – A human-friendly name (e.g., “My Linux Server”).
Checking the Current Hostname
Before changing the hostname, check the current hostname using:
Or use:
This will display details such as static hostname, transient hostname, and chassis type.
Changing the Hostname Using hostnamectl (Recommended)
Step 1: Change the Hostname
Use hostnamectl
to set a new hostname.
For example, to change the hostname to linux-server
:
Step 2: Verify the Change
Run:
or
Alternative Commands
Instead of hostnamectl
, you can use:
Changing Hostname Using nmtui (Graphical Interface)
If you prefer a text-based UI, use nmtui (Network Manager Text User Interface).
Step 1: Open nmtui
Run:
Step 2: Select “Set Hostname”
- Navigate using arrow keys and select “Set System Hostname”.
- Enter the new hostname and press OK.
Step 3: Apply Changes
Restart the system or NetworkManager for changes to take effect:
Alternative Commands
Use nmcli (Network Manager Command Line Interface):
Changing Hostname by Editing /etc/hostname Manually
If hostnamectl
is unavailable, manually edit the configuration file.
Step 1: Open the Hostname File
Edit /etc/hostname
using a text editor:
Step 2: Change the Hostname
- Delete the existing hostname.
- Type the new hostname.
- Save the file (Ctrl + X → Y → Enter).
Step 3: Edit /etc/hosts
To avoid DNS issues, update /etc/hosts
:
Find the line:
Change it to:
Step 4: Apply Changes
Restart the system for changes to take effect:
Setting a Temporary Hostname (Lost After Reboot)
To change the hostname temporarily (until next reboot), use:
To verify:
After a reboot, the original hostname will be restored.
Alternative Commands
Another way to set a temporary hostname:
Changing Hostname in Debian-Based Systems (Ubuntu, Debian)
For Debian-based distributions, the following method works:
Step 1: Edit the Hostname File
Replace the old name with the new hostname.
Step 2: Edit Hosts File
Change:
To:
Step 3: Restart the System
Changing Hostname in RHEL-Based Systems (CentOS, Fedora, Rocky Linux)
For Red Hat-based distributions, use:
Then verify:
If hostnamectl
is unavailable, manually edit /etc/hostname
and /etc/hosts
, then reboot.
Changing Hostname Without Rebooting
Instead of rebooting, restart the systemd-hostnamed
service:
Or restart the shell session:
This applies changes without requiring a full restart.
Common Errors and Troubleshooting
Error: Hostname Does Not Change After Reboot
- Ensure
/etc/hostname
and/etc/hosts
contain the correct hostname. - Restart
systemd-hostnamed
:
Error: Command Not Found
- If
hostnamectl
is missing, update system packages:
or
Error: SSH Issues After Changing Hostname
- Restart the SSH service:
- Update the SSH config if needed.
Summary: Best Methods to Change Hostname
Conclusion
The process of changing hostnames in Linux exists as multiple methods which include hostnamectl, nmtui, nmcli, and manual config file editing. The available methods for changing the hostname perform best based on system requirements alongside user choice. The recommended method for hostname modification is hostnamectl because it provides simple and persistent functionality. Manual editing of system configuration provides advanced customization but quick hostname alterations can be done through temporary methods that do not require device restarts. Users who implement the guide’s instructions can accomplish hostname modifications on different Linux distributions which results in improved system recognition capabilities and network setup.