If you work with servers or have multiple computers on your home network, knowing how to rename the host in Linux is practically essential. A generic hostname like localhost might suffice initially, but as soon as you manage several machines, chaos ensues: duplicates, confusion when connecting via SSH, and difficulty identifying which is which.
In modern distributions, and even in older systems, there are several methods to change the hostname, both permanently and temporarily, without restarting . In the following sections, you'll see step-by-step how to do this using `hostnamectl` , text-based tools like `nmtui` , by modifying classic configuration files, or even by using a simple `hostname` command, as well as some specific details for CentOS, Debian, Ubuntu, RHEL , and derivatives.
What is the hostname in Linux and why should you change it?
In any Linux system, the hostname is the identifier by which the machine presents itself on the network. It can be a simple name like "server" or something more elaborate like srv-web-01.company.local , but it will always be the primary reference for other computers and services.
Typically, during distribution installation, a default hostname is assigned, often localhost or a generic name that doesn't reveal much. If you have multiple devices with the same hostname on the same network, you might encounter name resolution conflicts, services that don't respond where you expect, or simply a monumental mess when connecting via SSH or managing logs.
In production environments, it's common for each machine to have a name consistent with its function and location (for example, LINUX-SYSADMIT-01, db01, mail-srv, etc.). This simplifies administration, monitoring, and troubleshooting, and also avoids relying solely on IP addresses, which can change more easily.
It's also important to understand that a hostname is usually associated with a specific IP address . On local networks, this is often done using the /etc/hosts file , and on larger networks or the internet, using DNS. Therefore, when you change your computer's name, it's a good idea to check for any references on the system and network that might still point to the old name.
Checking the current hostname in Linux
Before changing anything, it's advisable to check the machine's current hostname . In Linux, there are several ways to do this, and all of them are very fast.
The most direct option is to use the hostname command in the terminal:
hostname
The output of this command simply shows the current hostname , for example:
linuxshelltips
If you want a bit more system information, especially on systemd- based distributions , you can use hostnamectl . It's a command-line utility that displays computer data and allows you to change the hostname.
hostnamectl
The output includes fields such as Static hostname , operating system, kernel version, virtual machine type , architecture, etc. The key data here is the Static hostname , which is the persistently defined name for the system.
If you want to see more "low-level" information, you can also consult some system files. The file /proc/sys/kernel/hostname reflects the hostname that the kernel is currently using:
cat / proc / sys / kernel / hostname
And, of course, in many distributions the /etc/hostname file permanently stores the configured hostname. You can view it with:
cat / etc / hostname
Change the hostname permanently in Linux using hostnamectl
In most modern distributions (such as Ubuntu 16.04 or higher, recent Debians, RHEL 7+, CentOS 7+ and derivatives) the init system is systemd , and with it comes the hostnamectl tool , which is the recommended way to permanently rename the host in Linux.
The first thing to do is make sure your system uses systemd. It comes standard on modern distributions, but if you're working with an older system, you might need to install it or use alternative methods. On Debian-based systems, you can install it (if it's not already there) with:
apt-get install systemd
To view the current hostname using hostnamectl, run:
hostnamectl
You'll see something like this (typical example on an Ubuntu VPS):
Static hostname: hostinger
Icon name: computer-vm
Chassis: vm
Machine ID: 45598cbdb6ee462e8696166b520fe788
Boot ID: 99526e56aeea45c2a0f3b2ffaaffe9d9
Virtualization: openvz
Operating System: Ubuntu 22.04.3 LTS
Kernel: Linux 5.4.0
Architecture: x86-64
To set a new permanent hostname, use the following syntax:
hostnamectl set-hostname NEW_HOSTNAME
For example, if you want to name your computer LINUX-SYSADMIT-01 , you would do the following:
hostnamectl set-hostname LINUX-SYSADMIT-01
This command usually displays nothing on the screen if executed correctly. To verify that the change has been applied, simply run the command again.
hostnamectl
and verify that the Static hostname field has been updated to your new name. You can also simply use:
hostname
To verify that the system is responding with the updated hostname. With this method, you don't need to restart in most cases, as systemd handles propagating the change.
Rename the host by editing the classic configuration files
In addition to systemd's specific tools, you can modify the hostname in virtually any distribution by directly editing configuration files . This method works even on older systems and gives you very fine control over what you change.
The first file to check is /etc/hostname , which usually contains a single line with the current hostname. To edit it, you can use vi, vim, nano , or your preferred editor. For example:
sudo vim /etc/hostname
Within the file, replace the current name with the new hostname you want to use, for example:
hostinger
Save the changes and exit the editor. You've now modified the base configuration, but there are a couple of key steps left to ensure the change is consistent across the entire system.
The next file to update is /etc/hosts , which is used to resolve hostnames to IP addresses locally . Open it with your usual text editor:
sudo vim /etc/hosts
Within this file there is usually a line similar to:
127.0.0.1 localhost old-hostname
or something equivalent. You must replace the previous name with the new one, for example:
127.0.0.1 hostinger
In environments where fully qualified domain names (FQDNs) are used, you may see entries like:
192.168.0.1 engelausmetall.engel.com engelausmetall
In this case, if you are going to change the hostname, you will need to adjust the name portion and, if applicable, the FQDN to maintain consistency with the network and services that use that name.
In many older configurations based on System V init , especially on Red Hat or derivative systems, the /etc/sysconfig/network file is also used to define the hostname. It is in this file that you will see a line declaring the HOSTNAME variable:
HOSTNAME=engelausmetall.engel.com
If this file exists on your system, edit it with:
nano /etc/sysconfig/network
and modify the HOSTNAME variable with the new name. This step is important to ensure the change persists after reboots , especially on older Red Hat distributions.
Once /etc/hostname, /etc/hosts , and, if applicable, /etc/sysconfig/network have been updated , it is common practice to restart certain network services or the system itself. Many manuals recommend, for example, the following for classic systems:
/etc/init.d/hostname.sh start
or restart the network service:
service network restart
After this, it's advisable to close the terminal session and open a new one, or even log out and log back in to the graphical environment. This will verify that the console prompt and applications now display the updated hostname.
Configure the hostname in Linux using nmtui (NetworkManager TUI)
Many Red Hat-based distributions, such as RHEL and CentOS , include a text-based interface for NetworkManager called nmtui (Network Manager Text User Interface) in addition to pure command-line tools. This utility allows you to configure network connections and adjust the system hostname from a text-based menu.
If you don't have it installed, you can add it on systems that use yum with the NetworkManager-tui package :
sudo yum install NetworkManager-tui
When the installer lists the dependencies and asks if you want to continue, answer with y and press Enter. When finished, you will see a message like " Complete!" , indicating that the package has been installed successfully.
Before using nmtui, make sure the NetworkManager service is running. You can check this with:
service NetworkManager status
If the service is not running, start it with:
service NetworkManager start
Now you can launch the interface in text mode by typing:
sudo nmtui
On the tool's main screen, you'll see several options, including "Set system hostname ". Use the arrow keys to navigate to this option, press Enter, and you'll access a dialog box where you can enter the new hostname you want to assign to your system.
Enter the new hostname, select OK , and confirm the changes. Once you exit nmtui, the system will apply the new name. You can verify this again with:
hostname
or, if your distribution uses systemd, with:
hostnamectl
Using cloud-init and preserving the hostname after reboot
Many VPS and cloud server images use cloud-init , a tool that configures the system on its first boot (and sometimes subsequent boots) based on templates provided by the vendor. This includes, among other things, the hostname.
If you manually change the hostname on a cloud-init managed server but don't adjust its configuration, it's possible that after restarting the original hostname from the template will be reapplied, losing your changes.
To prevent this, edit the main cloud-init configuration file, usually /etc/cloud/cloud.cfg . You can do this with:
sudo vim /etc/cloud/cloud.cfg
Within this file, look for the `preserve_hostname` directive . If it is set to `false` or does not appear, add or modify the line so that it looks like this:
preserve_hostname: true
This option tells cloud-init to respect the system's current hostname and not overwrite it on subsequent startups. After saving the file, restart the server to verify that the hostname is retained.
sudo reboot
Once it has restarted, run:
hostname
and verify that the name is still the one you configured. If your system does not use cloud-init or the /etc/cloud/cloud.cfg file does not exist, you can simply ignore these steps.
Changing the hostname in Linux without restarting the system
There are times when you only need to change the hostname temporarily , for example, to run specific tests or perform a particular task. In those cases, you can do it directly with the `hostname` command , keeping in mind that the change will not be permanent after a reboot.
The basic syntax of the command to assign a new name is:
hostname NEW_HOSTNAME
For example, if you want to temporarily name the computer "hostinger" , you would run:
hostname hostinger
This change takes effect immediately for the current system session. To verify that it has taken effect, you can restart:
hostname
You'll see the new hostname. However, as soon as you restart the machine, the system will revert to the permanent hostname configured in the configuration files or using `hostnamectl`, so don't replace the persistent configuration steps with this if you need the change to be permanent.
Additional considerations and best practices when renaming the host in Linux
Renaming the host is not just changing a string in a couple of files; it's worth checking some details to ensure everything works smoothly , especially on production servers or in corporate environments.
A first recommendation is to choose a clear and consistent hostname that aligns with your naming scheme. Many companies follow patterns like ROLE-LOCATION-NUMBER (for example, WEB-MAD-01, DB-BCN-02), which makes it easy to identify the function of each server simply by looking at its name.
It's also important to check if the hostname appears in scripts, services, certificates, or firewall rules . For example, some services might be configured to listen on a specific hostname or generate logs where the computer name is part of paths and files. After the change, it's a good idea to quickly review these points.
Another aspect to consider is the potential relationship between the hostname and DNS resolution . If your machine is registered with a DNS server under a specific name, changing its local hostname will also require updating the corresponding DNS record so that the logical name matches the IP address it resolves to.
Finally, remember to always check the /etc/hosts file to ensure that any references to the old name have been updated correctly. It's common to forget this step and then find that some services are performing inconsistent name resolutions because there are still old lines in that local file.


