How to Change Hostname on CentOS 8

Change Hostname on CentOS 8

In this article, we will have explained the necessary steps to change hostname on CentOS 8. Before continuing with this tutorial, make sure you are logged in as a user with sudo privileges. All the commands in this tutorial should be run as a non-root user.

A hostname is a name assigned to a “host” i.e. a computer on a network. The hostname is basically just your computer’s name. It’s used to identify your computer on the network.

There are three classes of hostname: static, pretty, and transient.

  • static – The traditional hostname. It is stored in the /etc/hostname file and can be set by the user.
  • pretty – A free-form UTF8 hostname used for presentation to the user. For example, linuxtips's desktop.
  • transient – A dynamic hostname that is maintained by the kernel. DHCP or mDNS servers can change the transient hostname at run time. By default, it is the same as the static hostname.

Change Hostname on CentOS

Step 1. The first command will update the package lists to ensure you get the latest version and dependencies.

sudo dnf update

Step 2. Change hostname in CentOS using command line.

  • To set a persistent hostname using hostnamectl command, use the command:
sudo hostnamectl set-hostname <name> --static

Example:

sudo hostnamectl set-hostname root.linuxtips.us --static

Confirm your new hostname:

$ hostnamectl 
   Static hostname: root.linuxtips.us
Transient hostname: cent-01
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 596e865e6c510012098c4fd47fdb858138
           Boot ID: 47d2f07bmwb799ae6b0c0ba8600e
    Virtualization: kvm
  Operating System: CentOS Linux 8 (Core)
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 3.10.0-957.5.1.el7.x86_64
      Architecture: x86-64
  • Set Pretty hostname.
sudo hostnamectl set-hostname "linuxtips dekstop" --pretty
  • Set Transient hostname – Usually same as static hostname.
sudo hostnamectl set-hostname root.linuxtips.us --transient

To view the current hostname, execute the following command:

$ hostnamectl 
   Static hostname: root.linuxtips.us
   Pretty hostname: linuxtips dekstop
         Icon name: computer-vm
           Chassis: vm
        Machine ID: 596e865e6c1001208c4fd47fdb858138
           Boot ID: 47d2f07ebmwb799ae6b0c0ba8600e
    Virtualization: kvm
  Operating System: CentOS Linux 8 (Core)
       CPE OS Name: cpe:/o:centos:centos:8
            Kernel: Linux 3.10.0-957.5.1.el7.x86_64
      Architecture: x86-64

Congratulation, you have learned how to change hostname on CentOS 8. If you have any question, please leave a comment below.