禁用 Ubuntu 使用 systemd-resolved 进行 DNS 解析

后知后觉 暂无评论

Ubuntu 系列的发行版默认使用 systemd-resolved 组件进行 DNS 解析,组件会占用本地所有的 53 端口。

为了解除 DNS 占用,将其替换。

禁用

先进行一下解析,可以发现由本地的 53 端口的 systemd-resolved 服务进行解析,

$ dig bing.com

; <<>> DiG 9.18.12-0ubuntu0.22.04.1-Ubuntu <<>> bing.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20101
;; flags: qr rd ra; QUERY: 1, ANSWER: 2, AUTHORITY: 0, ADDITIONAL: 1

;; OPT PSEUDOSECTION:
; EDNS: version: 0, flags:; udp: 65494
;; QUESTION SECTION:
;bing.com.                      IN      A

;; ANSWER SECTION:
bing.com.               2657    IN      A       13.107.21.200
bing.com.               2657    IN      A       204.79.197.200

;; Query time: 0 msec
;; SERVER: 127.0.0.53#53(127.0.0.53) (UDP)
;; WHEN: Tue May 02 14:00:00 CST 2023
;; MSG SIZE  rcvd: 69

尝试禁用 systemd-resolved 服务

sudo systemctl status systemd-resolved.service
sudo systemctl stop systemd-resolved.service

再次尝试解析,可见已经无法解析

$ dig bing.com
;; communications error to 127.0.0.53#53: connection refused
;; communications error to 127.0.0.53#53: connection refused
;; communications error to 127.0.0.53#53: connection refused

; <<>> DiG 9.18.12-0ubuntu0.22.04.1-Ubuntu <<>> bing.com
;; global options: +cmd
;; no servers could be reached

这是因为 systemd-resolved 将系统的解析文件进行了替换,可见 /etc/resolv.conf 变为了软链。

$ ll /etc/resolv.conf 
lrwxrwxrwx 1 root root 39 Apr 23  2020 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf

将其删除,重新生成配置文件

sudo rm -f /etc/resolv.conf
echo "nameserver 1.1.1.1" | sudo tee /etc/resolv.conf
小贴士:如果有多个 DNS 需要配置可以按格式加入 resolv.conf 中,一行一个即可。

附录

参考链接

如果遇到问题或者对文章内容存疑,请在下方留言,博主看到后将及时回复,谢谢!
禁用 / 当前已拒绝评论,仅可查看「历史评论」。