How to Change Hostname on Centos 8 using 3 different methods

How to Change Hostname on Centos 8 using 3 different methods

One of the first things you should learn after installing Centos 8 is how to change the hostname of your server.

The hostname is a name assigned to a computer within a network. The hostname is used as a machine identification by many network services. The hostnames, on the other hand, are designed for the user rather than the network, because the latter identifies devices based on their IP address. As a result, the hostname is a human-friendly identification, not a computer-friendly identifier.

How to Display Current Hostname

To find out what a machine's hostname is, open a terminal and type:

hostname

1..png

Our virtual machine has the default hostname, localhost.localdomain.

Another approach to check the static hostname and get more insights concerning the machine is to use the hostnamectl command:

hostnamectl

2021-09-26_11h50_25.png

Change Hostname on CentOS 8

We will discuss how to change the hostname using three different method:

  1. Using hostnamectl
  2. Using nmtui
  3. Using nmcli

Method 1: Use hostnamectl to Change Hostname

The hostnamectl command lets you to set all classes of hostnames. The command works on other Linux system as well.

Set Static Hostname with hostnamectl

The syntax to set a static hostname with hostnamectl is:

sudo hostnamectl set-hostname host.domain

For instance, we will change the hostname to girishlab:

sudo hostnamectl set-hostname girishlab

The terminal will return another line. Using one of the two options given above to confirm if the hostname was changed: 2021-09-26_11h51_43.png

Set Pretty Hostname with hostnamectl

append --pretty to the command, to modify the pretty hostname

sudo hostnamectl set-hostname "Pretty Hostname" --pretty

For instance:

sudo hostnamectl set-hostname "Girish's Lab Centos
![2021-09-26_11h53_51.png]

2021-09-26_11h53_51.png

After rebooting your server, you will see the hostname is now changed. 2021-09-26_11h59_45.png

Read more on hostnamectl here: https://www.geeksforgeeks.org/hostnamectl-command-in-linux-with-examples/

Method 2: Use nmtui to Change Hostname

Launch the NetworkManager TUI with the nmtui command to change the hostname.

Enter nmtui on your terminal:

nmtui

This will open the NetworkManager TUI as shown below. 2021-09-26_12h00_30.png

Select Set system hostname to change the hostname.

After that, Enter your desired hostname, in this example, the hostname is set to girishlabtest and select ok

2021-09-26_12h01_00.png

Enter your password if prompted, and quit the tool.

After doing the changes, reboot your system and you should see the changes was made. 2021-09-26_12h02_26.png

Read more on nmtui here: https://www.geeksforgeeks.org/nmcli-command-in-linux-with-examples/

Method 3: Using nmcli to Change hostname

The nmcli tool is also used to control the NetworkManager, but without the GUI.

The command to change the hostname with nmcli is:

sudo nmcli g hostname newhostname

For instance:

sudo nmcli g hostname girishlab

2021-09-26_12h04_04.png

Proceed to reboot your server and you will see the change was made.

2021-09-26_12h05_37.png

Read more on nmcli here: https://www.geeksforgeeks.org/nmcli-command-in-linux-with-examples/

Conclusion

With CentOS, we have several ways to change the hostname. This tutorial explained 3 different options to change the hostname on CentOS 8. Select the system name you prefer, and quickly modify the hostname on your CentOS 8 system. Having a hostname helps identify a computer on the network, making management a lot easier!