CentOS7 yum安装报错“Could not resolve host: mirrorlist.centos.org; N

[复制链接]
发表于 2026-10-3 20:37:26 | 显示全部楼层 |阅读模式

马上注册,结交更多好友,享用更多功能,让你轻松玩转社区。

您需要 登录 才可以下载或查看,没有账号?立即注册

×

一、现象:yum 装包报 Could not resolve host

执行一条 yum 安装命令时报:

Loaded plugins: fastestmirror, langpacks
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os
error was: 14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Name or service not known"

One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
safe thing yum can do is fail. There are a few ways to work "fix" this:
   1. Contact the upstream for the repository and get them to fix the problem.
   2. Reconfigure the baseurl/... for the repository, to point to a working
      upstream. This is most often useful if you are using a newer
      distribution release than is supported by the repository (and the
      packages for the previous distribution releases still work).
   3. Disable the repository, so yum won't use it by default. Yum will then
      just ignore the repository until you permanently enable it again or use
      --enablerepo for temporary usage:
      yum-config-manager --disable &
   4. Configure the failing repository to be skipped, if it is unavailable.
      ...
      yum-config-manager --save --setopt=....skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/7/x86_64

记住那个 curl#6 - Could not resolve host:这是域名解析失败,不是包不存在、也不是权限问题。

二、判断:是 DNS 的问题还是源的问题

排查只有两步,但顺序不能反:

  • 先 ping 报错里那个域名:ping mirrorlist.centos.org —— 不通;
  • 再 ping 一个 known-good 域名(如 ping www.baidu.com)——正常。

这个组合说明:网络通、公网能解析,唯独这个域名解析不了 → 根本确认是 yum 源(镜像列表地址)的问题,而不是 DNS 配置崩了。

为什么 CentOS 7 特别容易撞上?因为 CentOS 7 在 2024 年 6 月已结束生命周期(EOL),官方把 mirrorlist.centos.org 的 release=7 条目移除了,继续用默认的 mirrorlist 地址,解析拿不到有效镜像列表就报这个错。 这不是你配错了,是上游关停了。

三、解法:换掉 base repo 指向可用镜像

思路是把 /etc/yum.repos.d/CentOS-Base.repo 指向仍然维护的镜像站。

1. 先装 wget(没 wget 的话)

yum install -y wget

2. 备份原配置(强烈建议)

cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak

3. 下载对应发行版的 repo 文件

# wget 方式
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

# 或者 curl 方式
curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

CentOS 7 用 Centos-7.repo,换成 CentOS 6/8 就把文件名里的版本对上。

4. 重建缓存并更新

yum clean all
yum makecache
yum -y update

跑完这三行就通了。

四、不想换源时的两种临时办法

yum 自己其实在第 4 点给了解法,照做即可:

1. 直接跳过这个源

# 针对具体 repo id
yum-config-manager --save --setopt=base.skip_if_unavailable=true

2. 或者把 mirrorlist 换成 baseurl

把 repo 文件里的 mirrorlist=... 注释掉,改用 baseurl=... 指向一个可用镜像目录,这样连 DNS 解析步骤都省了。

五、顺带修的那个 ImportError(另一种报错,别混)

有些机器在 yum 安装过程中会先报一类完全不相关的错:

ImportError: /lib64/libcrypto.so.10: version `OPENSSL_1.0.2' not found
  (required by /www/server/panel/pyenv/lib/python3.7/lib-dynload/_ssl.cpython-37m-x86_64-linux-gnu.so)

这是 Python 3.7 的 _ssl 扩展模块在加载 openssl 动态库时版本不匹配,和 yum 源的解析错误是两件事,别被它带偏——报错里出现 OPENSSL_1.0.2 not found 的字样,就是动态库链接问题,要修的是 Python/OpenSSL 的库版本,不是 yum 源。

六、三条实操建议

  1. 先备份再换源:repo 文件一改错,连排障的退路都没了;
  2. EOL 系统的源是一次性治理:换完最好顺手处理 updates、extras、epel 几个 repo(同样指向可用镜像),只换 base 往往会下个包又报同一个错;
  3. 长期方案是升级系统:CentOS 7 已停维护,安全补丁要靠 upstream 或迁移到 Rocky / AlmaLinux / Anolis 这类延续发行版,光换源解决不了所有问题。

七、一句话总结

判断方法:ping mirrorlist.centos.org 不通 + ping www.baidu.com 通 → 是源的问题不是 DNS;解法:备份 CentOS-Base.repo,下载 mirrors.aliyun.com/repo/Centos-7.repo 覆盖,再 yum clean all && yum makecache。

根因记一句:CentOS 7 已 EOL,官方 mirrorlist 不再返回条目,所以「换源」不是可选优化,是这个系统继续用的前提。

(本文由 qidao123.com企服评测-应用市场 编辑提示整理:内容围绕 CentOS 7 yum 源失效与 repo 更换,适用于老版本系统的包管理修复与迁移规划。涉及系统级配置变更请先备份。)

回复

使用道具 举报

登录后关闭弹窗

登录参与点评抽奖  加入IT实名职场社区
去登录
快速回复 返回顶部 返回列表