执行sudo apt update时遭遇Ubuntu源域名解析临时失败问题求助
嗨,看起来你在执行sudo apt update的时候碰到了域名解析的问题,这种情况我帮很多用户排查过,大多是DNS配置或者网络连通性的小问题,咱们一步步来解决:
首先先把你遇到的错误日志贴出来方便后续参考:
root@ubuntu:/# sudo apt update Err:1 http://archive.ubuntu.com/ubuntu focal InRelease Temporary failure resolving 'archive.ubuntu.com' Err:2 http://security.ubuntu.com/ubuntu focal-security InRelease Temporary failure resolving 'security.ubuntu.com' Err:3 http://ports.ubuntu.com/ubuntu-ports focal InRelease Temporary failure resolving 'ports.ubuntu.com' Err:4 http://archive.ubuntu.com/ubuntu focal-updates InRelease Temporary failure resolving 'archive.ubuntu.com' Err:5 http://ports.ubuntu.com/ubuntu-ports focal-updates InRelease Temporary failure resolving 'ports.ubuntu.com' Err:6 http://archive.ubuntu.com/ubuntu focal-backports InRelease Temporary failure resolving 'archive.ubuntu.com' Err:7 http://ports.ubuntu.com/ubuntu-ports focal-backports InRelease Temporary failure resolving 'ports.ubuntu.com' Err:8 http://ports.ubuntu.com/ubuntu-ports focal-security InRelease Temporary failure resolving 'ports.ubuntu.com' Reading package lists... Done Building dependency tree Reading state information... Done All packages are up to date. W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-updates/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/focal-backports/InRelease Temporary failure resolving 'archive.ubuntu.com' W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/focal-security/InRelease Temporary failure resolving 'security.ubuntu.com' W: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/dists/focal/InRelease Temporary failure resolving 'ports.ubuntu.com' W: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/dists/focal-updates/InRelease Temporary failure resolving 'ports.ubuntu.com' W: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/dists/focal-backports/InRelease Temporary failure resolving 'ports.ubuntu.com' W: Failed to fetch http://ports.ubuntu.com/ubuntu-ports/dists/focal-security/InRelease Temporary failure resolving 'ports.ubuntu.com' W: Some index files failed to download. They have been ignored, or old ones used instead.
这个错误提示Temporary failure resolving说明你的系统没办法把Ubuntu源的域名转换成对应的IP地址,核心问题出在DNS解析上,下面是具体的排查和解决步骤:
第一步:先确认基础网络是否连通
先试试ping公共DNS服务器,比如Google的8.8.8.8,看看网络本身有没有问题:ping -c 4 8.8.8.8如果能收到响应,说明网络是通的,问题就集中在DNS解析;如果ping不通,那得先解决网络连接问题——比如检查网线插好没、WiFi是否正常连接,或者路由器有没有故障。
第二步:临时更换DNS服务器测试
如果网络没问题,那可以先临时换个靠谱的DNS服务器试试:- 打开DNS配置文件:
nano /etc/resolv.conf - 把里面的内容替换成这两行:
nameserver 8.8.8.8 nameserver 8.8.4.4 - 按
Ctrl+O保存,回车确认,再按Ctrl+X退出编辑器,然后重新执行sudo apt update看看能不能正常更新。
- 打开DNS配置文件:
第三步:清理系统DNS缓存
Ubuntu现在大多用systemd-resolved管理DNS,缓存异常也可能导致解析失败,试试清理缓存:sudo systemd-resolve --flush-caches然后重启DNS服务:
sudo systemctl restart systemd-resolved完成后再跑一遍
sudo apt update测试。第四步:检查源列表配置是否正确
有时候源地址拼写错误也会引发解析问题,你可以检查一下你的源配置文件:cat /etc/apt/sources.list cat /etc/apt/sources.list.d/*.list确认里面的源地址没有拼写错误,比如focal版本的源应该是
http://archive.ubuntu.com/ubuntu/这类。如果发现有问题,或者想换成国内更快的镜像源,替换后再执行sudo apt update。第五步:重启网络服务
如果前面的步骤都没解决,试试重启网络服务:
对于有线网络:sudo systemctl restart networking对于无线网络:
sudo systemctl restart NetworkManager重启后再测试更新命令。
备注:内容来源于stack exchange,提问作者Chirag Pawar

