Gra*_*lls 23 linux hostname systemd
与编辑 /etc/hostname 或其他相关的地方相反?
一定有一个很好的理由(我希望)——总的来说,我更喜欢“旧”的方式,一切都是一个文本文件。我不是想引起争议——我真的很想知道,并自己决定这是否是一个很好的理由。谢谢。
slm*_*slm 32
hostnamectl
是 systemd 的一部分,并提供适当的 API 来处理以标准化方式设置服务器的主机名。
$ rpm -qf $(type -P hostnamectl)
systemd-219-57.el7.x86_64
Run Code Online (Sandbox Code Playgroud)
以前,每个不使用 systemd 的发行版都有自己的方法来执行此操作,这造成了许多不必要的复杂性。
Run Code Online (Sandbox Code Playgroud)DESCRIPTION hostnamectl may be used to query and change the system hostname and related settings. This tool distinguishes 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 used to initialize the kernel hostname at boot (e.g. "lennarts-laptop"), and the transient hostname which is a default received from network configuration. If a static hostname is set, and is valid (something other than localhost), then the transient hostname is not used. Note that the pretty hostname has little restrictions on the characters used, while the static and transient hostnames are limited to the usually accepted characters of Internet domain names. The static hostname is stored in /etc/hostname, see hostname(5) for more information. The pretty hostname, chassis type, and icon name are stored in /etc/machine-info, see machine-info(5). Use systemd-firstboot(1) to initialize the system host name for mounted (but not booted) system images.
hostnamectl
还将大量不同的数据集中到一个位置进行启动:
$ hostnamectl
Static hostname: centos7
Icon name: computer-vm
Chassis: vm
Machine ID: 1ec1e304541e429e8876ba9b8942a14a
Boot ID: 37c39a452464482da8d261f0ee46dfa5
Virtualization: kvm
Operating System: CentOS Linux 7 (Core)
CPE OS Name: cpe:/o:centos:centos:7
Kernel: Linux 3.10.0-693.21.1.el7.x86_64
Architecture: x86-64
Run Code Online (Sandbox Code Playgroud)
此处的信息来自/etc/*release
、uname -a
等,包括服务器的主机名。
顺便说一句,一切都在文件中,hostnamectl
只是简化了我们如何与这些文件交互或了解它们的每个位置。
作为证明,您可以使用strace -s 2000 hostnamectl
并查看它从哪些文件中提取:
$ strace -s 2000 hostnamectl |& grep ^open | tail -5
open("/lib64/libattr.so.1", O_RDONLY|O_CLOEXEC) = 3
open("/usr/lib/locale/locale-archive", O_RDONLY|O_CLOEXEC) = 3
open("/proc/self/stat", O_RDONLY|O_CLOEXEC) = 3
open("/etc/machine-id", O_RDONLY|O_NOCTTY|O_CLOEXEC) = 4
open("/proc/sys/kernel/random/boot_id", O_RDONLY|O_NOCTTY|O_CLOEXEC) = 4
Run Code Online (Sandbox Code Playgroud)
对于精明的观察者,您应该注意到上面strace
并非所有文件都存在。hostnamectl
实际上是与服务交互,systemd-hostnamectl.service
它实际上与大多数管理员熟悉的大多数文件进行“交互”,例如/etc/hostname
.
因此,当您运行时,hostnamectl
您会从服务中获取详细信息。这是一项按需服务,因此您不会看到是否一直在运行。只有在hostnamectl
运行时。如果你运行一个watch
命令,然后开始hostnamectl
多次运行,你可以看到它:
$ watch "ps -eaf|grep [h]ostname"
root 3162 1 0 10:35 ? 00:00:00 /usr/lib/systemd/systemd-hostnamed
Run Code Online (Sandbox Code Playgroud)
它的来源在这里:https : //github.com/systemd/systemd/blob/master/src/hostname/hostnamed.c如果你仔细阅读它,你会看到对/etc/hostname
等的引用。
它仍然是一个文本文件,你仍然可以编辑它,不会有问题。
文本文件已标准化为/etc/hostname
.
根据维护者的说法,systemd-hostname、systemd-timedated 等服务很大程度上是为 GNOME 等现有 GUI 设计的。systemd-hostnamed 允许 GUI 请求主机名更改,而无需以 root 身份运行(取决于 polkit 策略)。Dbus 还提供了一种订阅更改的方法,适合 GUI 需求。在这些情况下,可能总共被一个应用程序使用:)。我不知道,也许时钟使用 systemd-timedated 来监听时区重新配置?
将 hostnamectl 视为运行 GUI 后端的存根,它可能是也可能不是有用的 CLI 实用程序。systemd-hostnamed 特别不打算添加 GUI 代码未执行的全部功能。
systemd-hostnamed 服务无意抽象发行版之间的差异。上游 systemd 在单个配置文件 上进行标准化,/etc/hostname
其中以前在基于 Debian 和 Redhat 的发行版上有不同的配置文件。
这假设 hostnamectl 正在与 systemd-hostnamed 的标准实现进行对话。但据我所知,当前没有任何发行版可以修补所使用的文件名。
我想指出的是,/etc/hostname
启动时的加载是由 systemd PID 1 早期执行的。它不依赖于运行 systemd-hostnamed。
我想如果您打开系统设置 GUI 并且同时显示主机名,您可能会注意到一个无害的差异。如果您进行编辑/etc/hostname
,然后将hostname --file /etc/hostname
更改应用到正在运行的系统,GUI 显示可能不会立即更新。systemd-hostnamed 提供有关其维护的所有主机名版本更改的 dbus 通知,因此 GUI 可能不会费心去侦听现代 Linux 内核上提供的系统主机名通知。
(传统上,在运行时更改主机名是一个坏主意。 它可能会导致 X 等软件出现问题。我相信systemd没有解决这个问题。也许使用 systemd 的发行版已经解决了这个问题) 。
归档时间: |
|
查看次数: |
16324 次 |
最近记录: |