在 Ubuntu系统中,虽然在网卡中配置了DNS服务器的IP地址,但在使用相关命令进行DNS解析时, 默认的 DNS 服务器使用的是 127.0.0.53,而并不是我们在网卡上配置的DNS 服务器地址。

[root@ubuntu ~]# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
 renderer: NetworkManager
 ethernets:
   eth0:
      #dhcp4: true
     addresses: [10.0.0.206/24]
     gateway4: 10.0.0.2
     nameservers:
       search: []
       addresses: [223.6.6.6,8.8.8.8]
 version: 2
[root@ubuntu ~]# nslookup www.baidu.com
Server: 127.0.0.53
Address: 127.0.0.53#53
Non-authoritative answer:
Name: www.baidu.com
Address: 39.156.70.239

systemd-resolved服务为本地应用程序提供了网络名字解析服务, 系统通过它对外进行DNS请求。

[root@ubuntu ~]# ll /etc/resolv.conf
lrwxrwxrwx 1 root root 39 Apr 21  2022 /etc/resolv.conf -> 
../run/systemd/resolve/stub-resolv.conf
[root@ubuntu ~]# cat /etc/resolv.conf 
......
nameserver 127.0.0.53 #默认DNS 配置在此处
options edns0 trust-ad
search .
#直接修改上述文件,将
[root@ubuntu ~]# vim /etc/resolv.conf
......
#nameserver 127.0.0.53
nameserver 223.6.6.6
#测试
[root@ubuntu ~]# nslookup www.baidu.com
Server: 223.6.6.6
Address: 223.6.6.6#53
Non-authoritative answer:
Name: www.baidu.com
Address: 39.156.70.239

但是只要再次重启网络相关,DNS内容会被还原。

[root@ubuntu ~]# netplan apply
[root@ubuntu ~]# cat /etc/resolv.conf | grep nameserver
nameserver 127.0.0.53

修改软链接文件指向,保证DNS永久生效。

[root@ubuntu ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Apr 21  2022 /etc/resolv.conf -> 
../run/systemd/resolve/stub-resolv.conf
[root@ubuntu ~]# rm -f /etc/resolv.conf
[root@ubuntu ~]# ln -sv /run/systemd/resolve/resolv.conf /etc/resolv.conf
'/etc/resolv.conf' -> '/run/systemd/resolve/resolv.conf'
[root@ubuntu ~]# ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 32 Jun  1 11:26 /etc/resolv.conf -> 
/run/systemd/resolve/resolv.conf
[root@ubuntu ~]# nslookup www.baidu.com
Server: 223.6.6.6
Address: 223.6.6.6#53
Non-authoritative answer:
Name: www.baidu.com
Address: 39.156.70.239

若此时删除网卡配置文件中设置DNS,重启网络服务,查看DNS,无法ping通外网域名(需设置全局DNS)

#修改网卡配置,注释掉DNS配置
[root@ubuntu ~]# cat /etc/netplan/00-installer-config.yaml 
# This is the network config written by 'subiquity'
network:
 renderer: NetworkManager
 ethernets:
   eth0:
      #dhcp4: true
     addresses: [10.0.0.206/24]
     gateway4: 10.0.0.2
     nameservers:
       search: []
          #addresses: [223.6.6.6,8.8.8.8]
 version: 2
#重启网络服务
[root@ubuntu ~]# netplan apply
#再次查看DNS
[root@ubuntu ~]# cat /etc/resolv.conf 
......
# No DNS servers known.
search .
#无法使用域名解析服务
[root@ubuntu ~]# ping www.baidu.com
ping: www.baidu.com: Temporary failure in name resolution

若网卡配置无DNS设置,设置系统全局DNS,若网卡配置有DNS设置,此步骤可忽略,此时主机通过全局DNS解析域名。

[root@ubuntu ~]# vim /etc/systemd/resolved.conf
......
DNS=223.5.5.5 223.6.6.6
#重启服务
[root@ubuntu ~]# systemctl restart systemd-resolved.service
#查看
[root@ubuntu ~]# cat /etc/resolv.conf
......
nameserver 223.5.5.5
nameserver 223.6.6.6
#测试
[root@ubuntu ~]# nslookup www.baidu.com
Server: 223.6.6.6
Address: 223.6.6.6#53
Non-authoritative answer:
Name: www.baidu.com
Address: 39.156.70.239

网卡配置文件加DNS,此时主机即可通过全局DNS解析,也可通过网卡配置文件中的DNS解析

[root@ubuntu ~]# cat /etc/netplan/00-installer-config.yaml
# This is the network config written by 'subiquity'
network:
 renderer: NetworkManager
 ethernets:
   eth0:
      #dhcp4: true
     addresses: [10.0.0.206/24]
     gateway4: 10.0.0.2
     nameservers:
       search: []
       addresses: [10.0.0.206]
 version: 2
 [root@ubuntu ~]# netplan apply
#查看
[root@ubuntu ~]# cat /etc/resolv.conf 
......
nameserver 223.5.5.5
nameserver 223.6.6.6
nameserver 10.0.0.206
search .