All Products
Search
Document Center

Alibaba Cloud Linux:Specify a hostname

Last Updated:Feb 27, 2026

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:

TypeDescriptionPersistence
StaticStored in /etc/hostname. Initializes the kernel hostname at boot.Persists across reboots
TransientA 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

  1. Set the hostname. In this example, the hostname is set to alinux-dev-test.

       sudo hostnamectl set-hostname alinux-dev-test
  2. Verify the change. Query the static and transient hostnames: Expected output: The hostnamectl set-hostname command sets both the static and transient hostnames.

       hostnamectl --static
       hostnamectl --transient
       alinux-dev-test
       alinux-dev-test
  3. Confirm the static hostname is saved in /etc/hostname. Expected output:

       cat /etc/hostname
       alinux-dev-test

Method 2: Use the hostname command (transient only)

Warning

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

Note

NetworkManager manages hostname settings related to network interfaces when you configure network connections. When NetworkManager modifies network connection settings, it notifies the systemd-hostnamed component to change hostname settings 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:

  1. Use the hostname from Dynamic Host Configuration Protocol (DHCP), if returned.

  2. Attempt reverse DNS resolution.

  3. Use the transient hostname set by another service (for example, the hostname command).

  4. 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:

  1. Use the hostname from DHCP, if returned.

  2. Use the transient hostname set by another service (for example, the hostname command).

  3. 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.

  1. Open the /etc/NetworkManager/NetworkManager.conf file.

       sudo vim /etc/NetworkManager/NetworkManager.conf
  2. Press the I key to enter Insert mode and add the following line below the [main] section:

       hostname-mode=none
  3. Press the Esc key, enter :wq, and then press the Enter key to save and close the file.

  4. 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