Linux设置主机名

在CentOS/RHEL 7中,关于主机名的配置。

参考3.3. CONFIGURING HOST NAMES USING HOSTNAMECTL

UNDERSTANDING HOST NAMES

有三种定义的主机名:静态的(static),灵活的(pretty)以及瞬态的(transient)。
The “static” host name is the traditional hostname, which can be chosen by the user, and is stored in the /etc/hostname file. The “transient” hostname is a dynamic host name maintained by the kernel. It is initialized to the static host name by default, whose value defaults to “localhost”. It can be changed by DHCP or mDNS at runtime. The “pretty” hostname is a free-form UTF8 host name for presentation to the user.

NOTE
A host name can be a free-form string up to 64 characters in length. However, Red Hat recommends that both static and transient names match the fully-qualified domain name (FQDN) used for the machine in DNS, such as host.example.com. It is also recommended that the static and transient names consists only of 7 bit ASCII lower-case characters, no spaces or dots, and limits itself to the format allowed for DNS domain name labels, even though this is not a strict requirement. Older specifications do not permit the underscore, and so their use is not recommended.
The hostnamectl tool will enforce the following: Static and transient host names to consist of a-z, A-Z, 0-9, “-”, “_” and “.” only, to not begin or end in a dot, and to not have two dots immediately following each other. The size limit of 64 characters is enforced.

CONFIGURING HOST NAMES USING TEXT USER INTERFACE, NMTUI

The text user interface tool nmtui can be used to configure a host name in a terminal window. Issue the following command to start the tool:

$ nmtui

The text user interface appears. Any invalid command prints a usage message.

The NetworkManager Text User Interface starting menu

To navigate, use the arrow keys or press Tab to step forwards and press Shift+Tab to step back through the options. Press Enter to select an option. The Space bar toggles the status of a check box.

The NetworkManager text user interface tool nmtui can be used to query and set the static host name in the /etc/hostname file. Note that at time of writing, changing the host name in this way will not be noticed by hostnamectl.

To force hostnamectl to notice the change in the static host name, restart hostnamed as root:

#
systemctl restart systemd-hostnamed

CONFIGURING HOST NAMES USING HOSTNAMECTL

The hostnamectl tool is provided for administering the three separate classes of host names in use on a given system.

  • View All the Host Names
    To view all the current host names, enter the following command:
    $
    hostnamectl status

The status option is implied by default if no option is given.

  • Set All the Host Names
    To set all the host names on a system, enter the following command as root:
    #
    hostnamectl set-hostname name

This will alter the pretty, static, and transient host names alike. The static and transient host names will be simplified forms of the pretty host name. Spaces will be replaced with “-” and special characters will be removed.

  • Set a Particular Host Name

To set a particular host name, enter the following command as root with the relevant option:

#
hostnamectl set-hostname name [option...]

Where option is one or more of: –pretty, –static, and –transient.
If the –static or –transient options are used together with the –pretty option, the static and transient host names will be simplified forms of the pretty host name. Spaces will be replaced with “-” and special characters will be removed. If the –pretty option is not given, no simplification takes place.
When setting a pretty host name, remember to use the appropriate quotation marks if the host name contains spaces or a single quotation mark. For example:

#
hostnamectl set-hostname "Stephen's notebook" --pretty

  • Clear a Particular Host Name

To clear a particular host name and allow it to revert to the default, enter the following command as root with the relevant option:

#
hostnamectl set-hostname "" [option...]

Where “” is a quoted empty string and where option is one or more of: –pretty, –static, and –transient.

  • Changing Host Names Remotely

To execute a hostnamectl command on a remote system, use the -H, –host option as follows:

#
hostnamectl set-hostname -H [username]@hostname

Where hostname is the remote host you want to configure. The username is optional. The hostnamectl tool will use SSH to connect to the remote system.

CONFIGURING HOST NAMES USING NMCLI

The NetworkManager tool nmcli can be used to query and set the static host name in the /etc/hostname file. Note that at time of writing, changing the host name in this way will not be noticed by hostnamectl.

To query the static host name, issue the following command:

$ nmcli general hostname

To set the static host name to my-server, issue the following command as root:

#
nmcli general hostname my-server

To force hostnamectl to notice the change in the static host name, restart hostnamed as root:

#
systemctl restart systemd-hostnamed