Ubuntu 22.04默认DNS架构 127.0.0.1:53

1.Ubuntu 22.04默认DNS 架构

1
2
3
4
5
6
7
应用程序

127.0.0.53(systemd-resolved 本地DNS代理)

真正上游DNS
223.5.5.5
114.114.114.114

即:

1
glibc -> systemd-resolved -> 阿里DNS/114DNS

架构如下:

cur/ping/apt -> /etc/resolv.conf -> 127.0.0.53 -> systemd-resolved -> 223.5.5.5

因此:

  • 127.0.0.53 只是本地 stub
  • 真正 DNS 在 systemd-resolved 内部

/etc/resolv.conf 实际上是一个指向 /run/systemd/resolve/stub-resolv.conf 的软链接。


2.验证真正 DNS 是否生效

执行:

1
2
resolvectl status
resolvectl query qq.com

或者(老版本查询命令):

1
systemd-resolve --status

你会看到:

1
2
3
4
Link 2 (enp131s0)
Current Scopes: DNS
DNS Servers: 223.5.5.5
114.114.114.114

说明 netplan 已经成功下发 DNS。


3.进一步验证

3.1 查看 systemd-resolved 使用的 DNS

1
resolvectl dns

结果类似:

1
2
Global:
Link 2 (enp131s0): 223.5.5.5 114.114.114.114

3.2 实际 DNS 查询

1
dig baidu.com

你会看到:

1
;; SERVER: 127.0.0.53#53(127.0.0.53)

这只是本地代理继续看:

1
resolvectl query baidu.com

会显示最终 upstream。


4.netplan 配置本身没问题

配置/etc/netplan/00-installer-config.yaml:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
network:
ethernets:
enp6s18:
addresses:
- 10.6.1.2/24
nameservers:
addresses:
- 223.5.5.5
- 223.6.6.6
search: []
routes:
- to: default
via: 10.6.1.254
version: 2

应用后:

1
netplan apply

5.为什么 Ubuntu 要这样设计

Ubuntu 20+/22+使用:

  • DNS 缓存
  • DNSSEC
  • split DNS
  • VPN DNS
  • 多网卡路由 DNS

这些都需要:

1
systemd-resolved

因此不再直接把真实 DNS 写入 /etc/resolv.conf


6.如果你想直接看到真实 DNS

可以关闭 stub resolver。


6.1 修改 resolved.conf

编辑:

1
/etc/systemd/resolved.conf

增加:

1
2
[Resolve]
DNSStubListener=no

6.2 删除旧 resolv.conf

1
rm -f /etc/resolv.conf

6.3 建立新链接

1
ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

这个文件会直接显示真实 DNS:

1
2
nameserver 223.5.5.5
nameserver 114.114.114.114

6.4 重启

1
2
systemctl status systemd-resolved
systemctl restart systemd-resolved

7.生产建议

通常:

  • 不建议关闭 systemd-resolved
  • 保持默认 stub 模式即可

因为:

  • 支持 DNS cache
  • 支持容器
  • 支持 VPN split-horizon DNS
  • 支持多 uplink
  • 避免应用直接依赖 DNS server