ubuntu18.04 netplan 設置dns,dns不生效


ubuntu18.04通過netplan設置DNS,而查看DNS時發現DNS並沒生效,其原因是系統指向的問題。

以下是解決步驟:

1.編輯netplan下的yaml文件
2.重啟網絡 netplan apply
3.刪除原resolv.conf, rm -rf /etc/resolv.conf
4.創建軟連接,ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf

對於ubuntu18.04的環境,提供了用yaml文件形式修改網絡配置,但系統初始設置的/etc/resolv.conf -> /run/systemd/resolv/stub-resolv.conf,如果我們通過/etc/netplan/xxx.yaml修改我們的網絡配置,假如更改了DNS,這相當於用戶設置的,對應在網卡重啟后,即netplan apply后更新的dns配置文件是/run/systemd/resolv/resolv.conf,系統的stub-resolv.conf還是原內容,此時我們通過/etc/resolv.conf查看DNS的配置,其實還是系統初始的軟連接,所以如果存在文件用到/etc/resolv.conf時,一定要在網卡重啟后,刪除原先的/etc/resolv.conf,重新創建軟連接,從而使根據用戶修改的網絡DNS配置指向/etc/resolv.conf,否則還是指向系統的鏈接指向。

重點

  • 系統初始鏈接: /etc/resolv.conf -> /run/systemd/resolv/stub-resolv.conf
  • /run/systemd/resolv/resolv.conf: 用戶配置網絡的DNS,重啟后更新此文件
  • /run/systemd/resolv/stub-resolv.conf: 系統配置文件,默認127.0.0.53
  • 用戶更改網絡的DNS,指向/etc/resolv.conf的文件應改為 /run/systemd/resolv/resolv.conf

更多

外網解答:

Using resolv.conf instead of stub-resolv.conf will bypass a lot of systemd-resolved configuration, such as DNS answer caching, per-interface DNS configuration, DNSSec enforcement, etc.

When using stub-resolv.conf, applications will make DNS requests to the DNS stub resolver provided by systemd on address 127.0.0.53. This stub resolver will proxy the DNS requests to the upstream DNS resolvers configured in systemd-resolved, applying whatever logic it wants to those requests and answers, like caching them.

When using resolv.conf, applications will directly make DNS requests to the "real" (aka. upstream) DNS resolvers configured in systemd-resolved. In this case, systemd-resolved only acts as a "resolv.conf manager", not as a DNS resolver itself.

參考:


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM