我从利诺德安装了 Ubuntu 20.04 LTS (Focal Fossa)。但是 Linode 的默认 DNS 服务器无法解析我真正需要它解析的域名。因此,我尝试编辑我的 netplan 配置以将 DNS 服务器更改为 Google 的 DNS 服务器,因为 VPS 显然是无头的。我编辑了文件 /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:
eth0:
dhcp4: yes
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
Ubuntu 20.04 默认使用 networkd 作为 netplan 后端。我检查了 YAML,发现格式正确。DHCP 默认处于开启状态(例如通过设置 dhcp4: yes),将其关闭(通过设置 dhcp4: no)无法解决问题。根据 ,eth0 是我的网络连接名称ip a
。但是,每当我sudo netplan apply
在保存 netplan 配置文件后运行时,活动名称服务器似乎并没有根据 进行更改。即使通过运行和或通过运行重新启动 systemd-resolvedsudo systemd-resolve --status
强制重新启动 networkd也不起作用。sudo ip link set eth0 down
sudo ip link set eth0 up
sudo systemctl restart systemd-resolved
为什么更改 Ubuntu Server 20.04 LTS 上的 DNS 服务器如此困难?我是不是漏掉了什么?
非常感谢你的帮助,
Joshua
编辑:
输出sudo lshw -C network
:
*-network
description: Ethernet controller
product: Virtio network device
vendor: Red Hat, Inc.
physical id: 4
bus info: pci@0000:00:04.0
version: 00
width: 64 bits
clock: 33MHz
capabilities: msix bus_master cap_list rom
configuration: driver=virtio-pci latency=0
resources: irq:20 ioport:c080(size=64) memory:febd3000-febd3fff memory:fe
008000-fe00bfff memory:feb80000-febbffff
*-virtio2
description: Ethernet interface
physical id: 0
bus info: virtio@2
logical name: eth0
serial: f2:3c:93:91:e5:ed
capabilities: ethernet physical
configuration: autonegotiation=off broadcast=yes driver=virtio_net
driverversion=1.0.0 ip=170.187.185.46 link=yes multicast=yes
输出sudo ls -al /etc/resolv.conf
:
lrwxrwxrwx 1 root root 37 Feb 24 06:53 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
输出sudo cat /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 "resolvectl 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 trust-ad
search members.linode.com
输出sudo cat /etc/network/interfaces
:
# ifupdown has been replaced by netplan(5) on this system. See
# /etc/netplan for current configuration.
# To re-enable ifupdown on this system, you can run:
# sudo apt install ifupdown
输出cat /etc/netplan/*.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: true
dhcp4-overrides:
use-dns: false
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
答案1
去https://netplan.io/reference/并查看DHCP 覆盖。
/etc/netplan/01-netcfg.yaml...
network:
version: 2
renderer: networkd
ethernets:
eth0:
dhcp4: true
dhcp4-overrides:
use-dns: false
nameservers:
addresses: [8.8.8.8, 8.8.4.4]
sudo netplan generate
sudo netplan apply
reboot
# 如果需要
答案2
这里的问题是,默认情况下 systemd 不会在本地网络上搜索主机名。要解决此问题,您需要编辑 netplan 配置文件以在名称服务器中包含搜索字段。
例如,要搜索名为 tacos.local 的本地域,如果您的名称服务器是 192.168.1.1 和 .2,则需要使其看起来像这样。然而,出于某种原因,这也会给您返回一个非权威的答案。
nameservers:
search: [tacos.local]
addresses: [192.168.1.1,192.168.1.2]
答案3
以下命令显示/etc/resolve.conf
指向存根解析器,它是一个本地缓存名称服务器。
sudo ls -al /etc/resolv.conf
lrwxrwxrwx 1 root root 37 Feb 24 06:53 /etc/resolv.conf -> /run/systemd/resolve/stub-resolv.conf
如果您想使用上行链路名称服务器,只需更改符号链接/etc/resolv.conf
指向的文件:/run/systemd/resolve/resolv.conf
。
sudo rm /etc/resolve.conf
sudo ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
手册页中概述了这一点systemd-resolved
:systemd-resolved 手册页
/ETC/RESOLV.CONF top
Four modes of handling /etc/resolv.conf (see resolv.conf(5)) are
supported:
• systemd-resolved maintains the
/run/systemd/resolve/stub-resolv.conf file for compatibility
with traditional Linux programs. This file lists the
127.0.0.53 DNS stub (see above) as the only DNS server. It
also contains a list of search domains that are in use by
systemd-resolved. The list of search domains is always kept
up-to-date. Note that /run/systemd/resolve/stub-resolv.conf
should not be used directly by applications, but only through
a symlink from /etc/resolv.conf. This file may be symlinked
from /etc/resolv.conf in order to connect all local clients
that bypass local DNS APIs to systemd-resolved with correct
search domains settings. This mode of operation is
recommended.
• A static file /usr/lib/systemd/resolv.conf is provided that
lists the 127.0.0.53 DNS stub (see above) as only DNS server.
This file may be symlinked from /etc/resolv.conf in order to
connect all local clients that bypass local DNS APIs to
systemd-resolved. This file does not contain any search
domains.
• systemd-resolved maintains the
/run/systemd/resolve/resolv.conf file for compatibility with
traditional Linux programs. This file may be symlinked from
/etc/resolv.conf and is always kept up-to-date, containing
information about all known DNS servers. Note the file
format's limitations: it does not know a concept of
per-interface DNS servers and hence only contains system-wide
DNS server definitions. Note that
/run/systemd/resolve/resolv.conf should not be used directly
by applications, but only through a symlink from
/etc/resolv.conf. If this mode of operation is used local
clients that bypass any local DNS API will also bypass
systemd-resolved and will talk directly to the known DNS
servers.
• Alternatively, /etc/resolv.conf may be managed by other
packages, in which case systemd-resolved will read it for DNS
configuration data. In this mode of operation
systemd-resolved is consumer rather than provider of this
configuration file.
Note that the selected mode of operation for this file is
detected fully automatically, depending on whether
/etc/resolv.conf is a symlink to /run/systemd/resolve/resolv.conf
or lists 127.0.0.53 as DNS server.