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

在Ubuntu22.04或20.04中遇到无法访问百度的问题,根源在于DNS解析故障。可以通过修改/etc/systemd/resolved.conf文件,设置DNS服务器为8.8.8.8和114.114.114.114,并重启systemd-resolved服务来解决。此外,需要确保systemd-resolved的配置生效并创建指向/run/systemd/resolve/resolv.conf的软链接。

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

### 问题分析 在 Ubuntu 终端中出现 "temporary failure in name resolution" 错误,通常表示系统无法解析域名。这可能是由于 DNS 配置不正确、网络连接问题或 DNS 缓存服务异常引起的。 ### 解决方法 #### 1. 检查网络连接 确保系统已正确连接到互联网,并且可以访问外部网络。可以通过以下命令测试网络连通性: ```bash ping -c 4 google.com ``` 如果无法 ping 通,则需要检查物理网络连接或虚拟机网络设置。 #### 2. 配置 DNS 解析器 编辑 `/etc/resolv.conf` 文件,手动指定公共 DNS 服务器。例如,使用 Google 的公共 DNS: ```bash echo "nameserver 8.8.8.8" | sudo tee /etc/resolv.conf > /dev/null ``` 或者,在某些系统中,建议将 DNS 配置写入 `/etc/resolvconf/resolv.conf.d/base`: ```bash echo "nameserver 8.8.8.8" | sudo tee /etc/resolvconf/resolv.conf.d/base > /dev/null ``` 执行完上述操作后,重新生成 `resolv.conf` 文件: ```bash sudo resolvconf -u ``` #### 3. 检查 APT 配置中的镜像源 如果问题是出现在尝试通过 `apt-get` 安装软件包时,可能是因为当前使用的镜像源不可用。可以尝试更换为更稳定的镜像源,例如中国科学技术大学的镜像源。修改 `/etc/apt/sources.list` 或添加新的 Docker CE 软件源时,确保使用正确的 Codename(如 focal): ```bash sudo add-apt-repository \ "deb [arch=amd64] https://mirrors.ustc.edu.cn/docker-ce/linux/ubuntu/ \ focal \ stable" ``` 更新 APT 缓存以确保配置生效: ```bash sudo apt update ``` #### 4. 启用并重启网络管理服务 有时,网络管理服务(如 NetworkManager 或 systemd-resolved)可能出现问题。可以尝试重启这些服务: ```bash sudo systemctl restart NetworkManager # 或者 sudo systemctl restart systemd-resolved ``` 此外,确保 `systemd-resolved` 的配置文件 `/etc/systemd/resolved.conf` 中启用了 DNS 解析功能: ```ini DNS=8.8.8.8 FallbackDNS=8.8.4.4 ``` 重启服务后,再次检查 DNS 解析是否正常: ```bash nslookup google.com ``` #### 5. 检查防火墙和代理设置 如果系统处于受限制的网络环境中,可能存在代理设置影响了 DNS 解析。检查环境变量中的代理配置: ```bash echo $http_proxy echo $https_proxy ``` 如果存在代理设置,但不需要使用,可以清除它们: ```bash unset http_proxy unset https_proxy ``` --- ###
评论 1
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值