How to Change or Set Hostname on CentOS 8/RHEL 8?


Introduction

In computer networking, a hostname is a label that is used to identify devices connected to a network. It is essentially a human-readable name assigned to a device, which can be used instead of its IP address.

Hostnames are important because they make it easier for users on the network to access other devices by name rather than having to remember the IP addresses of each device. Hostnames are also crucial for network communication and resource sharing.

When two devices communicate on a network, they typically use their hostnames to identify themselves rather than their IP addresses. This makes it easier for them to find and communicate with each other.

Checking the Current Hostname

Before changing the hostname, it is important to know what the current hostname is. This can be done easily on CentOS 8 / RHEL 8 by using the following command: ``` hostnamectl ```

The output of this command will show you the current hostname, along with other system information such as operating system version, kernel version, and architecture. For example, if the current hostname was set to "oldhostname", running the above command would produce output similar to this −

Static hostname: oldhostname Icon name: computer-vm 
Chassis: vm Machine ID: 4fd5f58XXXXXXXXXXXXXXXXXXXXXXXXXXXXX 
Boot ID: 06a7ee1XXXXXXXXXXXXXXXXXXXXXXXXXXXXXXX Virtualization: kvm 
Operating System: CentOS Linux 8 (Core) CPE OS Name: cpe:/o:centos:centos:8 
Kernel: Linux 4.18.0-80.el8.x86_64 Architecture: x86-64  

The most important piece of information in this output is "Static hostname", which shows you what the current hostname is set to. The other information is useful for understanding more about your system's configuration.

Changing the Hostname Temporarily

It is sometimes necessary to change the hostname of a system temporarily, for example, during testing or when connecting to different networks. The following command can be used to change the hostname temporarily −

sudo hostname newhostname

The newhostname argument should be replaced with the desired temporary hostname. This command changes the hostname only until a system reboot, after which it will revert back to the original one. The temporary change in hostname affects only that system and does not propagate through the network.

This means that other systems on the network will still see and communicate with this system using its original (permanent) hostname. Therefore, in a networked environment, it is recommended to use permanent hostnames and avoid relying on temporary ones.

If you need to set up multiple hostnames for a single system or want your temporary changes to persist through reboots, you can also modify your /etc/hosts file by adding an extra entry for your newhostname alias. However, it is recommended not to rely heavily on this method as modifying /etc/hosts files can lead to inconsistencies across various systems.

Changing the Hostname Permanently

Editing /etc/hostname File

The first step in changing the hostname permanently on CentOS 8 / RHEL 8 is to edit the "/etc/hostname" file. This file contains only one line, which represents the current hostname.

$ sudo vim /etc/hostname

Use a text editor such as Nano or Vim to modify this file and replace the old hostname with your desired new hostname. Be sure to save and close the file once you have made your changes.

Editing /etc/hosts File

The second step in changing the hostname permanently on CentOS 8 / RHEL 8 is to edit the "/etc/hosts" file. This file maps hostnames to IP addresses and is used by various system utilities to resolve hostnames locally.

$ sudo vim /etc/hosts

Open this file in a text editor, locate the line that contains your old hostname and replace it with your new one. Be sure to save and close the file once you have made your changes.

Restarting Systemd-hostnamed Service

After editing both files, it's important to restart the systemd-hostnamed service for your changes to take effect. Use this command: "systemctl restart systemd-hostnamed" This will reload all configuration files related to hostnames including those you just edited, ensuring that everything is up-to-date.

systemctl restart systemd-hostnamed 

Verification of new Hostname Using "hostnamectl" Command

Use the "hostnamectl" command to verify that your new hostname has been successfully applied. This command displays the current system hostname along with other relevant details such as operating system and kernel version.

You should see your new hostname listed here if everything was done correctly. Changing or setting a hostname on CentOS 8 / RHEL 8 may seem like a daunting task, but it's actually quite straightforward when you follow these simple steps.

By editing the "/etc/hostname" and "/etc/hosts" files, restarting the systemd-hostnamed service, and verifying your changes with the "hostnamectl" command. You can ensure that your new hostname is properly set up to be used in local network and internet.

Troubleshooting Common Issues

A World of Problems, or So it Seems

While changing or setting a hostname on CentOS 8 / RHEL 8 may seem like an easy task, a seemingly small mistake can cause major issues. This section will discuss some of the common problems that arise during this process and offer solutions to overcome them.

Permission Denied Errors

One of the most common errors during editing files is "Permission Denied." When trying to edit system files such as /etc/hostname and /etc/hosts, it requires root privileges to make changes. The solution is to use the "sudo" command before each change.

For instance, if you are using vi editor, you need to run it as sudo by typing "sudo vi ". This provides temporary superuser privileges for that specific operation.

The Hostname Didn't Change

Another issue that arises after changing or setting a hostname on CentOS 8 / RHEL 8 is when network connectivity fails. To fix this issue −

  • Check your DNS settings−

DNS configurations must match with the domain name given in the new hostname. If your DNS doesn't have an entry for your new hostname, you'll need to update it.

  • Restart networking service −

After making changes in configuration files and DNS settings, restart networking services using this command: "systemctl restart network". This should renew the IP address and refresh connections.

  • Reboot the system −

Each potential problem mentioned in this article has a solution that can be applied with relative ease if one knows what they are doing. By diligently following these suggestions step-by-step while troubleshooting common issues after changing or setting a hostname on CentOS 8 / RHEL 8, you can ensure that everything goes smoothly.

Conclusion

In this article, we have explored the steps involved in changing or setting a hostname on CentOS 8 / RHEL 8. We learned how to check the current hostname and change it both temporarily and permanently. We also discussed possible issues that may arise during the process and their solutions.

Having a proper hostname is crucial for easy identification of hosts on a network. It helps in establishing connections with other devices and services and aids in troubleshooting network issues that may arise. We hope this article helps you to change or set your hostname without any difficulty.

Updated on: 24-Aug-2023

398 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements