ubuntu解决域名不能访问的问题

刚装好的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/
    
  • 大功告成
    在这里插入图片描述
    在这里插入图片描述

  • 4
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值