All Products
Search
Document Center

Alibaba Cloud Linux:Specify a hostname

Last Updated:Sep 19, 2024

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

Warning

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:

    hostname
  • Run 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 hostnamectl command to query hostnames:

    # Query the static hostname.
    hostnamectl --static
    # Query the temporary hostname.
    hostnamectl --transient

    The sample command output shown in the following figure contains the hostnames that you queried.

    image

  • Run the hostnamectl command to specify a hostname.

    1. Specify a hostname.

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

      sudo hostnamectl set-hostname alinux-dev-test

      Query the static and temporary hostnames.

      hostnamectl --static
      hostnamectl --transient

      The hostnamectl command 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.

      image

    2. View the /etc/hostname file.

      cat /etc/hostname

      The following figure shows the most recent static hostname saved in the /etc/hostname file.

      image

(Important) Use NetworkManager to specify and manage hostnames

Note

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.

  • If a static hostname is specified, NetworkManager uses this static hostname as the temporary hostname.

  • If no static hostname is specified, NetworkManager uses Dynamic Host Configuration Protocol (DHCP) to obtain a temporary hostname.

    • If DHCP returns a hostname, NetworkManager uses this hostname as the temporary hostname.

    • If DHCP does not return a hostname, NetworkManager uses reverse DNS resolution to obtain a temporary hostname.

      If the reverse DNS resolution fails, the temporary hostname specified by a service other than NetworkManager is used. For example, the hostname specified by using the hostname command is used. If the temporary hostname is valid, this hostname is used. If the temporary hostname is invalid, NetworkManager sets the temporary hostname to localhost.localdomain.

dhcp

  • If a static hostname is specified, NetworkManager uses this static hostname as the temporary hostname.

  • If no static hostname is specified, NetworkManager uses DHCP to obtain a temporary hostname.

    • If DHCP returns a hostname, NetworkManager uses this hostname as the temporary hostname.

    • If DHCP does not return a hostname, the temporary hostname specified by a service other than NetworkManager is used. For example, the hostname specified by using the hostname command is used. If the temporary hostname is valid, this hostname is used. If the temporary hostname is invalid, NetworkManager sets the temporary hostname to localhost.localdomain.

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-test
  • Set hostname-mode to none, which disables NetworkManager from managing hostnames.

    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 content below the string [main]:

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

    4. Restart NetworkManager to make the configuration take effect.

      sudo systemctl restart NetworkManager
  • Use the systemd service to specify a hostname.

    Add the following configuration to the service file of the systemd service to ensure that the systemd service can start before NetworkManager.

    [Unit]
    Before=NetworkManager.service