回到手册索引

命令用途

hostnamectl 是 systemd 提供的管理工具,用于查看和修改 Linux 系统的主机名及相关系统信息(如操作系统版本、内核版本、架构等),支持同时管理静态(static)、瞬态(transient)和美观(pretty)三种类型的主机名。

常用用法示例

  1. 查看当前主机名及系统信息

    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    hostnamectl
    Static hostname: ubuntu-server
    Icon name: computer-vm
    Chassis: vm
    Machine ID: 8a1b3c4d5e6f7g8h9i0j
    Boot ID: 1k2l3m4n5o6p7q8r9s0t
    Virtualization: kvm
    Operating System: Ubuntu 22.04 LTS
    Kernel: Linux 5.15.0-78-generic
    Architecture: x86-64

    显示当前主机名、系统版本、内核版本等详细信息。

  2. 仅显示静态主机名

    1
    2
    hostnamectl --static
    ubuntu-server

    直接输出静态主机名(不会换行),适用于脚本调用。

  3. 设置静态主机名

    1
    sudo hostnamectl set-hostname "web-server"

    此操作会永久修改/etc/hostname文件。

  4. 同时设置美观主机名

    1
    sudo hostnamectl set-hostname "web01" --pretty

    允许设置带空格和特殊字符的显示名称(如”Production Web Server 01”),该名称不会用于系统标识。

  5. 查看所有类型主机名

    1
    2
    3
    4
    5
    hostnamectl status
    Static hostname: web-server
    Pretty hostname: Production Web Server 01
    Icon name: computer-server
    Chassis: server

    输出包含三种主机名状态。

  6. 更改系统图标类型

1
sudo hostnamectl set-icon-name "computer-laptop"

修改系统图标标识(影响某些桌面环境显示),有效值包括computer-desktop、computer-laptop等。

  1. 设置服务器类型
    1
    sudo hostnamectl set-chassis "server"
    修改系统机箱类型标识(影响服务管理策略),有效值包括desktop、laptop、server等。

常用参数选项

  • set-hostname [NAME]
    设置静态主机名(需要root权限),永久性修改。

  • –transient
    仅操作瞬态主机名(重启后失效),常用于临时测试。

  • –static
    仅操作静态主机名(存储在/etc/hostname),适用于持久化配置。

  • –pretty
    操作美观主机名(支持空格和特殊字符),用于显示友好名称。

  • set-icon-name [NAME]
    设置系统图标名称,影响部分GUI环境的设备标识显示。

  • set-chassis [TYPE]
    定义系统硬件类型,影响系统服务管理策略(如笔记本会启用电池服务)。

  • set-location [TEXT]
    设置设备物理位置描述(如”IDC-RackA-12”),用于系统信息记录。

  • status
    显示完整的系统信息概览(默认参数),包含主机名、系统版本、内核版本等。

原厂文档

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
NAME

hostnamectl - Control the system hostname

SYNOPSIS


hostnamectl [OPTIONS...] {COMMAND}

DESCRIPTION

hostnamectl may be used to query and change the system hostname
and related settings.

systemd-hostnamed.service(8) and this tool distinguish three
different hostnames: the high-level "pretty" hostname which might
include all kinds of special characters (e.g. "Lennart's Laptop"),
the "static" hostname which is the user-configured hostname (e.g.
"lennarts-laptop"), and the transient hostname which is a fallback
value received from network configuration (e.g. "node12345678").
If a static hostname is set to a valid value, then the transient
hostname is not used.

Note that the pretty hostname has little restrictions on the
characters and length used, while the static and transient
hostnames are limited to the usually accepted characters of
Internet domain names, and 64 characters at maximum (the latter
being a Linux limitation).

Use systemd-firstboot(1) to initialize the system hostname for
mounted (but not booted) system images.

COMMANDS

The following commands are understood:

status
Show system hostname and related information. If no command is
specified, this is the implied default.

Added in version 195.

hostname [NAME]
If no argument is given, print the system hostname. If an
optional argument NAME is provided then the command changes
the system hostname to NAME. By default, this will alter the
pretty, the static, and the transient hostname alike; however,
if one or more of --static, --transient, --pretty are used,
only the selected hostnames are changed. If the pretty
hostname is being set, and static or transient are being set
as well, the specified hostname will be simplified in regards
to the character set used before the latter are updated. This
is done by removing special characters and spaces. This
ensures that the pretty and the static hostname are always
closely related while still following the validity rules of
the specific name. This simplification of the hostname string
is not done if only the transient and/or static hostnames are
set, and the pretty hostname is left untouched.

