Ubuntu Server 22.04执行sudo apt update时域名解析失败问题求助
最近我在电脑上安装了Ubuntu Server 22.04,但每次执行sudo apt update时,总会弹出以下报错信息:
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-updates/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://archive.ubuntu.com/ubuntu/dists/jammy-backports/InRelease Temporary failure resolving 'archive.ubuntu.com'
W: Failed to fetch http://security.ubuntu.com/ubuntu/dists/jammy-security/InRelease Temporary failure resolving 'security.ubuntu.com'
W: Some index files failed to download. They have been ignored, or old ones used instead.
我已经尝试更换了多个国内镜像源,包括官方主源,但问题依然存在。有没有大佬能给点解决建议?
[编辑补充]
我的/etc/apt/sources.list内容暂未贴出,若需要我可以补充相关信息。
解决方案建议
别发愁,这种"Temporary failure resolving"的问题基本都是DNS解析或者基础网络配置出了问题,我给你几个一步步排查的方向:
先确认基础网络是否连通
先试试直接ping公共IP,比如执行ping 8.8.8.8,如果能收到回复,说明你的服务器能连上外网,问题肯定出在DNS解析环节;如果ping不通,那得先检查网线连接、IP地址分配(比如用ip addr看看有没有正确获取到IP)这些基础配置。手动配置稳定的DNS服务器
Ubuntu Server默认用DHCP分配的DNS有时候不太靠谱,咱们手动换成公共DNS试试:- 打开systemd的DNS配置文件:
sudo nano /etc/systemd/resolved.conf - 找到
DNS和FallbackDNS这两行,去掉前面的#注释,改成:
这里用的是谷歌和Cloudflare的公共DNS,都很稳定。DNS=8.8.8.8 1.1.1.1 FallbackDNS=8.8.4.4 1.0.0.1 - 保存退出后,重启DNS服务:
sudo systemctl restart systemd-resolved - 现在再跑一遍
sudo apt update,看看能不能正常解析域名了。
- 打开systemd的DNS配置文件:
检查sources.list的源地址是否正确
虽然你换过镜像,但还是要确认源地址有没有拼写错误。比如国内阿里云的Jammy(22.04)源格式应该是这样的,你可以对比一下自己的配置:deb http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-security main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-updates main restricted universe multiverse deb http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse deb-src http://mirrors.aliyun.com/ubuntu/ jammy-backports main restricted universe multiverse如果你的文件里有错误的域名或者路径,也会导致连接失败,建议先备份原文件(
sudo cp /etc/apt/sources.list /etc/apt/sources.list.bak),再替换成正确的配置。清除APT缓存后重试
有时候旧的缓存文件会搞事情,执行下面两条命令清掉缓存再更新:sudo apt clean sudo apt update
备注:内容来源于stack exchange,提问作者Barney.

