Specify a hostname
Hostnames identify Elastic Compute Service (ECS) instances on a network, simplifying resource management, maintenance, and security configuration such as hostname-based Resource Access Management (RAM) rules.
Applies to: Alibaba Cloud Linux 3
Quick reference
To set a persistent hostname, run:
sudo hostnamectl set-hostname <your-hostname>
To verify:
hostnamectl --static
Hostname types
Alibaba Cloud Linux 3 uses systemd to manage two types of hostnames:
| Type | Description | Persistence |
|---|---|---|
| Static | Stored in /etc/hostname. Initializes the kernel hostname at boot. |
Persists across reboots |
| Transient | A runtime hostname set by network services or manual commands. Falls back to the static hostname if one is configured. | Lost on reboot |
Method 1: Use hostnamectl (recommended)
The hostnamectl command uses the systemd-hostnamed service to manage hostnames. systemd-hostnamed is a background service component that manages hostname settings. This method sets both the static and transient hostnames and persists the change in /etc/hostname.
Query the current hostname
# Query the static hostname
hostnamectl --static
# Query the transient hostname
hostnamectl --transient
Expected output:
alinux-dev-test
Set a hostname
-
Set the hostname. In this example, the hostname is set to
alinux-dev-test.sudo hostnamectl set-hostname alinux-dev-test -
Verify the change. Query the static and transient hostnames: Expected output: The
hostnamectl set-hostnamecommand sets both the static and transient hostnames.hostnamectl --static hostnamectl --transientalinux-dev-test alinux-dev-test -
Confirm the static hostname is saved in
/etc/hostname. Expected output:cat /etc/hostnamealinux-dev-test
Method 2: Use the hostname command (transient only)
A hostname set by using the hostname command is a transient hostname that becomes invalid on system restart. For a persistent hostname, use hostnamectl set-hostname (Method 1) or specify the hostname directly in the /etc/hostname file.
Query the current hostname
hostname
Set a transient hostname
Set the hostname. In this example, the hostname is set to alinux-dev-test.
sudo hostname alinux-dev-test
Verify the change:
hostname
Expected output:
alinux-dev-test
This hostname reverts to the static hostname (or the default) after a system restart.
Method 3: Use NetworkManager
When you configure network connections, NetworkManager can update hostname settings by notifying systemd-hostnamed through the D-Bus interface.
NetworkManager manages the transient hostname based on the hostname-mode parameter in the NetworkManager.conf file.
hostname-mode values
The hostname-mode parameter accepts the following values:
default (default value)
If a static hostname is set, NetworkManager uses it as the transient hostname. If no static hostname is set, NetworkManager resolves the transient hostname in the following order:
-
Use the hostname from Dynamic Host Configuration Protocol (DHCP), if returned.
-
Attempt reverse DNS resolution.
-
Use the transient hostname set by another service (for example, the
hostnamecommand). -
Fall back to
localhost.localdomain.
dhcp
If a static hostname is set, NetworkManager uses it as the transient hostname. If no static hostname is set, NetworkManager resolves the transient hostname in the following order:
-
Use the hostname from DHCP, if returned.
-
Use the transient hostname set by another service (for example, the
hostnamecommand). -
Fall back to
localhost.localdomain.
Unlike default mode, dhcp mode does not attempt reverse DNS resolution as a fallback.
none
NetworkManager does not set or manage the transient hostname.
Avoid conflicts with NetworkManager
To prevent conflicts with NetworkManager hostname management, use one of the following methods:
Option A: Set a static hostname with hostnamectl (recommended)
Set a static hostname by using the hostnamectl command. When a static hostname exists, NetworkManager uses it as the transient hostname regardless of the hostname-mode setting.
sudo hostnamectl set-hostname alinux-dev-test
Option B: Disable NetworkManager hostname management
Set hostname-mode to none to prevent NetworkManager from managing the transient hostname.
-
Open the
/etc/NetworkManager/NetworkManager.conffile.sudo vim /etc/NetworkManager/NetworkManager.conf -
Press the
Ikey to enter Insert mode and add the following line below the[main]section:hostname-mode=none -
Press the
Esckey, enter:wq, and then press theEnterkey to save and close the file. -
Restart NetworkManager for the configuration to take effect.
sudo systemctl restart NetworkManager
Option C: Use a systemd service to set the hostname before NetworkManager
Add the following configuration to the [Unit] section of your systemd service file to ensure the service starts before NetworkManager:
[Unit]
Before=NetworkManager.service