RHEL 8 Change Hostname (computer name) command

last updated in Categories ,

I am a new RHEL 8 user and setup a server. They setup my server name to ‘localhost’ during deployment but now I would like to change the computer name to something else. How can I remove old name and set it to ‘nixcraft-rhel8’ on Red Hat Enterprise Linux? Can you tell me how do I change the RHEL 8 computer name? How do I assign a static hostname to server running RHEL 8?

Introduction : For RHEL 8 you can use the hostnamectl command to change the hostname of an RHEL 8 server, laptop or desktop. You can use the hostname command to see or set the system’s hostname too. The hostname or computer name is usually at system startup in /etc/hostname file. Open the terminal application and type the following command to change the hostname on RHEL 8.

Display the current RHEL 8 hostname

Type any one of the following command:
$ hostname
OR
$ hostnamectl
RHEL 8 show or display current hostname

RHEL 8 change hostname command

The procedure to change the computer name on OpenSUSE Linux:

  1. Type the following command to edit /etc/hostname using nano or vi text editor:
    sudo vi /etc/hostname
  2. Delete the old name and setup new name.
  3. Next Edit the /etc/hosts file:
    sudo vi /etc/hosts
  4. Replace any occurrence of the existing computer name with your new one.
  5. Reboot the system to changes take effect:
    sudo reboot

How to change the RHEL 8 server hostname without a system restart

Type the following commands:
$ sudo hostname nixcraft-rhel8
Next edit the /etc/hostname file and update hostname:
$ sudo vi /etc/hostname
Finally, edit the /etc/hosts file and update the lines that reads your old-host-name:
$ sudo vi /etc/hosts
From:
127.0.1.1 localhost
To:
127.0.1.1 nixcraft-rhel8
Save and close the file.

A note about hostnamectl command

Systemd based Linux distributions such as RHEL 8 can simply use the hostnamectl command to rename hostname. To see current setting just type:
$ hostnamectl
Sample outputs:

   Static hostname: localhost
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 2f23e83aad5d413498ef79742ad660eb
           Boot ID: c3c158618d54451f82f3cee23471248d
    Virtualization: kvm
  Operating System: Red Hat Enterprise Linux 8.0 Beta (Ootpa)
       CPE OS Name: cpe:/o:redhat:enterprise_linux:8.0:beta
            Kernel: Linux 4.18.0-32.el8.x86_64
      Architecture: x86-64

To change hostname from linux-mig6 to suse-nixcraft, enter:
$ sudo hostnamectl set-hostname nixcraft-rhel8
$ hostnamectl

How to set or change RHEL 8 server hostname
Set or change RHEL 8 server hostname using the hostnamectl command

Conclusion

On RHEL 8 one can use the hostnamectl command to control the system hostname and set a new name as well. This is recommended method for all RHEL 8 users.

Posted by: Vivek Gite

The author is the creator of nixCraft and a seasoned sysadmin, DevOps engineer, and a trainer for the Linux operating system/Unix shell scripting. Get the latest tutorials on SysAdmin, Linux/Unix and open source topics via RSS/XML feed or weekly email newsletter.