CentOS 8 Change Hostname (computer name) command

last updated in Categories ,

I am a new CentOS 8 Linux user and setup a server. They setup my server name to ‘li2025-143’ 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-centos8’ on CentOS Enterprise Linux? Can you tell me how do I change the CentOS 8 computer name? How do I assign a static hostname to server running CentOS 8?

Introduction : For CentOS 8 you can use the hostnamectl command to change the hostname of a CentOS 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 CentOS 8.

Display the current CentOS 8 hostname

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

CentOS 8 change hostname command

The procedure to change the computer name on CentOS 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 CentOS 8 server hostname without a system restart

Type the following commands:
$ sudo hostname nixcraft-centos8
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-centos8
Save and close the file.

A note about hostnamectl command

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

   Static hostname: localhost.localdomain
Transient hostname: li2025-143.members.linode.com
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 3e729c2d7c094902af0333ce40564ffe
           Boot ID: e359e65f68ff45f2825134354bdc0a8d
    Virtualization: kvm
  Operating System: CentOS Linux 8 (Core)
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 4.18.0-80.7.1.el8_0.x86_64
      Architecture: x86-64

To change hostname from localhost.localdomain to nixcraft-centos8, enter:
$ sudo hostnamectl set-hostname nixcraft-centos8
$ hostnamectl

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

Conclusion

On CentOS 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 CentOS 8 users.

This entry is 16 of 16 in the Linux/Unix Set hostname Tutorial series. Keep reading the rest of the series:
  1. Ubuntu Linux Change Hostname (computer name)
  2. Debian Linux: Change Hostname / Computer Name Permanently
  3. Linux change my hostname / computer system name
  4. Linux Change Hostname
  5. RHEL / Centos Linux 7: Change and Set Hostname Command
  6. OpenBSD Change Hostname
  7. CentOS Linux 5/6 Change Hostname Command
  8. Linux setting hostname and domain name of my server
  9. FreeBSD Change Hostname without reboot
  10. SuSe Linux Change Hostname without reboot
  11. How to set the hostname on Fedora Linux
  12. Ubuntu 18.04 LTS change hostname permanently
  13. Ubuntu set hostname permanently (computer name) command
  14. OpenSUSE Linux set hostname permanently (computer name) command
  15. RHEL 8 Linux set hostname permanently (computer name) command
  16. CentOS 8 Linux set hostname permanently (computer name) command

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.

Notable Replies

  1. Under Centos 7 it would be “hostnamectl set-hostname nixcraft-centos8”. I would think that it still works under Centos 8.

  2. yes, it is just an example. you can replace nixcraft-centos8 with anything you want.

Continue the discussion www.nixcraft.com

Participants

Historical Comment Archive

3 comment

    Still, have a question? Get help on our forum!