How to Set or Change Hostname in CentOS 7?


The hostname plays a crucial role in identifying a server on a network and generating the fully qualified domain name (FQDN) for the system. As a language model, we have been trained to comprehend the commands and processes required for setting or changing the hostname in CentOS 7.

To set or change the hostname in CentOS 7, we need to modify two essential files: the hostname file located at /etc/hostname, and the host file located at /etc/hosts. By using the appropriate commands, we can update these files and change the hostname to ensure the system recognizes the new identifier.

Furthermore, rebooting the system after making these changes is essential to ensure they take effect. By following these steps, we can effortlessly set or change the hostname on our CentOS 7 system and ensure proper identification of our server on the network.

Step 1  Check the Current Hostname

To check the current hostname of our CentOS 7 system, we typically begin by opening a terminal window. we accomplish this by clicking on the terminal icon located in the applications menu. Once the terminal window is open, we then enter the following command to retrieve and display the current hostname 

hostname

This will display the current hostname of the system −

[root@server ~]# hostname
server.tutorialpoint.com

Checking the current hostname is important to ensure that our system has the correct system identifier and FQDN for networking and communication with other systems on the network. If we need to change the hostname then we can use the current hostname as a reference point to ensure that we enter the correct new hostname.

Step 2  Change the Hostname

In this step 2, we will edit the hostname on our system. The hostname is an essential system identifier that uniquely identifies a server and also gives a fully qualified domain name (FQDN).

To modify the hostname, we need to edit the hostname file that is located at this location /etc/hostname in our system and it may also be different in some cases. This file contains the current hostname of that particular system and we will change it with the new hostname we want to set.

Then, we open a terminal and write the following command −

sudo vi /etc/hostname

The vi command is the name of the text editor we'll be using, but if you prefer a different editor, you can use that instead of this editor.

After running this command, the vi editor will open the hostname file. we move the cursor to the end of the existing hostname using the arrow keys and then write the new hostname that we want to use.

Assuming we wish to modify the hostname from server.tutorialpoint.com to webserver.tutorialpoint.com we will make changes to this file that will appear as follows 

webserver.tutorialpoint.com

To change the hostname, we modify the hostname file at /etc/hostname. Using the vi editor, we save the file and exit by pressing the Esc key and typing:wq. Afterward, we update the host file so that other systems can correctly resolve the new hostname to the associated IP address.

Step 3  Update the Hosts File

In the third step, we will update the host file, which can be found at /etc/hosts. The host's file is a plain text file that maps hostnames to IP addresses. It serves as a backup for DNS, allowing the system to resolve hostnames to IP addresses even when DNS is not available. Our goal in this step is to add the new hostname that we set in Step 2 to the host file.

To update the host file, we opened a terminal window and executed the following command −

sudo vi /etc/hosts

We opened the host file using the vi editor, which contains a list of IP addresses and their corresponding hostnames on separate lines. The IP address is listed first, followed by a space, and then the hostname.

To add the new hostname, we located the line starting with 127.0.0.1, which maps the hostname localhost to the loopback address and should not be modified. we then added the new hostname after this line, separated by a space. For instance, if the new hostname is webserver.tutorialpoint.com, the updated line would be

127.0.0.1 localhost.localdomain localhost webserver.tutorialpoint.com

After changing the host file with the new hostname, we saved the changes and closed the editor. This step is crucial as it allows the system to accurately map the new hostname to its IP address and especially in networked environments where hostnames are used for communication between systems with each other.

Step 4  Reboot the System

Once modifications have been made to the hostname file and host file, it is crucial to reboot the system in order to implement the changes. Rebooting the system initiates a reload of the network configuration, causing the updated hostname from the modified files to be read.

To reboot the system, one can execute the following command in a terminal window 

sudo reboot

When you type the command to reboot the system, you'll be prompted to enter your password as this requires administrative privileges. Once you've entered the correct password and then the system will begin to shut down.

During the shutdown process, all running processes are stopped and all file systems are unmounted. This secures that any unsaved data is properly saved to disk. After everything has been stopped and unmounted, the system will power off.

Once you turn on the system again, it will go through the boot process and load the updated hostname. To check that the new hostname has been successfully applied, you can open a terminal window and type this command −

hostname

This will display the new hostname for the system. For example −

[root@webserver ~]# hostname
webserver.tutorialpoint.com

It's worth noting that rebooting the system will also cause any currently logged-in users to be logged out. If you have any important work or unsaved data, be sure to save it before rebooting.

Conclusion

To sum up, the process of changing the hostname in a CentOS 7 system is simple, easy, and requires only a few steps. Choosing a proper hostname can make server management easier and prevent clashes with other systems on the network. A clear and proper hostname can also enhance server security by making it more difficult for unauthorized users to gain access. Moreover, it can help in troubleshooting network issues by providing a distinct and identifiable name for the server.

Overall, we hope that this article has provided helpful guidance on updating or setting a new hostname in a CentOS 7 system. By following the steps outlined, you can easily improve the management and security of your server.

Updated on: 27-Jul-2023

852 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements