Hostnames can help you identify resources to simplify management and maintenance and to enhance system security in scenarios such as when you configure hostname-based Resource Access Management (RAM) rules. This topic describes how to specify hostnames by using the hostname and hostnamectl commands, and how to specify and manage hostnames by using the NetworkManager network management service.
Limits
The instance runs the Alibaba Cloud Linux 3 operating system.
Run the hostname command to query and specify a temporary hostname
A hostname specified by using the hostname command becomes invalid on system restart. To use a persistent hostname, you can specify a hostname in the /etc/hostname file.
Run the hostname command to query the hostname of the instance:
hostnameRun the hostname command to specify the hostname of the instance.
In this example, the hostname is set to
alinux-dev-test.sudo hostname alinux-dev-test
Run the hostnamectl command to query and specify hostnames
You can run the hostnamectl command to specify or view information about hostnames. You can use the systemd-hostnamed service to modify the hostnames. systemd-hostnamed is a service component that manages hostnames in the background.
Run the
hostnamectlcommand to query hostnames:# Query the static hostname. hostnamectl --static # Query the temporary hostname. hostnamectl --transientThe sample command output shown in the following figure contains the hostnames that you queried.

Run the
hostnamectlcommand to specify a hostname.Specify a hostname.
In this example, the hostname is set to
alinux-dev-test.sudo hostnamectl set-hostname alinux-dev-testQuery the static and temporary hostnames.
hostnamectl --static hostnamectl --transientThe
hostnamectlcommand can be used to specify the static and temporary hostnames. The command output shown in the following figure contains the static and temporary hostnames that you specified.
View the
/etc/hostnamefile.cat /etc/hostnameThe following figure shows the most recent static hostname saved in the
/etc/hostnamefile.
(Important) Use NetworkManager to specify and manage hostnames
NetworkManager manages hostname settings related to network interfaces when you configure network connections. When you use NetworkManager to modify network connection settings, NetworkManager notifies the systemd-hostnamed component to change hostname settings by using the dbus interface.
NetworkManager specifies and manages the temporary hostname by setting the hostname-mode parameter to default or dhcp in the NetworkManager.conf file. The following table describes the valid values of the hostname-mode parameter.
Value | Description |
default | This is the default value.
|
dhcp |
|
none | NetworkManager does not specify or manage the temporary hostname. |
Examples
To prevent conflicts with the hostname management system of NetworkManager, you can specify hostnames by using one of the following methods:
(Recommend) Run the hostnamectl command to specify a static hostname. In this example, the hostname is set to alinux-dev-test.
sudo hostnamectl set-hostname alinux-dev-testSet
hostname-modeto none, which disables NetworkManager from managing hostnames.Open the
/etc/NetworkManager/NetworkManager.conffile.sudo vim /etc/NetworkManager/NetworkManager.confPress the
Ikey to enter Insert mode and add the following content below the string[main]:hostname-mode=nonePress the
Esckey, enter:wq, and then press theEnterkey to save and close the configuration file.Restart NetworkManager to make the configuration take effect.
sudo systemctl restart NetworkManager
Use the
systemdservice to specify a hostname.Add the following configuration to the
servicefile of thesystemdservice to ensure that the systemd service can start before NetworkManager.[Unit] Before=NetworkManager.service