通过 Netplan Config 覆盖 DNS 服务器时出现问题

通过 Netplan Config 覆盖 DNS 服务器时出现问题

我正在尝试覆盖 netplan yaml 配置中的名称服务器设置,但似乎不起作用。这是 /etc/netplan/01-netcfg.yaml 文件:

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      dhcp4-overrides:
        use-dns: no
      nameservers:
        addresses: [192.168.1.7]

我正在运行Ubuntu 18.04.3 LTS(我更改了名称服务器 IP 地址,但其他一切都相同)。此外,当我运行 时netplan --debug generate,它会产生以下内容:

** (generate:1951): DEBUG: 20:05:57.212: Processing input file /etc/netplan/01-netcfg.yaml..
** (generate:1951): DEBUG: 20:05:57.212: starting new processing pass
** (generate:1951): DEBUG: 20:05:57.212: eth0: setting default backend to 1
** (generate:1951): DEBUG: 20:05:57.212: Configuration is valid
** (generate:1951): DEBUG: 20:05:57.212: Generating output files..
** (generate:1951): DEBUG: 20:05:57.212: NetworkManager: definition eth0 is not for us (backend 1)

真正令人困惑的是没有任何/run/netplan目录......

我还应该提到,这是一个虚拟专用服务器,所以我无法访问裸机……不确定这是否重要……此外,我使用 eth0 的原因是因为除了环回地址之外,这是我运行时唯一显示的地址。我从托管公司获得服务器时的ifconfig原始文件是:/etc/netplan/01-netcfg.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

我想重新配置它,以便我可以使用另一个虚拟专用服务器作为 DNS 服务器...

编辑 只是想提一下,输出systemd-resolve --status显示 DNS 服务器是 DHCP 最初配置的,而不是被上面的 netplan 配置覆盖的。它似乎不接受 netplan 设置。

编辑2 在回答评论中提出的一些问题时,我认为托管公司安装了 Ubuntu Server,而不是桌面安装。据我所知,我只能通过终端访问 VPS,而且我认为我无法访问 GUI 桌面。至于其他问题,我已恢复到原始/etc/netplan/01-netcfg.yaml文件并重新启动服务器。现在,它应该恢复到其原始配置:

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    enp0s3:
      dhcp4: yes

另外,当我这样做时,dhclient 似乎没有运行...如果 dhclient 没有在机器上运行,那么ps aux | grep -i dhc/etc/netplan/01-netcfg.yaml 文件如何配置?dhcp4: yes

当我跑步时ip a,我得到

1: lo: <LOOPBACK,UP,LOWER_UP> mtu 65536 qdisc noqueue state UNKNOWN group default qlen 1000
     ...
2: eth0: <BROADCAST,MULTICAST,UP,LOWER_UP> mtu 1500 qdisc fq_codel state UP group default qlen 1000
     ...

但是没有提到 enp0s3... 同样对于命令ifconfig,它只提到 eth0 而不是 enp0s3...

这是 /etc/resolv.conf 文件:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver <Some loopback IP>
options edns0
search home

我改变了上述的值nameserver以及seach选项的值。

systemd-resolve --status在其输出底部包含以下内容:

Link 2 (eth0)
      Current Scopes: DNS
       LLMNR setting: yes
MulticastDNS setting: no
      DNSSEC setting: no
    DNSSEC supported: no
         DNS Servers: <IP1>
                      <IP2>
                      <IP3>
          DNS Domain: <domain1>

其中 IP1、IP2 和 IP3 是三个 IPv4 地址,我并不想将它们用于 DNS,而 domain1 是我也用不到的域名。如果还有其他有用的信息,请在评论中告诉我。由于 dhclient 未运行,我是否应该在 /etc/netplan/01-netcfg.yaml 中配置静态 IP?另一个问题是,将其用于eth0网络设备是否比更有意义enp0s3,因为后者在ip a运行时不存在?

编辑3 了解相关服务器是由 Linode 提供的可能会很有用。我停用了他们的 Linode 网络助手,以便设置我自己的 DNS 服务器。只是觉得这是有用的信息...它应该只是一个普通的Ubuntu 18.04服务器。

对更新 1 的回应 - 参见 Heynnema 的回应

/etc/netplan/01-netcnf.yaml因此,重新启动后

# This file describes the network interfaces available on your system
# For more information, see netplan(5).
network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      dhcp4-overrides:
        use-dns: no
      nameservers:
        addresses: [ 192.168.1.7 ]

/etc/systemd/resolved.conf作为

#  This file is part of systemd.
#
#  systemd is free software; you can redistribute it and/or modify it
#  under the terms of the GNU Lesser General Public License as published by
#  the Free Software Foundation; either version 2.1 of the License, or
#  (at your option) any later version.
#
# Entries in this file show the compile time defaults.
# You can change settings by editing this file.
# Defaults can be restored by simply deleting this file.
#
# See resolved.conf(5) for details

[Resolve]
#DNS=
DNS=192.168.1.7
#FallbackDNS=
#Domains=
Domains=mydomain.com
#LLMNR=no
LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#Cache=yes
#DNSStubListener=yes

我有以下内容:

/run/resolvconf/resolv.conf未在系统中存在。

