马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。
您需要 登录 才可以下载或查看,没有账号?立即注册
×
一、报错长什么样
执行 apt-get update 和下载软件包时,控制台刷出一片类似下面的内容:
Could not resolve 'mirrors.cloud.aliyuncs.com'
E: Failed to fetch http://mirrors.cloud.aliyuncs.com/ubuntu-ports/pool/main/n/nginx/nginx-common_1.18.0-6ubuntu14.4_all.deb
Could not resolve 'mirrors.cloud.aliyuncs.com'
E: Failed to fetch http://mirrors.cloud.aliyuncs.com/ubuntu-ports/pool/main/n/nginx/libnginx-mod-http-geoip2_1.18.0-6ubuntu14.4_arm64.deb
...
E: Unable to fetch some archives, maybe run apt-get update or try with --fix-missing?
关键信号是那句 Could not resolve——它不是「下载慢」,也不是「源挂了」,而是域名根本没解析出 IP。 一个包都没下下来,末尾那句「试试 --fix-missing」属于官方给的通用提示,照着做没用,方向错了。
注意报错里的 arm64:这是 ARM 架构的 Ubuntu(比如树莓派、鲲鹏、部分云原生镜像)。阿里云有单独的 ubuntu-ports 源,和 amd64 的源不能混用,这点后面会提到。
二、前面那些「网上方案」为什么没用
搜索这道题,主流回答基本指向两个方向,但实操下来都解决不了:
1. DNS 解析问题(方向对,但给的地址是错的)
有回答让人往 /etc/resolv.conf 里加公共 DNS(8.8.8.8、114.114.114.114 之类)。加完 apt-get update 还是同样的 Could not resolve,因为问题不是「没有 DNS」,而是「DNS 地址填错了」。
2. 镜像源问题(方向也偏了)
有人建议把源换成别的国内镜像(清华、中科大等)。换完一般还是不行——因为你换的只是「从哪个域名拉包」,而 Could not resolve 卡在「域名怎么变成 IP」这一步,源换得再对,域名解析不出来照样一片红。
所以这道题真正的入口不在 sources.list,在 resolv.conf。
三、根因:DNS 地址填反了
这台机器上的情况是:阿里云在 Ubuntu 上推荐的 DNS 是 127.0.0.53,而机器上配置的是 127.0.0.253。 这个 127.0.0.53 不是普通的 DNS 服务地址,而是 systemd-resolved 提供的本地 DNS 桩(stub):真正的上游解析由它转发出去。
- 填 127.0.0.53:交给 systemd-resolved,由它按
/etc/resolv.conf 上游(通常已经在 /etc/systemd/resolved.conf 里配好)继续解析,阿里云域名能正常出结果;
- 填 127.0.0.253:这个端口上什么都没监听,解析请求直接石沉大海,于是每个 fetch 都
Could not resolve。
一句话:Ubuntu 20.04 起装了 systemd-resolved,就把 DNS 指到 127.0.0.53,别指到随便一个 127.0.0.x。
四、修改步骤
1. 打开并修改配置文件
sudo vi /etc/resolv.conf
2. 写入正确的 DNS
按 i 进入编辑模式,加上阿里云 DNS(本机解析走 systemd-resolved 桩):
nameserver 127.0.0.53
改完 esc 退出编辑模式,输入 :wq 保存并退出。然后重新跑一次 apt-get update,红字基本会一次性消失。
3. 顺带确认 sources.list 里的源
解析通了之后,如果还想让源本身也对,确认一下 sources.list 指向的是阿里云的 ubuntu-ports(ARM 机器)或普通 ubuntu(amd64 机器):
sudo vi /etc/apt/sources.list
# ARM 架构
deb http://mirrors.cloud.aliyuncs.com/ubuntu-ports focal main restricted
# amd64 架构
deb http://mirrors.cloud.aliyuncs.com/ubuntu focal main restricted
五、注意:resolv.conf 可能被覆盖
这里有个很容易再次踩到的点:/etc/resolv.conf 是个符号链接,指向 systemd-resolved 运行生成的配置文件,重启或装了某些网络工具后内容会被重写。 所以永久生效的做法是改 systemd 这一层:
sudo vi /etc/systemd/resolved.conf
# 加上
[Resolve]
DNS=127.0.0.53
sudo systemctl restart systemd-resolved
另外 NetworkManager 也会接管 DNS,如果这台机器有 NetworkManager,改完 resolv.conf 又被打回去,就去改对应连接的 IPv4 DNS 设置,别在文件层面反复打地鼠。
六、排障小抄
- 先验证解析本身:
nslookup mirrors.cloud.aliyuncs.com 或 ping -c 1 mirrors.cloud.aliyuncs.com;能出 IP 再看源,出不来先修 DNS;
- 看本地 DNS 到底是谁在干活:
ls -l /etc/resolv.conf,如果链接到 /run/systemd/resolve/stub-resolv.conf,说明走 127.0.0.53 是对的;
- 判断架构:
dpkg --print-architecture,arm64 必须用 ubuntu-ports 源;
- 别急着装公共 DNS:8.8.8.8 在部分网络里出口不通,而 127.0.0.53 是本机桩,稳定性往往更好。
总结一句:E: Failed to fetch 开头跟着 Could not resolve,先查 DNS 再查源;Ubuntu 配 127.0.0.53,断点就在这一行 nameserver 上。
(本文由 qidao123.com企服评测-应用市场 编辑提示整理:示例基于 Ubuntu 20.04/22.04 与 systemd-resolved 环境,适用于 ARM / amd64 架构的包管理排障。修改 DNS 前建议先备份 /etc/resolv.conf。) |