我正在尝试在 Ubuntu 20.04 桌面上设置 DNS 服务器。到目前为止,这就是我所做的。
/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=
#FallbackDNS=
#Domains=
#LLMNR=no
#MulticastDNS=no
#DNSSEC=no
#DNSOverTLS=no
#Cache=no-negative
#DNSStubListener=yes
#ReadEtcHosts=yes
由于所有内容都已注释,这意味着这里未设置 DNS 服务器。
在 中/etc/resolve.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 *domain*.com
在 中/etc/netplan/**-netconf-**.yaml
,内容如下:
network:
version: 2
renderer: networkd
ethernets:
ens160:
dhcp4: yes
dhcp4-overrides:
use-dns: false
dhcp6: yes
dhcp6-overrides:
use-dns: false
nameservers:
addresses:
- ***********
- ***********
- ***********
只是为了安全起见,我在 IP 地址上放置了 ***********,但它们不是我运行systemd-resolve --status
或时显示的resolvectl status
:
Link 2 (ens160)
Current Scopes: DNS
DefaultRoute setting: yes
LLMNR setting: yes
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
Current DNS Server: 8.8.8.8
DNS Servers: 8.8.8.8
8.8.4.4
8.8.2.2
DNS Domain: **domain**.com
DNS 服务器和 DNS 域与我发布的安全措施并不完全相同。但我只是想表明它与/etc/netplan/**-netconf-**.yaml
最后,我要说的是,该文件/run/systemd/resolve/resolv.conf
确实有 中显示的 DNS 服务器systemd-resolve --status
。这是 的内容/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 8.8.8.8
nameserver 8.8.4.4
nameserver 8.8.2.2
search **domain**.com
但是!当我编辑此文件并更改 DNS 服务器地址时,运行时它不会更改systemd-resolve --status
。而当我运行时sudo systemctl restart systemd-resolved
,它只会恢复到原始状态。
nameserver 8.8.8.8
nameserver 8.8.4.4
nameserver 8.8.2.2
search **domain**.com
我不知道从哪里获得 DNS,以便在重置 systemd-resolved 后恢复到它。
答案1
使用 Ubuntu 20.04 桌面安装,网络由以下方式管理网络管理器并不是systemd-networkd. 因此,您当前的 Netplan YAML 配置文件将不会生效。
通常,网络配置(特别是 DNS)是在 GUI 中定义的。但由于您已表明要通过命令行设置 DNS 服务器,因此您可以使用 来执行此操作nmcli
。
另请注意,默认情况下,Ubuntu 使用本地存根解析器来缓存 DNS 查询,以便更快地进行解析。即使您想定义特定的上行链路 DNS 服务器,我仍然建议您利用存根解析器。
您已表明您已编辑/run/systemd/resolve/resolv.conf/
。请不要这样做。文件中明确指出:
# This file is managed by man:systemd-resolved(8). Do not edit.
首先使用以下命令返回
/etc/resolv.conf
指向的符号链接。/run/systemd/resolve/stub-resolv.conf
sudo ln -s /run/systemd/resolve/stub-resolv.conf /etc/resolv.conf
接下来,将您的 Netplan YAML 文件恢复为默认值。
删除当前的 Netplan YAML 文件:
sudo rm /etc/netplan/**-netconf-**.yaml
使用您选择的编辑器创建一个新的 Netplan YAML 文件:
/etc/netplan/01-network-manager-all.yaml
# Let NetworkManager manage all devices on this system network: version: 2 renderer: NetworkManager
为您的连接设置特定的 DNS 服务器。让我们以我创建的虚拟机为例。
显示你的连接:
$ nmcli connection NAME UUID TYPE DEVICE Wired connection 1 882bdeaa-c11e-36cb-bef4-b89fff1d9aa0 ethernet enp0s3
在此示例中,连接名为“有线连接1'。因此,如果您的情况不同,请相应地进行调整。
让我们忽略从您的 DHCP 服务器推送给您的任何 DNS 服务器:
nmcli connection modify 'Wired connection 1' ipv4.ignore-auto-dns yes
向您的连接添加 DNS 服务器:
nmcli connection modify 'Wired connection 1' ipv4.dns 8.8.8.8
如果您想添加额外的 DNS 服务器:
nmcli connection modify 'Wired connection 1' +ipv4.dns 1.1.1.1
如果您想一次设置多个 DNS 服务器,而不是使用多个命令:
nmcli connection modify 'Wired connection 1' ipv4.dns "8.8.8.8, 1.1.1.1"
要设置搜索域:
nmcli connection modify 'Wired connection 1' ipv4.dns-search localdomain
完成修改后,重新加载连接:
sudo nmcli connection reload
最后,重新启动网络管理器:
sudo systemctl restart NetworkManager
此时,您的 DNS 服务器和搜索域将设置为您的连接的上行链路 DNS 服务器和搜索域。要查看此内容,请运行resolvectl
。请注意底部为 Link 2 设置的 DNS 服务器和搜索域。这是针对设备的enp0s3
,它与我们用 修改的连接的设备相匹配nmcli
。
$ resolvectl
Global
LLMNR setting: no
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
DNSSEC NTA: 10.in-addr.arpa
16.172.in-addr.arpa
168.192.in-addr.arpa
17.172.in-addr.arpa
18.172.in-addr.arpa
19.172.in-addr.arpa
20.172.in-addr.arpa
21.172.in-addr.arpa
22.172.in-addr.arpa
23.172.in-addr.arpa
24.172.in-addr.arpa
25.172.in-addr.arpa
26.172.in-addr.arpa
27.172.in-addr.arpa
28.172.in-addr.arpa
29.172.in-addr.arpa
30.172.in-addr.arpa
31.172.in-addr.arpa
corp
d.f.ip6.arpa
home
internal
intranet
lan
local
private
test
Link 2 (enp0s3)
Current Scopes: DNS
DefaultRoute setting: yes
LLMNR setting: yes
MulticastDNS setting: no
DNSOverTLS setting: no
DNSSEC setting: no
DNSSEC supported: no
Current DNS Server: 8.8.8.8
DNS Servers: 8.8.8.8
1.1.1.1
DNS Domain: ~.
localdomain
最终结果是,nmcli
在 中创建了一个配置文件/etc/NetworkManager/system-connections/
。看一下上述命令创建的文件:
$ sudo cat /etc/NetworkManager/system-connections/'Wired connection 1.nmconnection'
[connection]
id=Wired connection 1
uuid=882bdeaa-c11e-36cb-bef4-b89fff1d9aa0
type=ethernet
autoconnect-priority=-999
interface-name=enp0s3
permissions=
timestamp=1709607559
[ethernet]
mac-address-blacklist=
[ipv4]
dns=8.8.8.8;1.1.1.1;
dns-search=localdomain;
ignore-auto-dns=true
method=auto
[ipv6]
addr-gen-mode=stable-privacy
dns-search=
method=auto
[proxy]
您还会注意到/run/systemd/resolve/resolv.conf
您的 DNS 服务器和搜索域已更新:
$ 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 8.8.8.8
nameserver 1.1.1.1
search localdomain
了解更多信息:
Ubuntu 网站:
Arch Linux 网站: