背景
CentOS 7 已于 2024 年 6 月 30 日 EOL,官方 mirrorlist.centos.org 已下线,默认源全部失效,必须切换到 阿里云 Vault 归档源 或 阿里云镜像站。
方案一:阿里云官方 Repo 文件(推荐先尝试)
# 1. 备份原有源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak 2>/dev/null
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak 2>/dev/null
# 2. 下载阿里云官方 repo(无 wget 用 curl)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
# 3. 清理并重建缓存
yum clean all
yum makecache
阿里云官方 repo 文件可能已经自动指向归档地址,如果能正常
makecache,直接用这套即可。
方案二:手写 Vault 归档源(方案一失败时用)
如果方案一报 No route to host 或 Timeout,说明 repo 文件里的内网地址无法访问,直接手写归档地址:
# 1. 备份并清空旧源
mkdir -p /etc/yum.repos.d/bak
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/bak/ 2>/dev/null
# 2. 手写 CentOS 7 Vault 源
cat > /etc/yum.repos.d/CentOS-Base.repo << 'EOF'
[base]
name=CentOS-7.9.2009 - Base
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/os/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
[updates]
name=CentOS-7.9.2009 - Updates
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/updates/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
[extras]
name=CentOS-7.9.2009 - Extras
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/extras/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
EOF
# 3. 手写 EPEL 归档源
cat > /etc/yum.repos.d/epel.repo << 'EOF'
[epel]
name=Extra Packages for Enterprise Linux 7
baseurl=https://mirrors.aliyun.com/epel-archive/7/x86_64/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel-archive/RPM-GPG-KEY-EPEL-7
EOF
# 4. 重建缓存
yum clean all
yum makecache
验证
# 查看仓库列表
yum repolist
# 测试安装
yum install -y wget vim
# 查看当前生效的源地址
yum repolist -v | grep baseurl
常见问题
| 问题 | 原因 | 解决 |
|---|---|---|
Could not resolve host: mirrorlist.centos.org |
CentOS 7 官方源已下线 | 按上述方案切换阿里云源 |
Failed connect to mirrors.aliyuncs.com:80; No route to host |
阿里云 repo 文件包含内网地址 | 使用方案二手写 Vault 地址 |
Timeout on mirrors.cloud.aliyuncs.com |
公网无法访问阿里云内网镜像 | 同上,使用方案二 |
yum makecache 卡在 Determining fastest mirrors |
fastestmirror 插件检测慢 | sed -i 's/enabled=1/enabled=0/g' /etc/yum/pluginconf.d/fastestmirror.conf |
极简速查(复制即用)
# 方案一(30秒)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo
yum clean all && yum makecache
# 如果失败,直接执行方案二(手写 Vault)