我如何获得symlinded /etc /hostName在启动时生效



我在带有3个不同Linux分区的嵌入式Linux设备上工作。最终使用选择要从哪个分区启动。根文件系统仅读取。有一个安装读写的第四个分区。我需要Linux的所有实例与DNS服务器(即使用相同的主机名)重新启动相同名称。当创建文件系统并在稍后分配时,主机名是由于每个设备都需要唯一主机名而分配的。我所做的是在读写分区上创建一个文件,该文件将在以后再写。然后,我更改了/etc/hostName,成为该文件的符号链接。我知道在安装了读取文字分区之前,无法读取文件,我相信这是我的问题。如果我将/etc/hostName设置为普通文件,则该文件中指定的任何内容都可以正常工作。更改/etc/主机似乎对任何事情都没有。

WiFi连接时,要控制在DNS服务器上注册的名称。我发现控制这一点的唯一方法是通过主机名。/etc/hosts文件似乎不会影响它。如果主机名文件不可读或不设置,则将Linux默认为" localhost",并且不会在DNS中注册任何有用的内容。

通过启动后运行的应用程序,通过RFKill启用了WiFi。在启用WiFi之前,在脚本中运行某些内容是可能的解决方案。在启用WiFi之前,我无法通过更改命令行上的主机名来成功更改DNS注册的内容。我可以更改主机名,但是DNS注册的内容不会更改。

信息:

Linux内核为3.14.60,是基于YOCTO的构建。

SystemD在启动上管理服务。

电流/etc/hosts

127.0.0.1       ABC123.localdomain              ABC123

current/etc/hostName

SerialNumberGoesHere

这是启动后查看主机名的所有方法:

>hostname
localhost
>hostnamectl status
   Static hostname: SerialNumberGoesHere
Transient hostname: localhost
         Icon name: computer
        Machine ID: 4cdac8e5dce348d2bfc133dd393f6172
           Boot ID: 9b8c9da934e748fc86606c4a24f57f9e
  Operating System: Linux
            Kernel: Linux 3.14.60+g02d9429
      Architecture: arm
>uname -n
localhost
>echo /proc/sys/kernel/hostname
localhost
>sysctl kernel.hostname
kernel.hostname = localhost

您可以看到" hostnamectl状态"拾取了正确的静态主机名,但内核没有。

在设备上

>nslookup 192.168.12.238
Server:    192.168.12.6
Address 1: 192.168.12.6 dc1.hq.com
Name:      192.168.12.238
Address 1: 192.168.12.238 linux.local

在另一台计算机上(ping 192.168.12.238作品)

>nslookup 192.168.12.238
Server:     127.0.1.1
Address:    127.0.1.1#53
** server can't find 238.12.168.192.in-addr.arpa: NXDOMAIN

如果我将主机名符号链接更改为真实文件,这是所需的结果:

>hostname
SerialNumberGoesHere
>hostnamectl status
   Static hostname: SerialNumberGoesHere
         Icon name: computer
        Machine ID: 4cdac8e5dce348d2bfc133dd393f6172
           Boot ID: ed760b42b7ae414881bc2d9352a8bb82
  Operating System: ARANZ Sugar-Free Silhouette Star M2 (fido)
            Kernel: Linux 3.14.60+g02d9429
      Architecture: arm
>uname -n
SerialNumberGoesHere
>echo /proc/sys/kernel/hostname
SerialNumberGoesHere
>sysctl kernel.hostname
kernel.hostname = SerialNumberGoesHere

在设备上

> nslookup 192.168.12.238
Server:    192.168.12.7
Address 1: 192.168.12.7 dc1.hq.com
Name:      192.168.12.238
Address 1: 192.168.12.238 serialnumbergoeshere.hq.com

在另一台计算机上

> nslookup 192.168.12.238
Server:     127.0.1.1
Address:    127.0.1.1#53
238.12.168.192.in-addr.arpa name = serialnumbergoeshere.hq.com.

更新2017年1月10日

我找到了答案。您必须在主机名命令之后重新启动网络服务。

systemctl restart systemd-networkd.service

我正在寻找类似的功能。

我的第一个想法也是Symlink/etc/hostName到另一个分区上的文件,并确保在显示启动消息之前将分区安装在显示之前: systemd[1]: Set hostname to <myhostname>

但是,在挖掘者更深入之后,它并不是那么简单。从我在其他地方发现的东西:

我本来希望initramfs将您的/etc/hostName包装到实际主机名中,也许您需要再生initramfs?

systemd是在Initramfs早期启动的,在initramfs中设置了initramfs的主机名,但是在将root切换到实际系统后,它重新启用并再次设置了主机名,因此您应该最终以适当的hostName

最终以适当的hostName

所以我得到了Daniel Schepler提供的解决方案。Systemd服务:

[Unit]
Description=Update hostname to what we want
Before=systemd-networkd.target
After=mountdata.service
RequiresMountsFor=/usr
DefaultDependencies=no
[Service]
Type=simple
ExecStart=/usr/bin/mycustomhostnamescript.sh
[Install]
WantedBy=multi-user.target

脚本:

...
    hostn=$(cat "$FILE_SERIAL")
    echo "setting hostname=$hostn"
    echo "$hostn" > /etc/hostname
    hostname "$hostn"
...

重要的是您在脚本中不使用hostnamectl,因为它的依赖关系尚未加载,因此会失败!

该解决方案无需重新启动网络服务即可,因为它尚未启动。它还可以仅重新启动一次。

您必须在主机名命令之后重新启动网络服务。

systemctl restart systemd-networkd.service

- schustercp

for Mender我能够使用

[Unit]
Description=Update hostname to what we want
Before=systemd-networkd.target
After=mountdata.service
RequiresMountsFor=/data
DefaultDependencies=no
[Service]
Type=simple
ExecStart=/usr/bin/hostname -F /etc/hostname
[Install]
WantedBy=multi-user.target

我更改了"要求"为我的数据分区,该数据分区包含主机名的文本文件。

相关内容

最新更新