Usually the host name is set during the installation of the operating system or dynamically specified during the creation of the virtual machine.
There are many reasons why you need to change the hostname. The most common reason is that when an instance is automatically created, the host name needs to be automatically set.
This guide explains how to modify the hostname on CentOS 8 without rebooting the system.
Before proceeding with this article, please make sure you log in to the system with an account with sudo privileges.
A host name is the label name that marks a device on the network. In the same network, you should not have two or more machines with the same hostname.
` There are three types of hostname: static, pretty, and transient
static
-the traditional host name. It is stored in the /etc/hostname
file and can be set by the user pretty
-a free-form UTF8 hostname used to represent the user. For example: Linuxize's desktop
. transient
-The dynamic host name maintained by the kernel. During operation, the DHCP or mDNS server can change the transient host name. By default, it is exactly the same as the static hostname.We strongly recommend using the full domain name (FQDN) to set static
and transient
names, such as host.example.com
.
Execute the following command to display the current host name.
hostnamectl
In this example, the current hostname is set to centos8.localdomain
.
hostnamectl
commandIn CentOS 8 and all Linux distributions that use systemd, you can use the hostnamectl
command to modify the system hostname and related settings. The syntax is as follows:
sudo hostnamectl set-hostname host.example.com
sudo hostnamectl set-hostname "Your Pretty HostName"--pretty
sudo hostnamectl set-hostname host.example.com --static
sudo hostnamectl set-hostname host.example.com --transient
For example, to change the system static host name to host.linuxize.com
, you will use the following command:
sudo hostnamectl set-hostname host.linuxize.com
To change the pretty host name to Linuxize's desktop
, enter:
sudo hostnamectl set-hostname "Linuxize's desktop"--pretty
The hostnamectl
command does not produce output. If successful, return 0, otherwise, return a non-zero error code.
To verify that the host name was successfully modified, use the hostnamectl
command.
nmtui
command nmtui
is a curses-based tool that interacts with the network manager. It can also be used to set or modify the host name.
Start this tool and enter its name in the terminal:
sudo nmtui
Use the arrow keys to move, select Set system hostname
, and press the'Enter' key:
Enter the new host name:
Press Enter
to confirm the new hostname:
Finally, restart the systemd-hostnamed
service to make the changes take effect:
sudo systemctl restart systemd-hostnamed
nmcli
command nmcli
is a command line tool used to control the network manager, and it can also modify the hostname of the system.
To view the current host name, enter:
sudo nmcli g hostname
To change the host name to host.linuxize.com
, use the following command:
sudo nmcli g hostname host.linuxize.com
To make the changes take effect, restart the systemd-hostnamed
service:
sudo systemctl restart systemd-hostnamed
To modify the hostname of the CentOS system, use the hostnamectl set-hostname
command to add the new hostname to modify it.
Recommended Posts