How to Change Hostname in CentOS/RHEL 8?


Introduction

The hostname is a label assigned to a device on a network that identifies it uniquely. In CentOS/RHEL 8, the hostname plays an essential role in identifying servers and their purpose.

A hostname is used to identify servers by both people and other machines on the network. By default, CentOS/RHEL 8 assigns a random hostname during installation, which may not reflect the server's function or location; hence it's important to change it.

The Benefits of Changing Hostname

Changing your server's name can bring many benefits beyond just better organization and identification of servers within a network. Once you set up meaningful names for each server, you'll be able to track its usage patterns more easily and make better-informed decisions about how resources are allocated.

Additionally, with better organization comes improved collaboration among team members who will be able to more readily share information across different departments or teams without confusion or delay caused by unclear naming conventions. Overall, assigning relevant names that represent your server's function or location is an excellent way to make it easier for you and your team to manage your systems efficiently and securely.

Checking Current Hostname

Before changing the hostname in CentOS/RHEL 8, it's important to check the current hostname to ensure that you are modifying the correct server. Checking the hostname can be done easily using the command line. To check your current hostname, open a terminal window and type in "hostname" command followed by Enter.

This will display your current hostname on the screen. The output of this command will be a single word that represents the name of your computer.

# hostname centos-8.example.com  

As seen from this example, our current host name is centos-8.example.com. If you need more information about your machine's specific network settings or IP address, you can use additional commands like `ifconfig` or `ip addr show`.

It's important to note that some hosting providers may have their own custom methods for checking hostnames which vary depending on which platform they use. Make sure that you follow any specific guidelines provided by those hosting providers as well.

The Power of Temporariness

It may be necessary to change the hostname temporarily in certain situations, such as testing out a new name before making it permanent. This can easily be done using the "hostname" command on CentOS/RHEL 8.

Step-by-Step Instructions

To change the hostname temporarily, first open a terminal window on your server. Then, enter the following command −

$ sudo hostname "new_hostname"

Replace "new_hostname" with the desired temporary hostname you'd like to use. This command will change the hostname for your current session only and will not be saved permanently. To verify that the new temporary hostname is in effect, issue this command −

$ hostname

Potential Issues and Troubleshooting

If you encounter issues after changing the hostname temporarily, there are several things to keep in mind. One potential issue is that some applications might have problems resolving IP addresses if they are using your old hostname. To fix this issue, you may need to update your /etc/hosts file so that it maps your new temporary hostname to its IP address.

You can do this by opening up /etc/hosts in a text editor and adding an entry for your new temporary hostname −

127.0.0.1   localhost.localdomain localhost 

::1 localhost6.localdomain6 localhost6 192.168.x.x mynewhostname

Make sure you replace 192.168.x.x with the actual IP address of your server and mynewhostname with your new temporary hostname. If you're still encountering issues after making changes to /etc/hosts file or if other issues arise, always remember that you can revert back to your original hostname at any time by using the same "hostname" command and specifying your old hostname.

Changing Hostname Permanently

If you want to change the hostname permanently in CentOS/RHEL 8, you will need to edit the /etc/hostname file. This file contains the current hostname of your system. You will need to replace it with your desired hostname.

Finding the Hostname File

The /etc/hostname file is located in the root directory of your system. You can use any text editor to modify this file, but we recommend using a terminal-based editor like vim or nano. To open this file in nano, type −

sudo nano /etc/hostname

You should see the current hostname of your system listed inside this file. Replace it with your desired hostname, then save and exit the file.

Applying Changes and Troubleshooting Potential Issues

After editing the /etc/hostname file and saving it, you will need to apply these changes by running a command that restarts both networking and systemd-hostnamed services −

sudo systemctl restart systemd-hostnamed.service && sudo systemctl restart NetworkManager.service

If everything goes well, you have successfully changed your host name permanently.

Potential Issues That may Arise During Permanent Change Process

Changing a hostname permanently can sometimes cause some unintended consequences that may affect other services on your system. If you encounter any issues after changing the hostname permanently - such as network connection problems or errors while accessing certain applications - it's possible that these problems are related to SELinux permissions not being updated for new hostname yet. To fix this problem execute following commands −

$ sudo semanage fcontext -a -t hostname_exec_t "/usr/bin/hostnamectl" 
$ sudo restorecon /usr/bin/hostnamectl

If this doesn't fix the issue, try rebooting your system. If the problem persists, revert to the old hostname and seek additional support from experts.

Updating SELinux Policy

The Importance of Updating SELinux Policy After Changing a Hostname

SELinux (Security Enhanced Linux) is a security module present in CentOS/RHEL 8 that provides mandatory access control for processes and files. When you change the hostname of your server, the SELinux policy may not recognize the new hostname and deny access to certain files or processes.

This can cause errors and make it difficult to use your server properly. Therefore, it's important to update the SELinux policy after changing a hostname.

How to Update SELinux Policy Using the "semanage" Command?

To update the SELinux policy after changing a hostname, follow these steps −

  • Open your terminal or SSH client and log in as root.

  • Run the following command to install semanage −

yum install policycoreutils-python-utils
  • Run the following command to check if selinux-policy-targeted is installed −

yum list installed selinux-policy-targeted
  • Run the following command to modify the existing network interface with your new hostname −

semanage fcontext -a -t net_conf_t '/etc/sysconfig/network-scripts/ifcfg-{your-new-hostname-here}'

Note − Replace {your-new-hostname-here} with your actual new hostname.

  • Run the following command to apply changes made by semanage −

restorecon -vR /etc/sysconfig/network-scripts/
  • Restart networking services using one of the following commands based on which version is installed −

For CentOS 8

systemctl restart NetworkManager.service

For RHEL 8

systemctl restart network.service

After updating the SELinux policy, you can verify that everything is working correctly by running a test command or trying to access files/processes that were previously denied. If there are still issues, it may be necessary to troubleshoot further or seek professional support. But by following these instructions, you'll have successfully updated your SELinux policy after changing your hostname in CentOS/RHEL 8.

Conclusion

In this article, we have explored the reasons why changing the hostname is important in CentOS/RHEL 8. We have established that a hostname is a unique identifier that helps us organize and identify our servers. By following the steps outlined in this article, you can ensure that your server's hostname is accurately reflecting its purpose and improving your server management.

During the process of changing the hostname, there may be potential issues. However, we have provided troubleshooting tips for any situation that may arise such as incorrect syntax or difficulties updating SELinux policy. By following these guidelines, you can quickly mitigate any problems that may occur.

Updated on: 24-Aug-2023

669 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements