CentOS 7 停止维护后更换 yum 源

很多朋友最近在使用 CentOS 7 时遇到了无法通过 yum 安装软件包的问题。

这是因为 CentOS 7 已在 2024 年 6 月 30 日正式停止维护 (EOL), 官方不再提供更新和支持, 原有的官方 yum 源也随之下线, 因此会出现如下报错:

Loaded plugins: fastestmirror
Determining fastest mirrors
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

Could not retrieve mirrorlist http://mirrors.elrepo.org/mirrors-elrepo.el7 error was
12: Timeout on http://mirrors.elrepo.org/mirrors-elrepo.el7: (28, 'Connection timed out after 30000 milliseconds')

Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=extras&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=updates&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"

解决方法就是更换第三方提供的 yum 镜像源, 例如阿里云开源镜像站。

更换 yum 源

备份当前 yum 源

在修改之前, 建议先备份原有的 yum 源配置文件, 以防万一, 以便后续恢复

mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup

这一步会将原来的配置文件重命名为 CentOS-Base.repo.backup

下载新的 yum 源配置文件

这里以阿里云镜像源为例

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

清理并生成缓存

# 清理旧的缓存文件
yum clean all

# 生成新的缓存
yum makecache

验证是否生效

现在可以尝试安装一个软件包, 例如

yum install vim

如果能够正常安装, 说明新的 yum 源已经生效

脚本

使用脚本快速替换

bash <(curl -sL shell.puka.cc/replace-repo)

原文

centos7停止维护后更换yum源教程
mirrorlist.centos.org no longer resolve?
阿里巴巴开源镜像站-OPSX镜像站
Epel 镜像 - 阿里巴巴开源镜像站

最后更新于 2025-08-26