刚装好的ubuntu22.04或ubuntu20.04不能访问百度,然后发现只能ping通ip,而不能ping通www.baidu.com,网上查了下解决方案,
-
方案一:修改dns:
修改/etc/resolv.conf
配置文件,但很快就会被覆盖,文件第二行已经声明Do not edit.
# 显示如下 # This is /run/systemd/resolve/stub-resolv.conf managed by man:systemd-resolved(8). # Do not edit. # # This file might be symlinked as /etc/resolv.conf. If you're looking at # /etc/resolv.conf and seeing this text, you have followed the symlink. # # 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 should typically 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 nameserver 8.8.8.8 nameserver 114.114.114.114 options edns0 trust-ad search .
将
nameserver
改为8.8.8.8
后保存,再次ping www.baidu.com
恢复正常,不出意外的话很快就会发现/etc/resolve.conf
又回去了,只好再次寻找查找解决办法 -
方案二:
修改/etc/systemd/resolved.conf
,去除DNS前的#,修改值如下:# This file is part of systemd. 2 # 3 # systemd is free software; you can redistribute it and/or modify it under the 4 # terms of the GNU Lesser General Public License as published by the Free 5 # Software Foundation; either version 2.1 of the License, or (at your option) 6 # any later version. 7 # 8 # Entries in this file show the compile time defaults. Local configuration 9 # should be created by either modifying this file, or by creating "drop-ins" in 10 # the resolved.conf.d/ subdirectory. The latter is generally recommended. 11 # Defaults can be restored by simply deleting this file and all drop-ins. 12 # 13 # Use 'systemd-analyze cat-config systemd/resolved.conf' to display the full config. 14 # 15 # See resolved.conf(5) for details. 16 17 [Resolve] 18 # Some examples of DNS servers which may be used for DNS= and FallbackDNS=: 19 # Cloudflare: 1.1.1.1#cloudflare-dns.com 1.0.0.1#cloudflare-dns.com 2606:4700:4700:: 1111#cloudflare-dns.com 2606:4700:4700::1001#cloudflare-dns.com 20 # Google: 8.8.8.8#dns.google 8.8.4.4#dns.google 2001:4860:4860::8888#dns.google 2001:4860:4860::8844#dns.google 21 # Quad9: 9.9.9.9#dns.quad9.net 149.112.112.112#dns.quad9.net 2620:fe::fe#dns.qu ad9.net 2620:fe::9#dns.quad9.net 22 DNS=8.8.8.8 114.114.114.114 23 #FallbackDNS= 24 #Domains= 25 #DNSSEC=no 26 #DNSOverTLS=no 27 #MulticastDNS=no 28 #LLMNR=no 29 #Cache=no-negative 30 #CacheFromLocalhost=no 31 #DNSStubListener=yes 32 #DNSStubListenerExtra= 33 #ReadEtcHosts=yes 34 #ResolveUnicastSingleLabel=no
重启域名解析服务
systemctl restart systemd-resolved systemctl enable systemd-resolved
备份当前的/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/
-
大功告成