To check the hostname open your terminal and type hostname or hostnamectl command in CentOS 7/8. By default, the hostname is localhost.localdomain. To change hostname you can follow the following methods.
1) By Editing /etc/hostname File
The easiest way to change the hostname is to edit the /etc/hostname file. Open your terminal and with your favorite text editor delete localhost.localdomain and change to whatever name you like.
$ sudo vi /etc/hostname
Save changes and then confirm your hostname by using hostname command. If it has not changed then you need to restart the CentOS 7/8 server.
2) Using Hostnamectl Command
Hostnamectl is a tool that is used control the Linux system hostname. You can also use this tool to change the hostname.
First and foremost confirm the current hostname by using hostnamectl as follows:
$ hostnamectl status
To change the hostname type the following command:
$ hostnamectl set-hostname hostname
It's necessary to restart the systemd-hostnamed daemon so that as to reflect the change in Static hostname.
$ sudo systemctl restart systemd-hostnamed
Note: Hostnamectl recognizes three types of hostnames: pretty, static and transient. Pretty is stored in /etc/machine-info and is a human-readable format while static hostname is stored in /etc/hostname. Transient hostname is more of a temporary hostname which may take up the static hostname when network connectivity is lost.
3) Using Nmtui Utility
To change the hostname you can use NetworkManager text interface tool (nmtui).
From the command line invoke nmtui:
$ sudo nmtui
This will present to you a text user interface like this. Using the arrow keys select Set system hostname and use tab to select OK.
Edit the hostname which by default is localhost.localdomain and change it to whatever you prefer.
After changing the hostname, choose okay, then the following confirmation message will appear. If the hostname is as you desire you can choose OK. You can confirm changes using the hostname command.
4) Using Nmcli Utility
Nmcli is a command line tool for controlling the NetworkManager and can be used to change the hostname.
To check the hostname with nmcli, type in your terminal:
$ nmcli general hostname
This will print out the hostname to your terminal. By default in CentOS 7/8 it should be localhost.localdomain.
To change hostname use the command:
$ nmcli general hostname grace
This will request for your password via a GUI interface. If you are running this command remotely remember to use root or sudo:
$ sudo nmcli general hostname grace
You can confirm the hostname by running nmcli general hostname.
In this article, we learn methods to change hostname in CentOS 7 and same command you can apply on CentOS 8. Please let us know your feedback in the below comment section.