The static and transient hostnames must each be either a
single DNS label (a string composed of 7-bit ASCII lower-case
characters and no spaces or dots, limited to the format
allowed for DNS domain name labels), or a sequence of such
labels separated by single dots that forms a valid DNS FQDN.
The hostname must be at most 64 characters, which is a Linux
limitation (DNS allows longer names).

Added in version 249.

icon-name [NAME]
If no argument is given, print the icon name of the system. If
an optional argument NAME is provided then the command changes
the icon name to NAME. The icon name is used by some graphical
applications to visualize this host. The icon name should
follow the Icon Naming Specification[1].

Added in version 249.

chassis [TYPE]
If no argument is given, print the chassis type. If an
optional argument TYPE is provided then the command changes
the chassis type to TYPE. The chassis type is used by some
graphical applications to visualize the host or alter user
interaction. Currently, the following chassis types are
defined: "desktop", "laptop", "convertible", "server",
"tablet", "handset", "watch", "embedded", as well as the
special chassis types "vm" and "container" for virtualized
systems that lack an immediate physical chassis.

Added in version 249.

deployment [ENVIRONMENT]
If no argument is given, print the deployment environment. If
an optional argument ENVIRONMENT is provided then the command
changes the deployment environment to ENVIRONMENT. Argument
ENVIRONMENT must be a single word without any control
characters. One of the following is suggested: "development",
"integration", "staging", "production".

Added in version 249.

location [LOCATION]
If no argument is given, print the location string for the
system. If an optional argument LOCATION is provided then the
command changes the location string for the system to
LOCATION. Argument LOCATION should be a human-friendly,
free-form string describing the physical location of the
system, if it is known and applicable. This may be as generic
as "Berlin, Germany" or as specific as "Left Rack, 2nd Shelf".

Added in version 249.

OPTIONS

The following options are understood:

--static, --transient, --pretty
If status is invoked (or no explicit command is given) and one
of these switches is specified, hostnamectl will print out
just this selected hostname.

If used with hostname, only the selected hostnames will be
updated. When more than one of these switches are specified,
all the specified hostnames will be updated.

Added in version 195.

-H, --host=
Execute the operation remotely. Specify a hostname, or a
username and hostname separated by "@", to connect to. The
hostname may optionally be suffixed by a port ssh is listening
on, separated by ":", and then a container name, separated by
"/", which connects directly to a specific container on the
specified host. This will use SSH to talk to the remote
machine manager instance. Container names may be enumerated
with machinectl -H HOST. Put IPv6 addresses in brackets.

-M, --machine=
Execute operation on a local container. Specify a container
name to connect to, optionally prefixed by a user name to
connect as and a separating "@" character. If the special
string ".host" is used in place of the container name, a
connection to the local system is made (which is useful to
connect to a specific user's user bus: "--user
--machine=lennart@.host"). If the "@" syntax is not used, the
connection is made as root user. If the "@" syntax is used
either the left hand side or the right hand side may be
omitted (but not both) in which case the local user name and
".host" are implied.

--no-ask-password
Do not query the user for authentication for privileged
operations.

-h, --help
Print a short help text and exit.

--version
Print a short version string and exit.

--json=MODE
Shows output formatted as JSON. Expects one of "short" (for
the shortest possible output without any redundant whitespace
or line breaks), "pretty" (for a pretty version of the same,
with indentation and line breaks) or "off" (to turn off JSON
output, the default).

-j
Equivalent to --json=pretty if running on a terminal, and
--json=short otherwise.

EXIT STATUS

On success, 0 is returned, a non-zero failure code otherwise.

SEE ALSO

systemd(1), hostname(1), hostname(5), machine-info(5),
systemctl(1), systemd-hostnamed.service(8), systemd-firstboot(1)

NOTES

1. Icon Naming Specification
https://standards.freedesktop.org/icon-naming-spec/icon-naming-spec-latest.html

COLOPHON

This page is part of the systemd (systemd system and service
manager) project. Information about the project can be found at
⟨http://www.freedesktop.org/wiki/Software/systemd⟩. If you have a
bug report for this manual page, see
⟨http://www.freedesktop.org/wiki/Software/systemd/#bugreports⟩.
This page was obtained from the project's upstream Git repository
⟨https://github.com/systemd/systemd.git⟩ on 2024-02-02. (At that
time, the date of the most recent commit that was found in the
repository was 2024-02-02.) If you discover any rendering
problems in this HTML version of the page, or you believe there is
a better or more up-to-date source for the page, or you have
corrections or improvements to the information in this COLOPHON
(which is not part of the original manual page), send a mail to
man-pages@man7.org