Changing Hostname on Rocky Linux 8

On this short article we will learn how to change hostname on Rocky Linux 8.

Introduction

When we set the hostname of a server, usually the name of the server will represent a related entity, for example billsvr is the hostname for a billing server. As a sysadmin, we have to reset the hostname. As a sysadmin, we are usually asked to change the hostname if there is a need, for example when deploying from staging to production. In this article, we will discuss how to change the hostname on the Rocky Linux 8 operating system.

The steps to change the hostname are as follows :

  • Checking Current Hostname
  • Change Hostname
  • Update /etc/hosts
  • Confirm the Change

Checking Current Hostname

The first step to change the hostname of a server is to check the current hostname. This task can be performed by submitting command line hostname  or hostnamectl.

[ramansah@localhost ~]$ hostname
localhost

[ramansah@localhost ~]$ hostnamectl
   Static hostname: localhost.localdomain
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b94ae72b25a1402bab6e7762328b0138
           Boot ID: 152a2e9830ac44b592ef8892e822bab1
    Virtualization: vmware
  Operating System: Rocky Linux 8.4 (Green Obsidian)
       CPE OS Name: cpe:/o:rocky:rocky:8.4:GA
            Kernel: Linux 4.18.0-305.3.1.el8_4.x86_64
      Architecture: x86-64

Changing Hostname

On this scenario, we will change hostname from localhost.localdomain to otodiginet. We will use the hostnamectl command line.

$ sudo hostnamectl set-hostname otodiginet

The output is as shown below :

[ramansah@localhost ~]$ sudo hostnamectl set-hostname bckinfo
We trust you have received the usual lecture from the local System
Administrator. It usually boils down to these three things:

    #1) Respect the privacy of others.
    #2) Think before you type.
    #3) With great power comes great responsibility.

[sudo] password for ramansah: 

Update /etc/hosts file

The /etc/hosts file is used to resolve a name into an address. When our system is using a name server, the file is accessed only if the name server cannot resolve the host name.

[ramansah@localhost ~]$ sudo vi /etc/hosts

127.0.0.1 localhost.localdomain
::1       localhost.localdomain

to :

127.0.0.1 bckinfo bckinfo.com
::1       bckinfo bckinfo.com

Verifying The Hostname Change

To verify the hostname changes, we just use the hostnamectl command line again after we restart the server or restart the daemon server.

[ramansah@localhost ~]$ sudo systemctl restart systemd-hostnamed

Then we will use hostnamectl command line to verify.

[ramansah@localhost ~]$ hostnamectl
   Static hostname: bckinfo
         Icon name: computer-vm
           Chassis: vm
        Machine ID: b94ae72b25a1402bab6e7762328b0138
           Boot ID: 152a2e9830ac44b592ef8892e822bab1
    Virtualization: vmware
  Operating System: Rocky Linux 8.4 (Green Obsidian)
       CPE OS Name: cpe:/o:rocky:rocky:8.4:GA
            Kernel: Linux 4.18.0-305.3.1.el8_4.x86_64
      Architecture: x86-64

Conclusion

On this short article, we have shown you how to change the hostname of CentOS server.

Add a Comment

Your email address will not be published.