How to Change Hostname on CentOS 7?

Try this guide with our instant dedicated server for as low as 40 Euros

How to Change Hostname in Centos 9 (1)

In a typical network, you have several servers and devices connected to each other and the outside world.

A hostname is simply the name by which a server is identified on a network. As you can imagine, assigning a unique hostname to each server is a good practice so that anybody on the network can quickly identify and connect to a specific server on the network.

Given this, the system administrators must know how to change the hostname on CentOS 7 servers.

Why Assign Hostnames to Servers in the First Place?

Before going into the details of changing the hostname on CentOS 7 servers, it is important to understand why you need to go through the exercise of assigning additional identifying information to servers.

On a network, each device or server is assigned a unique IP address that is used to identify it on the network. Every packet moving through the network has an IP address as its destination and point of origin.

So, why should you go through the additional step of assigning a hostname to the nodes on the network?

While IP addresses are for machines, hostnames are for humans who find it difficult to remember long strings of numbers. That’s why hostnames are unique identifiers that sysadmins and humans use to identify servers on any network.

Important: While it is fine to assign just a hostname (myServ) to a server on an intranet, you need to assign a hostname and a domain (myServ.myDomain.com) to servers connected to the internet.

The combination of hostname and domain is known as a Fully Qualified Domain Name (FQDN). Note that CentOS 7 fully supports FQDN.

Prerequisites

  • A server running CentOS 7
  • Access to a user account with root privileges
  • A text editor

When finalizing a hostname, you need to make sure that it is unique on the network. In addition, you should also observe the following:

  • Hostnames should be a combination of lower-case letters and numbers
  • Only periods and hyphens are allowed as separators
  • The length of the hostname should be between 2 and 63

Change Hostname on CentOS 7 Servers

Now that you have a good idea of what hostnames are and what precautions you should take while finalizing hostnames for the CentOS 7 servers on your network, it is time to jump into the details of the process.

Step 1: Find Existing Hostname

Before changing anything, you need to find the current hostname. This is an important step because you want to avoid duplication when changing hostnames.

Launch the terminal and use the following command to find the hostname and related information:

hostnamectl

This will print the hostname and additional information such as the OS, kernel version, and the hardware architecture.

Step 2: Set a New Static Hostname

Now, you can set the new hostname. We assume that you have finalized a new hostname that conforms to the requirements we mentioned above.

In the terminal, enter the following command:

hostnamectl set-hostname <new hostname>

Alternative Step 2: Change Hostname on CentOS 7 by Editing the /etc/hosts File

Another way of changing the hostname on CentOS 7 is directly editing the /etc/hosts file. This file contains the IP address and the hostname currently in use.

We suggest opening this file in your favorite text editor. Before this, make sure you have superuser privilege so that you can access and edit the file. We prefer Vim so we’ll use the following command in the terminal:

sudo vi /etc/hosts

In the text editor, look for the line that begins with 127.0.0.1 (or the IP address that refers to the system you are working on).

Change the entry by replacing the current hostname with the new one.

Save the file and exit.

Check If The New Hostname is Active

Now that you have changed the hostname, check that the changes have taken effect.

Start by rebooting the server. This ensures that the /etc/hosts file is reloaded.

Next, launch the terminal and enter hostnamectl. This will give the details of the current hostname and you could verify that the server has the new hostname.

Conclusion

Hostnames are very important in network administration because they are used by admins and users in identifying the right server on the network. We mentioned two ways you can change the hostname of the servers on your network.

In case you need help with this, let our expert managed bare-metal and dedicated server hosting engineers know and they’ll fix the new hostname in no time.

Try this guide with our instant dedicated server for as low as 40 Euros