我读过有关 systemd-resolved.service 的内容https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html并学习了处理 /etc/resolv.conf 的四种模式。
- /run/systemd/resolve/stub-resolv.conf
- /usr/lib/systemd/resolv.conf
- /run/systemd/resolve/resolv.conf
- /etc/resolv.conf 可能由其他包管理
我读了好几遍,但仍然对如何确定作为普通用户应该选择 /etc/resolv.conf 的哪种模式感到困惑。
例如,我尝试添加一些自定义 dns 服务器,所以,
- 在/etc/systemd/resolved.conf中添加DNS=8.8.8.8 8.8.4.4,并检查/run/systemd/resolve/resolv.conf,其中存在8.8.8.8和8.8.4.4。
- 如果将 /run/systemd/resolve/resolv.conf 符号链接到 /etc/resolv.conf,则 8.8.8.8 和 8.8.4.4 将在 /run/systemd/resolve/resolv.conf 中消失。
更新1:
test@instance-1:~$ cat /run/systemd/resolve/resolv.conf
...
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.
nameserver 8.8.8.8
nameserver 8.8.4.4
test@instance-1:/etc$ sudo ln -sf /run/systemd/resolve/resolv.conf /etc/resolv.conf
test@instance-1:/etc$ ls -alh /etc/resolv.conf
lrwxrwxrwx 1 root root 32 Mar 18 07:22 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
test@instance-1:/etc$ sudo reboot
test@instance-1:~$ cat /etc/resolv.conf
domain c.prime-poetry-197705.internal
search c.prime-poetry-197705.internal. google.internal.
nameserver 169.254.169.254
test@instance-1:~$ cat /run/systemd/resolve/resolv.conf
domain c.prime-poetry-197705.internal
search c.prime-poetry-197705.internal. google.internal.
nameserver 169.254.169.254
test@instance-1:~$ ls -alh /etc/resolv.conf
lrwxrwxrwx 1 root root 32 Mar 18 07:22 /etc/resolv.conf -> /run/systemd/resolve/resolv.conf
更新 2:来自 /etc/resolv.conf 的符号链接
test@instance-1:~$ sudo ln -sf /etc/resolv.conf /run/systemd/resolve/resolv.conf
test@instance-1:~$ ls -alh /run/systemd/resolve/resolv.conf
lrwxrwxrwx 1 root root 16 Mar 18 07:51 /run/systemd/resolve/resolv.conf -> /etc/resolv.conf
test@instance-1:~$ sudo reboot
test@instance-1:~$ ls -alh /run/systemd/resolve/resolv.conf
-rw-r--r-- 1 systemd-resolve systemd-resolve 603 Mar 18 07:52 /run/systemd/resolve/resolv.conf
答案1
我的猜测是您从 DHCP 获取 IP 配置,这会覆盖文件中的 DNS 信息resolved.conf
(来自systemd.network(5)
):
[DHCP]
部分选项[...]
UseDNS=
当true
(默认)时,将从 DHCP 服务器接收到的 DNS 服务器将被使用,并优先于任何静态配置的服务器。这对应于
nameserver
中的选项resolv.conf(5)
。
尝试将以下内容添加到您的{networkname}.network
文件中(在 中/etc/systemd/network
):
[DHCP]
UseDNS=false