前段時間,我偶然進行查看linux DNS配置,通過查看 /etc/resolv.conf 文件,得到如下結果:
➜ devices ll /etc/resolv.conf lrwxrwxrwx 1 root root 39 12月 28 18:55 /etc/resolv.conf -> ../run/systemd/resolve/stub-resolv.conf ➜ devices 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
之前,我曾經簡單寫過一個linux下如何獲取dns服務器的博客:Linux 中查看 DNS 與 配置;但是無論怎么查找,顯示的都是 127.0.0.53 回環地址。說明 ubuntu 對dns服務器做了一層管理,但是內部的機制我還不是很明白。所以今天進行簡要了解一下,填一下年前遺留的一個坑。通過上述注釋我們可以了解到,我們可以使用 resolvectl status 命令查看上行鏈路的dns服務器。我們也可以使用自己創建regular文件進行手動配置;命令運行情況如下:
這顯然於我們在netplan中配置的dns相同:
但是,127.0.0.53 代表的是什么呢,其存在的必要性是什么呢?我對此進行了進一步探究,並進行簡要記錄,方便我個人記憶,如果對你也有啟發,歡迎留言討論:
- Resolved is a local, caching, DNS nameserver resolver and is used by default on Ubuntu.
- https://www.freedesktop.org/software/systemd/man/systemd-resolved.service.html (通過閱讀此鏈接很多問題可得到答案)
- 關於配置文件,可以參考:/etc/systemd/resolved.conf
- 相關講座:https://ftp.belnet.be/mirror/FOSDEM/2019/K.4.601/dns_systemd_resolved.webm
- systemd-resolved 帶來的好處,一個是統一了dns的管理;另外一個就是可以通過本地cache加速dns查詢;
參考文檔:
How and why (not) to use the 127.0.0.53 nameserver, systemd-resolved and resolvctl