在我运行 dhclient 之前,DNS 查找在虚拟机中不起作用

在我运行 dhclient 之前,DNS 查找在虚拟机中不起作用

已经花了 4 个小时尝试调试为什么虚拟机启动时 DNS 查找不起作用。

我不仅尝试手动调试问题,还查阅了互联网上的数百篇文章,包括 Ask Ubuntu 上发布的解决方案,但顺便说一句,没有一个起作用。

我正在尝试构建一个 VM 模板(Ubuntu Server 22.04),如果我想启动并运行一个干净的实例,我可以轻松地在 VMware 内部克隆该模板。

当我运行时,DNS 查找开始工作dhclient,但我不想在每次启动时自动启动它,配置文件应该自动定位到要使用的 DNS 服务器,而不是通过自动配置dhclient

如果dhclient必须使用,那么它首先就违背了“手动配置”的目的。

我还应该指出,我已经在/etc/systemd/network目录中做了一些与NAT仅限主持人VMware 提供的接口。

# cd /etc/systemd/network
# cat 001-nat.network
[Match]
Name=ens32

[Network]
Gateway=192.168.33.1
DNS=192.168.33.1
DHCP=yes

# cat 002-host-only.network
[Match]
Name=ens34

[Network]
Gateway=10.0.10.1
DHCP=yes

# cat 003-bridge.network
[Match]
Name=ens35

[Network]
Gateway=192.168.88.1
DHCP=yes

我这样做的原因是,将来我需要具有静态 IP 地址的虚拟机,而这是我所知道的设置方式。

以下是运行 之前和之后ifconfigiproute和的输出:resolvectlpingdhclient

  • ifconfig
