How to Change Hostname on Ubuntu and CentOS

The hostname refers to the name assigned to every individual computer. It serves to differentiate and identify each computer on the network. Without a hostname, monitoring, managing, and communicating within the network becomes difficult. A hostname helps administrators or applications identify a specific computer within a networked cluster of systems. They often change hostname when configuring a new server, working on a VPS, or setting up servers during production.

Moreover, administrators usually configure a server’s hostname based on its function to make identification easier. For instance, it is common to see hostnames such as web-server01, database-server01, or backup-node. Without the proper configuration of the hostnames on the servers, it would be a difficult task to manage servers in a network where thousands of devices operate.

This guide will show you different ways to change the hostname in Ubuntu and CentOS. It will show you several methods so you can make a decision about what method to use for what circumstances.

How to Change Hostname on Ubuntu and CentOS

There are numerous methods of executing this task in modern Linux distributions. The most common of all is the hostnamectl command. Note that this command works only on systems based on systemd. Besides this method, administrators can also use other methods, such as changing certain configuration files, such as “/etc/hostname”, “/etc/hosts”.

It is crucial to find your current hostname, and it is essential to be clear about the task and the configurations. It will help to validate the changes. Therefore, you can use the command below to display the current hostname along with other system information.

hostnamectl

This command will show you the current hostname “ubuntu”, the version of the operating system, the version of the kernel, and the system architecture.

check hostname and other system info

If you only want to see the current hostname without other system information, you can run the following command.

hostname
check hostname only

The output of this command shows the current hostname assigned to the system.

The most preferable method of changing the hostname in the new versions of Linux is using the hostnamectl command. This works on Ubuntu 18.04 and above, CentOS 7 and above, and basically all distributions that use systemd.

Using hostnamectl changes the hostname instantly and keeps the change after the system reboots.

Step 1: Change the Hostname

To modify the system’s hostname with hostnamectl, execute the command below and replace new-hostname with the hostname you want to assign to the system.

sudo hostnamectl set-hostname new-hostname

So, for the case of changing the hostname to “web-server01”, the command would be as follows.

sudo hostnamectl set-hostname web-server01
change hostname with hostnamectl

Executing this command changes the hostname instantly.

Step 2: Edit the Hosts File

It is also advisable to edit the file “/etc/hosts” when changing the hostname. This is to avoid issues with hostname resolution when the “system/host” cannot resolve its hostname, since that file converts names to addresses. 

You can edit the hosts file with the following command.

sudo nano /etc/hosts

Inside that file, find the old hostname line. It would look like this.

127.0.1.1 old-hostname
check old hostname in etc hosts file

Replace the old hostname with the new hostname so that it looks like this.

127.0.1.1 web-server01
replace old hostname with new hostname

After the edit, exit the editor.

Step 3: Verify the Hostname Change

After changing the hostname, confirm that the system reflects the new name to ensure the change succeeded. To check the hostname, type the command below.

hostnamectl
Verify the Hostname Change

The new hostname should appear in the Static hostname section. To check the hostname, run the following command.

hostname
verify new hostname

If the system lists the new hostname, the modification succeeded.

Method 2: Modifying the /etc/hostname File 

On the Ubuntu operating system, the system stores the static hostname in the /etc/hostname file. You can modify this file to change the hostname.

Step 1: Access the Hostname Config File

To access the config file, use the following command by specifying the configuration file name.

sudo nano /etc/hostname

The file consists of only one line with the hostname that will be the existing hostname.

web-server01

Step 2: Enter New Hostname

To give the system a new hostname, edit the line and enter the text of the new hostname.

new-server
add new hostname in etc hostname

After updating the file, make sure to save and exit the text editor.

Step 3: Modify the Hosts File

The next step is editing the hosts file for the new hostname to take effect. You can do this with the command.

sudo nano /etc/hosts

Find the line with the old hostname and change it to the new hostname.

127.0.1.1 new-server
replace old hostname with new

After making the appropriate changes, save the file.

Step 4: Implement Changes 

After modifying the configuration files, you will have to restart the machine for all the services to verify the new hostname. The following command will initiate a restart.

sudo reboot

Once the system is back on or new session, you can verify the hostname with the command below.

hostnamectl
verify new name

Method 3: Change Hostname Using the hostname Command (Temporarily)

Administrators can change the hostname of a machine using the hostname command. This change is temporary because the system returns to the original hostname after a reboot.

Step 1: Check the Existing Hostname

To view the hostname of the machine, and not the other system information, one can use the command below.

hostname
Check the Existing name

The output shows that the system uses Ubuntu as its current hostname.

Step 2: Change Hostname Temporarily

Use the command below to temporarily change the hostname, replacing new-hostname with the desired name.

sudo hostname new-hostname

Change the hostname to test-server using the command below.

sudo hostname test-server
Change Hostname Temporarily

This will change the hostname to test-server, which will only be valid for the current session.

Step 3: Confirm the Updated Hostname

To verify the change of the hostname, run the command.

hostname

You should see the hostname displayed. Keep in mind that these changes will revert after a reboot unless you make a permanent change.

Confirm the Updated name

Guidelines to Change Hostname

For servers, administrators must assign hostnames that reflect the system’s role. When choosing a hostname, an administrator should:

  • Select a name that indicates what the server does, e.g., web-server, database-server, backup-node.
  • Static hostnames should be one continuous string with no spaces or special characters. 
  • Use lowercase letters and use hyphens instead of underscores.
  • Static hostnames must be unique within the network.

All these combined will simplify server management in large spaces and will help the role of the system be readily apparent.

Conclusion

Server identification and organization within a network is a key task when changing and configuring hostnames on Ubuntu and CentOS. Proper hostnames allow systems to communicate with each other and for applications and services to communicate with the systems.

Hostnamectl is the most preferred way of changing hostnames. The other ways include hostname, which makes a temporary change, and the manual way of changing hostname in files (i.e., editing /etc/hostname and /etc/hosts). Other ways assist the administrator so that the network of Linux systems is simple and orderly.

FAQs

What is the purpose of a hostname in Linux?
Is it necessary to restart the server after changing the hostname?
What is the easiest way to change the hostname?
What happens if we do not modify the /etc/hosts file?
Is it possible to change the hostname without having root access?
What problems would you encounter if two servers had the same hostname?
How was the hostname change successful?

Related Post

How to Install Laravel on Ubuntu 22.04

Laravel is a renowned open-source framework meant for P...

How to Install Apache Zookeeper on Ubuntu

Apache ZooKeeper Ubuntu is an open-source server that e...

7-How-to-Use-Powerlevel10k-to-Customize-Zsh

How to Use Powerlevel10k to Customize Zsh

Powerlevel10k is a Zsh theme that improves the speed an...

How to Install Redmine on Ubuntu 22.04

Redmine is a flexible project management web applicatio...

Install Wildcard SSL Certificate on Ubuntu 22...

Securing your website with an SSL certificate is import...

How to Uninstall or Remove Packages in Ubun...

Managing installed packages is essential for keeping yo...

Leave a Comment