In Linux, a hostname is a unique identifier that helps differentiate systems on a network. Whether setting up a new Linux server or managing a Linux system in a large network, changing the hostname can be crucial for organization and identification. Essentially, hostnames serve a similar purpose to domain names and allow a machine to be identified without memorizing IP addresses. In this guide, we’ll cover different methods to change the hostname on Linux distributions, allowing you to manage your system’s identity easily.
How to check the current hostname in Linux
Knowing your system’s current name before modifying the hostname is helpful. This ensures you’re making changes to the correct settings, especially in multi-system environments. To view your original hostname, use the following command:
hostname
This command will display the hostname in a simple format. Alternatively, for a more detailed view, you can use:
hostnamectl status
The hostnamectl status command provides additional information, including the static hostname, any transient hostname (assigned temporarily by the network) and other relevant details. With this information, you’ll be ready to modify the hostname in Linux. Let’s explore the various methods to do so.
Methods to change Linux hostname
There are multiple ways to change the hostname on Linux, depending on your distribution and preference for command-line or graphical interface.
Note: Valid hostnames are between 2 and 64 characters in length. They can contain only letters, numbers, periods and hyphens, but they only begin and end with letters and numbers.
Using the hostnamectl command
Modern Linux distributions come with systemd, a system and service manager that includes the hostnamectl command by default. This command allows for easy management and modification of system hostnames.
Steps to Change the Hostname with hostnamectl:
- Open the Terminal: Start by opening the terminal on your Linux machine. If you’re working on a Linux server, log in via SSH.
2. Check for systemd: While most distributions come with systemd pre-installed, you can install it manually if necessary. Use the following command and press Enter:
sudo apt-get install systems
- View the Current Hostname: To see the existing hostname on your system, type:
hostnamectl
This command will output detailed information about your system, including the static hostname. For example:
Static hostname: hostinger
Icon name: computer-vm
Chassis: vm
Machine ID: 45598cbdb6ee462e8696166b520fe788
Boot ID: 99526e56aeea45c2a0f3b2ffaaffe9d9
Virtualization: openvz
Operating System: Ubuntu 22.04.4 LTS
Kernel: Linux 5.15.0
Architecture: x86-64"
In this output, the static hostname is listed as “hostinger”.
- Change the Hostname: To change the current hostname to a new one, use the following command, replacing the hostname permanently with your desired name:
sudo hostnamectl set-hostname new-hostname
Note: This command doesn’t produce any output, so verifying the change afterward is important.
- Verify the New Hostname: To confirm that the hostname has been updated, run the following command again:
hostnamectl
The output will now display the new hostname, confirming the change.
Using hostnamectl to modify the hostname ensures that it is changed permanently across reboots, making it one of the most reliable methods on modern Linux distributions with systemd. This approach is ideal for users who want a straightforward command-line method to set a new hostname permanently without editing configuration files manually.
Using GNOME settings
For users who prefer a graphical approach, GNOME settings make it easy to change system hostname on Linux distributions with a GNOME desktop environment.
Steps to change the Hostname:
- Open “Settings” in the GNOME menu.
2. Go to the “About” section.
3. Locate the hostname field, click on it and input your new hostname.
4. Save the changes and exit.
Using the Hostname Command
The hostname command is a quick method to temporarily or permanently configure the hostname in Linux.
Steps to Change Hostname with the Hostname Command:
- Open a Terminal: Launch the terminal on your Linux system. If you’re connected to a remote server, log in via SSH.
2. View the Current Hostname: Type the following command to see the current hostname:
3. Temporarily Change the Hostname: To modify the hostname temporarily, use:
Replace newhostname with your desired name. Note that this change will reset after a reboot.
- Make the Change Permanent:
- Edit the /etc/hostname File: Open the /etc/hostname file:
Replace the old hostname with the new one, save and exit.
- Update the /etc/hosts File: Update the hostname in /etc/hosts as well:
Find the line with 127.0.1.1 and replace the old hostname with the new one. Save and exit.
By completing these steps, the hostname change becomes permanent, even after reboots.
Using the Network Manager Text User Interface (nmtui)
The Network Manager Text User Interface (nmtui) is a convenient tool to configure network settings and hostnames through a text-based interface, available on various Linux distributions like CentOS and RHEL.
Steps to Change Hostname with nmtui:
- Open the Terminal: Start by opening the terminal and logging into your account via SSH.
- Install the NetworkManager-tui Package: If not already installed, add the NetworkManager TUI package with:
sudo yum install NetworkManager-tui
Type y to confirm installation if prompted.
- Check Network Manager Status: Confirm the status with:
service NetworkManager status
- Start the Network Manager: Use this command to activate it if needed:
service NetworkManager start
- Launch nmtui: Open the TUI by typing:
sudo nmtui
- Set System Hostname: In the nmtui window, select “Set system hostname”, enter the new hostname and choose “OK”.
The hostname is now updated, making nmtui an intuitive choice for users who prefer a visual interface over command-line input.
Using Network Manager CLI
The Network Manager Command Line Interface (nmcli) is a powerful option to configure hostname on Linux, especially for RedHat and CentOS systems.
Steps to Change Hostname with nmcli:
- Open the Terminal: Access the terminal. If you’re on a server, connect via SSH.
2. Check the Current Hostname: View your current hostname with:
nmcli general hostname
- Set a New Hostname: Change the hostname instantly with:
sudo nmcli general hostname newhostname
Replace newhostname with your desired hostname.
- Verify the New Hostname: Use the hostname command to confirm. If it hasn’t updated, restart
sudo systemctl restart systemd-hostnamed
Using nmcli is effective and doesn’t require a reboot for changes to take effect, making it ideal for quick adjustments.
Editing the configuration files
Editing configuration files is a more advanced but effective way to ensure the hostname change persists across reboots.
- Open the /etc/hostname file in a text editor:
sudo nano /etc/hostname
- Replace the old hostname with the new hostname and save the file.
- Next, edit the /etc/hosts file:
sudo nano /etc/hosts
- Update any reference to the old hostname to match the new one.
Editing these files manually allows for more control, especially in Linux servers and environments with custom configurations.
How to change hostname without rebooting
In certain situations, you may need to change your Linux system’s hostname temporarily without rebooting. This is useful when you want to make minor adjustments that don’t require a permanent change. Keep in mind, however, that this method only updates the hostname until the next reboot. To make a persistent change, consider using hostnamectl or editing the configuration files directly.
Step 1: Open the Terminal
To start, open your terminal. You can usually do this by pressing Ctrl + Alt + T on most Linux systems.
Step 2: Change the Hostname Temporarily
To change the hostname without restarting, use the following command format:
sudo hostname new-hostname
Replace new-hostname with the desired name you want to assign temporarily to your system. For example, if you want to set the hostname to “hostinger,” type:
sudo hostname hostinger
This command will immediately update the hostname for the current session. However, remember that this change is not permanent and will revert back after a reboot.
Step 3: Verify the Hostname Change
To confirm the hostname change, use the hostname command by typing:
Hostname
The output should now display the updated hostname. This confirmation lets you know the temporary change was successful.
Verifying the changes
Once you’ve changed the hostname, you must confirm it to ensure all settings are correctly applied. Use the following commands:
hostname
or
hostnamectl status
These commands will display the current hostname, allowing you to verify that the new hostname has been set successfully.
Troubleshooting common issues
Changing a hostname in Linux can sometimes cause unexpected issues, particularly on servers or systems with domain dependencies. Here’s a breakdown of common problems you might encounter and their solutions:
Issue 1: SSH Connection Problems
After changing the hostname, you might find that certain network commands, such as SSH, don’t work as expected. This can happen if the new hostname isn’t properly mapped to an IP address.
Solution:
To resolve this, update the /etc/hosts file with your new hostname. Make sure the entry for the hostname corresponds to the correct IP address. For example:
127.0.0.1 new-hostname
Updating this file ensures that the system can correctly resolve the hostname, allowing SSH and other network services to function smoothly.
Issue 2: Hostname Reverts to the Old Name After Reboot
Sometimes, after changing the hostname, you may notice that it reverts back to the old name upon restarting the system. This usually happens when the hostname change isn’t made permanent.
Solution:
To make the hostname persistent across reboots, update it in both the /etc/hostname and /etc/hosts files. Alternatively, you can use the hostnamectl command, which is designed for systemd-based Linux distributions and makes the change permanent. For example:
sudo hostnamectl set-hostname new-hostname
This command will ensure that the hostname remains the same even after a reboot.
Issue 3: DNS Resolution Issues
Changing the hostname on a server can sometimes disrupt DNS services, particularly if other systems or services rely on the previous hostname for domain resolution.
Solution:
If your server is part of a DNS-managed network, you may need to update your DNS records to reflect the new hostname. Contact your network administrator or adjust the DNS settings on your DNS server if you have access.
By addressing these issues proactively, you can avoid disruptions and ensure a smoother transition when changing your Linux hostname.
Conclusion
Modifying the Linux hostname is a straightforward yet essential task, whether for network management, server identification, or personal preference. With commands like hostnamectl and tools like Network Manager, you have a variety of methods to suit any Linux distribution. Now that you know how to change the hostname on Linux, you can manage your system’s identity more effectively.
FAQs
Yes, you can change the hostname without a reboot by using certain commands like hostnamectl or directly editing the /etc/hostname file. However, some systems may require a restart of network services to apply the changes fully.
Changing the hostname generally doesn’t affect your IP address directly, but it can impact network settings or DNS configurations if the hostname is tied to specific network services. It’s important to check your network configurations after making changes.
A static hostname is the permanent hostname saved on your system. The transient hostname is a temporary name that DHCP might assign and the pretty hostname is a user-friendly name that may include special characters or spaces.
Modifying the hostname typically requires root or sudo privileges, as the hostname configuration files and commands require elevated permissions to make system-level changes.
Yes, using short, alphanumeric names without special characters is recommended and avoiding starting the hostname with numbers. Keep it concise and descriptive, especially if the system is part of a larger network.