# =============== BEFORE ===============
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.33.4  netmask 255.255.255.0  broadcast 192.168.33.255
        inet6 fe80::20c:29ff:feaa:6533  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:33  txqueuelen 1000  (Ethernet)
        RX packets 4  bytes 808 (808.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 12  bytes 1474 (1.4 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.10.6  netmask 255.255.255.0  broadcast 10.0.10.255
        inet6 fe80::20c:29ff:feaa:653d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:3d  txqueuelen 1000  (Ethernet)
        RX packets 151  bytes 13923 (13.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 164  bytes 17651 (17.6 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens35: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.88.242  netmask 255.255.255.0  broadcast 192.168.88.255
        inet6 fe80::20c:29ff:feaa:6547  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:47  txqueuelen 1000  (Ethernet)
        RX packets 49  bytes 4098 (4.0 KB)
        RX errors 0  dropped 35  overruns 0  frame 0
        TX packets 20  bytes 2038 (2.0 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

# =============== AFTER ===============
ens32: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.33.4  netmask 255.255.255.0  broadcast 192.168.33.255
        inet6 fe80::20c:29ff:feaa:6533  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:33  txqueuelen 1000  (Ethernet)
        RX packets 9  bytes 1958 (1.9 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 15  bytes 2500 (2.5 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens34: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 10.0.10.6  netmask 255.255.255.0  broadcast 10.0.10.255
        inet6 fe80::20c:29ff:feaa:653d  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:3d  txqueuelen 1000  (Ethernet)
        RX packets 206  bytes 18791 (18.7 KB)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 202  bytes 23949 (23.9 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

ens35: flags=4163<UP,BROADCAST,RUNNING,MULTICAST>  mtu 1500
        inet 192.168.88.242  netmask 255.255.255.0  broadcast 192.168.88.255
        inet6 fe80::20c:29ff:feaa:6547  prefixlen 64  scopeid 0x20<link>
        ether 00:0c:29:aa:65:47  txqueuelen 1000  (Ethernet)
        RX packets 77  bytes 6471 (6.4 KB)
        RX errors 0  dropped 53  overruns 0  frame 0
        TX packets 26  bytes 2738 (2.7 KB)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0

lo: flags=73<UP,LOOPBACK,RUNNING>  mtu 65536
        inet 127.0.0.1  netmask 255.0.0.0
        loop  txqueuelen 1000  (Local Loopback)
        RX packets 0  bytes 0 (0.0 B)
        RX errors 0  dropped 0  overruns 0  frame 0
        TX packets 0  bytes 0 (0.0 B)
        TX errors 0  dropped 0 overruns 0  carrier 0  collisions 0
  • ip route show
# =============== BEFORE ===============
default via 10.0.10.1 dev ens34 proto static onlink
default via 192.168.33.1 dev ens32 proto static onlink
default via 192.168.88.1 dev ens35 proto static onlink
default via 192.168.33.1 dev ens32 proto dhcp src 192.168.33.4 metric 1024
default via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
1.0.0.1 via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
1.1.1.1 via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
10.0.10.0/24 dev ens34 proto kernel scope link src 10.0.10.6 metric 1024
10.0.10.1 dev ens34 proto dhcp scope link src 10.0.10.6 metric 1024
192.168.33.0/24 dev ens32 proto kernel scope link src 192.168.33.4 metric 1024
192.168.33.1 dev ens32 proto dhcp scope link src 192.168.33.4 metric 1024
192.168.88.0/24 dev ens35 proto kernel scope link src 192.168.88.242 metric 1024
192.168.88.1 dev ens35 proto dhcp scope link src 192.168.88.242 metric 1024

# =============== AFTER ===============
default via 192.168.88.1 dev ens35 proto static onlink
default via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
1.0.0.1 via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
1.1.1.1 via 192.168.88.1 dev ens35 proto dhcp src 192.168.88.242 metric 1024
10.0.10.0/24 dev ens34 proto kernel scope link src 10.0.10.6
192.168.33.0/24 dev ens32 proto kernel scope link src 192.168.33.4
192.168.88.0/24 dev ens35 proto kernel scope link src 192.168.88.242 metric 1024
192.168.88.1 dev ens35 proto dhcp scope link src 192.168.88.242 metric 1024
  • route -n
# =============== BEFORE ===============
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         10.0.10.1       0.0.0.0         UG    0      0        0 ens34
0.0.0.0         192.168.33.1    0.0.0.0         UG    0      0        0 ens32
0.0.0.0         192.168.88.1    0.0.0.0         UG    0      0        0 ens35
0.0.0.0         192.168.33.1    0.0.0.0         UG    1024   0        0 ens32
0.0.0.0         192.168.88.1    0.0.0.0         UG    1024   0        0 ens35
1.0.0.1         192.168.88.1    255.255.255.255 UGH   1024   0        0 ens35
1.1.1.1         192.168.88.1    255.255.255.255 UGH   1024   0        0 ens35
10.0.10.0       0.0.0.0         255.255.255.0   U     1024   0        0 ens34
10.0.10.1       0.0.0.0         255.255.255.255 UH    1024   0        0 ens34
192.168.33.0    0.0.0.0         255.255.255.0   U     1024   0        0 ens32
192.168.33.1    0.0.0.0         255.255.255.255 UH    1024   0        0 ens32
192.168.88.0    0.0.0.0         255.255.255.0   U     1024   0        0 ens35
192.168.88.1    0.0.0.0         255.255.255.255 UH    1024   0        0 ens35

# =============== AFTER ===============
Kernel IP routing table
Destination     Gateway         Genmask         Flags Metric Ref    Use Iface
0.0.0.0         192.168.88.1    0.0.0.0         UG    0      0        0 ens35
0.0.0.0         192.168.88.1    0.0.0.0         UG    1024   0        0 ens35
1.0.0.1         192.168.88.1    255.255.255.255 UGH   1024   0        0 ens35
1.1.1.1         192.168.88.1    255.255.255.255 UGH   1024   0        0 ens35
10.0.10.0       0.0.0.0         255.255.255.0   U     0      0        0 ens34
192.168.33.0    0.0.0.0         255.255.255.0   U     0      0        0 ens32
192.168.88.0    0.0.0.0         255.255.255.0   U     1024   0        0 ens35
192.168.88.1    0.0.0.0         255.255.255.255 UH    1024   0        0 ens35
  • resolvectl status
# =============== BEFORE ===============
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: foreign
Current DNS Server: 1.1.1.1
       DNS Servers: 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4
        DNS Domain: localdomain

Link 2 (ens32)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 192.168.33.1
    DNS Domain: localdomain

Link 3 (ens34)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 10.0.10.1
    DNS Domain: localdomain

Link 4 (ens35)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 1.1.1.1 1.0.0.1

# =============== AFTER ===============
Global
         Protocols: -LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
  resolv.conf mode: foreign
Current DNS Server: 1.1.1.1
       DNS Servers: 1.1.1.1 1.0.0.1 8.8.8.8 8.8.4.4
        DNS Domain: localdomain

Link 2 (ens32)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 192.168.33.1
    DNS Domain: localdomain

Link 3 (ens34)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 10.0.10.1
    DNS Domain: localdomain

Link 4 (ens35)
Current Scopes: DNS
     Protocols: +DefaultRoute +LLMNR -mDNS -DNSOverTLS DNSSEC=no/unsupported
   DNS Servers: 1.1.1.1 1.0.0.1
  • ping -c 8 google.com
# =============== BEFORE ===============
PING google.com (142.250.180.206) 56(84) bytes of data.

--- google.com ping statistics ---
8 packets transmitted, 0 received, 100% packet loss, time 7174ms

# =============== AFTER ===============
PING google.com (142.250.180.206) 56(84) bytes of data.
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=1 ttl=116 time=20.7 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=2 ttl=116 time=20.7 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=3 ttl=116 time=20.4 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=4 ttl=116 time=21.0 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=5 ttl=116 time=26.2 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=6 ttl=116 time=20.7 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=7 ttl=116 time=20.8 ms
64 bytes from bud02s33-in-f14.1e100.net (142.250.180.206): icmp_seq=8 ttl=116 time=22.2 ms

--- google.com ping statistics ---
8 packets transmitted, 8 received, 0% packet loss, time 7012ms
rtt min/avg/max/mdev = 20.400/21.588/26.168/1.806 ms

VMware NAT 配置:

图像

我是 Linux 新手,所以如果我在配置中遗漏了一些重要细节,我深表歉意。这不是我第一次遇到这个问题。

答案1

我通过以下方式解决了这个问题网络计划

以下是我的配置,供将来参考:

/etc/netplan/001-nat.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens32:
      # //
      # // Set both DHCP options to false
      # // when using a static IP
      # //
      dhcp4: true
      dhcp6: false
      # //
      # // Here you put your static IP
      # //
      #addresses:
      #  - 192.168.33.X/prefix
      nameservers:
        addresses: [192.168.33.1]
      routes:
        - to: default
          via: 192.168.33.1

/etc/netplan/002-host-only.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens33:
      # //
      # // Set both DHCP options to false
      # // when using a static IP
      # //
      dhcp4: true
      dhcp6: false
      # //
      # // Here you put your static IP
      # //
      #addresses:
      #  - 10.0.10.X/prefix
      nameservers:
        addresses: [10.0.10.1]

/etc/netplan/003-bridge.yaml

network:
  version: 2
  renderer: networkd
  ethernets:
    ens34:
      # //
      # // Set both DHCP options to false
      # // when using a static IP
      # //
      dhcp4: true
      dhcp6: false
      # //
      # // Here you put your static IP
      # //
      #addresses:
      #  - 192.168.88.X/prefix
      nameservers:
        addresses: [192.168.88.1]

相关内容