1. 程式人生 > 其它 >Ubuntu 20.04設定DNS解析(解決resolve.conf被覆蓋問題)。

Ubuntu 20.04設定DNS解析(解決resolve.conf被覆蓋問題)。

ubuntu在域名解析時,最直接使用的是/etc/resolve.conf檔案,它是/run/systemd/resolve/
resolve.conf的軟連結,而對於剛裝完的ubuntu系統,該檔案的內容如下:

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

直接修改/etc/resolv.conf中的nameserver 配置,例如nameserver 192.168.4.2,修改完成後,重啟服務會重新還原檔案。
解決:

  1. 修改/etc/systemd/resolved.conf,例如:
    [Resolve]
    DNS=8.8.8.8 114.114.114.114 192.168.4.2
    2.重啟域名解析服務
    systemctl restart systemd-resolved
    systemctl enable systemd-resolved
    3.備份當前的/etc/resolve.conf,並重新設定/run/systemd/resolve/resolv.conf 到/etc/resolve.conf的軟連結
    mv /etc/resolv.conf /etc/resolv.conf.bak
    ln -s /run/systemd/resolve/resolv.conf /etc/resolv.conf
    4.檢視驗證,
    cat /etc/resolv.conf
    重啟服務再檢視。ping www.baidu.com可以ping通。