Show Table of Contents
3.3. 使用 hostnamectl 配置主机名
hostnamectl 工具是用来管理给定主机中使用的三种类型的主机名。
3.3.1. 查看所有主机名
请运行下面的命令查看所有当前主机名:
~]$ hostnamectl status
如果未指定任何选项,则默认使用 status 选项。
3.3.2. 设定所有主机名
请作为
root 用户运行下面的命令设定系统中的所有主机名:
~]# hostnamectl set-hostname name
这样会将 pretty、static 和 transient 主机名变得相似。Static 和 transient 主机名会简化为 pretty 主机名格式。使用 “-” 替换空格,并删除特殊字符。
3.3.3. 设定特定主机名
请作为
root 用户,使用附带相关选项的以下命令设定特定主机名:
~]# hostnamectl set-hostname name [option...]
其中 option 是 --pretty、--static, 会 --transient 中的一个或多个选项。
如果
--static 或 --transient 选项与 --pretty 选项一同使用,则会将 static 和 transient 主机名简化为 pretty 主机名格式。使用 “-” 替换空格,并删除特殊字符。如果未使用 --pretty 选项,则不会发生简化。
设定 pretty 主机名时,如果该主机名中包含空格或单引号,请记住要使用正确的引号。例如:
~]# hostnamectl set-hostname "Stephen's notebook" --pretty
3.3.4. 清除特定主机名
要清除特定主机名,并将其还原为默认形式,请作为
root 用户使用附带相关选项的以下命令:
~]# hostnamectl set-hostname "" [option...]
其中 "" 是括起来的空白字符串,option 是 --pretty、--static 和 --transient 中的一个或多个选项。
3.3.5. 远程更改主机名
要在远程系统中运行
hostnamectl 命令,请使用 -H, --host 选项,如下所示:
~]# hostnamectl set-hostname -H [username]@hostname
其中 hostname 是要配置的远程主机。username 为自选项。hostnamectl 工具会使用 SSH 连接到远程系统。

Where did the comment section go?
Red Hat's documentation publication system recently went through an upgrade to enable speedier, more mobile-friendly content. We decided to re-evaluate our commenting platform to ensure that it meets your expectations and serves as an optimal feedback mechanism. During this redesign, we invite your input on providing feedback on Red Hat documentation via the discussion platform.