cat /run/systemd/resolve/resolv.conf生成:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients directly to
# all known uplink DNS servers. This file lists all configured search domains.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 192.168.1.7
nameserver IP1
nameserver IP2
# Too many DNS servers configured, the following entries may be ignored.
nameserver IP3
search mydomain.com <domain1>

其中 IP1、IP2 和 IP3 与 的原始输出相匹配systemd-resolve --status

cat /run/systemd/resolve/stub-resolv.conf生成:

# This file is managed by man:systemd-resolved(8). Do not edit.
#
# This is a dynamic resolv.conf file for connecting local clients to the
# internal DNS stub resolver of systemd-resolved. This file lists all
# configured search domains.
#
# Run "systemd-resolve --status" to see details about the uplink DNS servers
# currently in use.
#
# Third party programs must not access this file directly, but only through the
# symlink at /etc/resolv.conf. To manage man:resolv.conf(5) in a different way,
# replace this symlink by a static file or a different symlink.
#
# See man:systemd-resolved.service(8) for details about the supported modes of
# operation for /etc/resolv.conf.

nameserver 127.0.0.53
options edns0
search mydomain.com <domain1>

此外,还dpkg -l *dnsmasq* | grep ii产生:

ii  dnsmasq-base     2.79-1       amd64        Small caching DNS proxy and DHCP/TFTP server

dpkg -l *dhcp* | grep ii产生:

ii  isc-dhcp-client      4.3.5-3ubuntu7.1 amd64        DHCP client for automatically obtaining an IP address
ii  isc-dhcp-common      4.3.5-3ubuntu7.1 amd64        common manpages relevant to all of the isc-dhcp packages

但是,当我运行ps aux | grep dns和时ps aux | grep dh,除了 grep 本身之外没有返回任何结果。

编辑4

如果有人感兴趣的话,其内容(/etc/systemd/network/05-eth0.network减去评论)如下:

[Match]
Name=eth0

[Network]
DHCP=no
DNS=IP1 IP2 IP3
Domains=<domain1>
IPv6PrivacyExtensions=false

Gateway=the_gateway_ip
Address=my_public_ip
Address=my_private_ip

其中 IP1、IP2 和 IP3 是我需要覆盖的名称服务器 IP,而 domain1 是托管提供商创建的域名。网关和两个地址设置是我需要的正确设置。我只想覆盖 DNS 设置,因此重命名文件以避免使用它就可以了。

提前谢谢了!

答案1

对于简单有效的解决方案,请尝试以下方法:

sudo systemctl stop systemd-resolved
sudo systemctl disable systemd-resolved

现在删除链接/etc/resolv.conf并创建/etc/resolv.conf一个纯文本文件,仅列出您要使用的 DNS 服务器,例如

nameserver 192.168.1.7
search mydomain

mydomain本地域在本地 DNS 配置中声明的位置。示例:(/etc/bind/zones取决于您的绑定配置)

如果你的设备是移动设备,那么你的本地 DNS 当然会失败,因此我将它包含在 resolv.conf 中

 nameserver 1.1.1.1
 nameserver 1.0.0.1

...例如 Cloudfare 服务器。

我发现使用此设置会忽略 netplan DNS 配置。

在 Ubuntu 的最新几个版本中,这种方法对我来说一直很有效,没有任何问题。

我这样做是因为systemd-resolve坚持列出我的 ISP DNS 服务器——而我不想使用它。

答案2

我打电话给托管服务提供商,他们让我查看两个配置文件。一个是我知道的 netplan 配置文件,另一个是/etc/systemd/network/05-eth0.network。通过在文件.名称前面放置一个来重命名文件,然后重新启动服务器,/etc/netplan/01-netcfg.yaml对设备所做的更改就会生效eth0。我将文件的内容添加到问题中的最后更新中,希望它能对任何人有所帮助。

答案3

将您的 .yaml 文件改回您之前的 eth0 版本...

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      dhcp4: yes
      dhcp4-overrides:
        use-dns: no
      nameservers:
        addresses: [192.168.1.7]
  • sudo netplan --debug generate
  • sudo netplan apply
  • reboot

/etc/systemd/resolved.conf可能应该被编辑成看起来像这样......

[Resolve]
DNS=192.168.1.7
#FallbackDNS=
Domains=put_your_domain_here
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=yes
#DNSStubListener=yes
#ReadEtcHosts=yes

reboot

systemd-resolve --status

更新#1:

让我们检查 /etc/resolv.conf 的三个可能的符号链接...

重启后使用 eth0 .yaml 文件...和修改后的 /etc/systemd/resolved.conf...

给我看看...

cat /run/resolvconf/resolv.conf

cat /run/systemd/resolve/resolv.conf

cat /run/systemd/resolve/stub-resolv.conf

更新 #2:

假设 dhcp4-overrides 坏了,让我们尝试这个.yaml……

network:
  version: 2
  renderer: networkd
  ethernets:
    eth0:
      addresses: [x.x.x.x/24]
      gateway4: x.x.x.1
      nameservers:
        addresses: [192.168.1.7]
        search: [mydomain, otherdomain]

sudo netplan --debug generate

sudo netplan apply

reboot

systemd-resolve --status

答案4

对我来说这只是

sudo systemctl restart systemd-resolved

Ubuntu 20.04.1。

相关内容