The hostname is a custom label assigned to a computer or server connected to the network so that it can be easily identified in a network for access and communication and to be distinguished from other devices. Hostnames are used in various fields of the network, including the Internet, in human-readable text format along with DNS to map to the device’s IP address. Setting Hostname is one of the steps of installing your operating system or configuring the server, which is specified by the user for the device or server.
Sometimes, duplicate Hostnames in the network cause network conflict, which forces the user to change Hostname. Of course, other reasons cause the need to change Hostname, mainly because of network disruption and failure to identify the computer system by hostnames.
In this article, you will learn how to change host names in popular Linux distributions such as Ubuntu, Debian, and CentOS in different ways. Stay with us until the end of this article.
Hostname types in Linux
Hostnames are customized names for servers and computers connected to the network, which are necessary for network management, access to devices on the network, and communication between devices. As an administrator of a network containing several servers or computers, identifying and managing computers without names isn’t easy. Therefore, computers in the network can be identified, managed, and distinguished by host names.
The Hostname is classified into different types based on their structure and use. In modern Linux distributions that are supplied with systemd, the Hostname is classified into three types: Static Hostname, Pretty Hostname, and Transient. Each type of hostname is part of the overall system configuration with a specific purpose, which we will talk about further:
The Static Hostname is the main hostname, which is set during system configuration or installation and remains constant unless you manually change it, which can be found in the /etc/hostname file. You can see the static hostname by running the
hostnamecommand in the terminal. This type of hostname is used to distinguish devices from others in different networks.
Pretty Hostname allows the use of special and non-standard characters to make hostnames user-friendly. Pretty Hostname is a more user-friendly version of Hostname that is chosen for display purposes. Unlike static Hostname, which is used to identify devices in the network, pretty Hostname is used as a label for the system user. Check the /etc/machine-info directory to find the current pretty Hostname.
Transient Hostname is used to set temporary hostnames for the system. When you need your system to be identified differently, you can use Transient Hostname temporarily. The Transient Hostname does not remain after reboot because it is dynamic. Therefore, Transient Hostname is a good solution for scenarios where you need to change Hostname in linux temporarily. Transient Hostname is stored in the Linux kernel.
When should we change Hostname in Linux?
Various reasons make us decide to change Hostname in ubuntu, debian and centos, but this action must be done thoughtfully and strategically because it affects multiple system management and performance aspects. But when should we think about changing the Hostname?
- Changing the network structure and the need to update the host names to match the new configuration
- Improving security and preventing the integration of unauthorized devices in the network
- Changing Hostname to distinguish the devices and prevent the host names from being the same for network management and troubleshooting.
- Changing the server role and the need to change the Hostname to reflect the purpose of the server (for example, changing the web server to a database server means changing the server role)
- When upgrading hardware
- Changing the Hostname due to cloned, migrated, or scaled up of instances in cloud and virtualization environments.
- Network troubleshooting to solve the problem of not recognizing the system in the network
- The need to change Hostname for the purpose of organization when merging or dividing services on one server or several servers.
- Naming conflicts in the network, which changing the hostname is the solution for this problem.
- Installing or upgrading some software that needs to change Hostname in Linux.
- Improved user experience
Prerequisite for changing Hostname in Linux
To follow the instructions in this guide, you need a server or Linux VPS running Ubuntu, Debian, or CentOS. In addition, executing the necessary commands to change the hostname requires root privileges. Also, your server must be equipped with a text editor such as Vim.
Permanent change Hostname in Ubuntu, Debian, and CentOS through terminal
In this part of the article, you will learn to permanently change the current hostname in the popular Ubuntu, Debian, and CentOS distributions through the terminal and using various methods, such as using the
nmtui, and editing configuration files.
Method 1: Changing the hostname in Linux using the hostnamectl (Without Reboot Required)
The hostnamectl command is one of the famous commands to change the hostname and related settings in modern Linux distributions that are managed by systemd. The first method in this article is to use the hostnamectl command, which is available by default in most Linux distributions. The commands you run are not different in Ubuntu and CentOS, the only difference is in using their package manager.
1. Installing systemd (if systemd is not available)
If you are not using a modern Linux distribution and your system is not managed by systemd, you need to install it to run the hostnamectl command:
apt-get install systemd
2. Checking the current hostname
Check the Current Hostname in ubuntu and centos by one of the following commands:
The previous command displays the Static Hostname, providing a brief and useful output. To get additional information about the Hostname and your device, you can run the following command:
Static hostname: OperaVPS Icon name: computer-vm Chassis: vm Machine ID: 45598cbdb6ee462e8696166b520fe788 Boot ID: 99526e56aeea45c2a0f3b2ffaaffe9d9 Virtualization: openvz Operating System: Ubuntu 20.04.3 LTS Kernel: Linux 5.4.0 Architecture: x86-64
As you can see, the output of the hostnamectl command, in addition to providing the current static hostname, displays additional information about your device.
3. Permanently changing Hostname
Set the new and fully qualified Static Hostname by running the following command:
hostnamectl set-hostname new-hostname
If the previous command is successful, you will not receive output.
In determining the new Hostname, ensure that the Hostname must be between 2 and 64 characters and can consist of lowercase letters (a to z) and numbers (0-9). Hostnames cannot start and end with numbers and must start and end with letters. In addition, you can use periods and hyphens in Hostname.
4. Confirm the change
Since you don’t get the output after setting a new Hostname, run the following command to ensure the Hostname is changed:
Finally, you can ensure setting your new hostname by checking the output showing the new hostname.
5. Changing the pretty Hostname (optional)
Pretty Hostname is not a name that belongs to a system or network but is assigned to a user. To change the pretty hostname in Ubuntu, Debian, and CentOS distributions, run the following command:
hostnamectl set-hostname "new-hostname" --pretty
Note: Do not forget to type quotation marks in the previous command.
To ensure that the pretty hostname of your computer is changed, you can run the following command:
In the output, you will see that the pretty hostname has changed, and if you had not set the pretty hostname before, now the line containing the pretty hostname you want has been added.
Method 2: Changing the hostname in Linux using the nmtui
The nmtui command stands for Network Manager Text User Interface. It is A simple, text-based interface for managing network connections using NetworkManager in Linux, which allows the user to set hostnames and manage network connections through a Linux terminal. nmtui is one of the useful commands for managing network settings and configuring network connections on servers. The nmtui command is supported by most Linux distributions; in the following, we will teach how to use the nmtui command to change the Hostname in CentOS.
1. nmtui package Installation
After connecting to the server via SSH, enter the terminal and install the NetworkManager-tui package and nmtui dependencies on CentOS by running the following command:
sudo yum install NetworkManager-tui
After loading packages, type Y and press Enter.
Display the “Complete!” indicates the successful installation of nmtui packages.
2. Checking the network manager
To ensure the successful installation of nmtui packages, check the status of the Network manager by running the following command:
service NetworkManager status
Redirecting to /bin/systemct1 status NetworkManager.service NetworkManager.Service - Network Manager Loaded: Loaded (/usr/lib/systemd/system/NetworkManager. service; enabled; vendor preset: enabled) Active: inactive (dead) Docs: man: NetworkManager (8)
3. Start the nmtui service
Now it’s time to start the nmtui service; for this purpose, run the following command:
service NetworkManager start
Then run the following command to use the nmtui facilities to change Hostname and launch the nmtui text-based interface:
4. Changing Hostname
Navigate through the available options in the TUI NetworkManager window, select the “Set system hostname” option, and press Enter.
Type your desired valid Hostname and select OK to apply the changes.
Changing Hostname in Ubuntu and Debian through nmtui command is the same as the steps we taught in CentOS, and the difference is in using their package manager.
Method 3: Changing the Hostname in Linux using Edit the Configuration File (Reboot Required)
So far in the article, you have learned how to change Hostname through
hostnamectlcommands in Linux; there is another method that is simpler than the previous two methods, you can permanently change the Hostname in Ubuntu, Debian, and CentOS distributions by editing the/etc/hosts and /etc/hostname files that contain static Hostname.
Therefore, to permanently change the hostname by editing the configuration files through your favorite editor, follow the steps below.
1. Open the /etc/hostname file
Fortunately, the /etc/hosts and /etc/hostname files can be edited; you must access the contents of these files using your favorite editor and change the old hostname to the new hostname. First, open the configuration files with the desired editor:
The /etc/hostname file contains the current static hostname; after opening the file, you can update the old hostname, save your changes, and exit the file.
2. Open the /etc/hosts file
The /etc/hosts file contains hostnames and IP addresses; similar to step 1, open the /etc/hosts file with your favorite editor and replace the old Hostname with the new Hostname.
sudo vi /etc/hosts
After changing the old hostname, save your changes and exit.
3. Edit the cloud configuration file
You successfully changed the Hostname on the network by editing the configuration files; you need to edit the cloud configuration file to apply the changes after the system startup. For this purpose, you must access the content of the cloud configuration file by running the following command and your favorite editor:
sudo vim /etc/cloud/cloud.cfg
4. Making changes in the content of the cloud configuration file
After opening the cloud configuration file, change the value of the line containing preserve_hostname to true:
5. Reboot system
Reboot the system to apply the changes:
sudo systemctl reboot
6. Confirm the change
To check that the changes you made are preserved after rebooting the system, you can run the following command:
In the output of the previous command, you will see a new Hostname, and you will make sure that the new Hostname is set for the system after starting the system. You can also open the /etc/hosts file using the editor of your choice and ensure that the Hostname does not change after the system reboot by viewing the new Hostname.
Permanent change HostName in Ubuntu, Debian, and CentOS via GUI
Many users prefer to use the GUI to apply their settings and other tasks; that’s why we will teach how to change Hostname in different Linux distributions, including Ubuntu and Debian, in this part of the article.
1. Open “Settings” and select ”About.”
2. Click the “Device Name field” to access the Rename Device dialog box.
3. In the Rename Device dialog box, type New Hostname instead of Current Hostname.
4. To apply the changes, click the “Rename” button to change the Hostname permanently.
Hostname change in Ubuntu and CentOS temporarily
Maybe you don’t need to change the Hostname permanently; in a temporary situation, if you want to apply changes that won’t be saved after rebooting the system, then what should you do? Fortunately, the flexibility of Linux distributions allows the user to do anything. You can temporarily change the Hostname in different Linux distributions, and your changes will not remain after rebooting the system. The following command is helpful for this purpose:
Replace your desired hostname in the new-host-name field.
The successful operation of this command is shown by not providing the output. To ensure the temporary change of Hostname, run the following command:
The output should display a new hostname.
This change is applied until the system is rebooted. After rebooting the system, the old hostname is set for your device in the network, and it is as if you did not change the hostname at all.
Yes, changing the Hostname affects services that rely on the Hostname for identification. Therefore, after changing the hostname, you need to update the configuration files and programs that depend on the hostname. In addition, systems that are part of the network domain must be reconfigured for network communication after changing the hostname.
The most common solution is to update the corresponding configuration files and services. If the problem is not solved by updating the configuration files, you can set the previous Hostname and troubleshoot the system and network.
Hostnames should be short, meaningful, and conform to DNS naming conventions. Using numbers, periods, and hyphens in Hostnames is allowed, but hostnames should not start and end with numbers and hyphens.
Setting valid and unique host names is important in network management, network troubleshooting, and identifying and accessing devices; a network administrator should be an expert in changing host names in case of problems. In this article, we taught how to change hostnames through the terminal and GUI and various methods in popular Linux distributions such as Debian, Ubuntu, and CentOS so that you can become more professional in network management. Now, you can update the Hostname of your system using any method you are comfortable with.
Our technical support team is here to answer your questions about changing the Hostname in Linux, so if you have any questions about setting the Hostname in Linux, ask in the comments section.