修复 CentOS 7 仓库 404 错误(使用阿里云镜像)
CentOS 7 已于 2024 年 6 月 30 日停止维护(EOL),因此许多镜像站点不再托管这些仓库。推荐使用阿里云镜像来替代,速度更快且更稳定。
方法一:直接下载阿里云配置(推荐,最简单)
在您的 CentOS 7 系统上运行以下命令:
# 备份现有配置文件(如果存在)
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup 2>/dev/null || true
# 下载阿里云的 CentOS 7 仓库配置文件
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
# 清理并重建缓存
sudo yum clean all
sudo yum makecache方法二:使用脚本自动配置
使用提供的脚本(会自动下载阿里云配置):
chmod +x fix-centos7-repos.sh
sh ./fix-centos7-repos.sh
yum clean all
yum makecache
yum update -y如果下载失败,脚本会自动尝试修改现有配置文件。
方法三:手动修改现有配置
如果您想保留现有配置结构,可以手动修改:
# 备份仓库配置文件
sudo cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
# 更新仓库配置为阿里云镜像
sudo sed -i 's|^mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's|^#baseurl=http://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's|^baseurl=http://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's|^baseurl=http://vault.centos.org|baseurl=http://mirrors.aliyun.com/centos|g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's|https://mirrors.ustc.edu.cn/centos|http://mirrors.aliyun.com/centos|g' /etc/yum.repos.d/CentOS-Base.repo
sudo sed -i 's|http://mirrors.ustc.edu.cn/centos|http://mirrors.aliyun.com/centos|g' /etc/yum.repos.d/CentOS-Base.repo
# 清理并重建缓存
sudo yum clean all
sudo yum makecache方法四:手动编辑配置文件
编辑 /etc/yum.repos.d/CentOS-Base.repo 文件,将 baseurl 修改为阿里云镜像地址:
[base] 部分的示例:
[base]
name=CentOS-$releasever - Base
baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1同样需要修改 [updates]、[extras] 等部分:
[updates]
name=CentOS-$releasever - Updates
baseurl=http://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1
[extras]
name=CentOS-$releasever - Extras
baseurl=http://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
enabled=1验证配置
运行以下命令验证阿里云镜像是否配置成功:
yum repolist enabled在输出中应该能看到包含 “aliyun” 字样的仓库。
重要提示
阿里云镜像优势:
- 国内访问速度快
- 稳定可靠
- 提供 CentOS 7 的安全更新订阅服务(2024年6月30日至2025年6月30日)
安全更新订阅:
- 阿里云提供 CentOS 7 的扩展安全更新服务
- 建议订阅以保障系统安全
- 详情:https://help.aliyun.com/zh/ecs/user-guide/centos-7-security-update-subscription
长期建议:
- CentOS 7 已停止官方维护
- 强烈建议尽快迁移到受支持的操作系统(如 Rocky Linux、AlmaLinux、CentOS Stream 等)
- 以确保系统的长期安全性和稳定性
镜像地址格式:
- 阿里云镜像地址:
http://mirrors.aliyun.com/centos/$releasever/... - 注意使用 HTTP(不是 HTTPS)
- 阿里云镜像地址:
如果您愿意,可以使用提供的脚本:
vi fix-centos7-repos.sh#!/bin/bash
# Script to fix CentOS 7 repository configuration after EOL
# This updates the base repository to use Aliyun (阿里云) mirrors
REPO_FILE="/etc/yum.repos.d/CentOS-Base.repo"
# Method 1: Download Aliyun's repo file directly (recommended)
METHOD=${1:-"download"}
if [ "$METHOD" = "download" ]; then
echo "Downloading Aliyun CentOS 7 repository configuration..."
# Backup existing file if it exists
if [ -f "$REPO_FILE" ]; then
BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${REPO_FILE}.backup.${BACKUP_TIMESTAMP}"
cp "$REPO_FILE" "$BACKUP_FILE"
echo "Backup saved to: $BACKUP_FILE"
fi
# Download Aliyun's CentOS 7 repo file
curl -o "$REPO_FILE" http://mirrors.aliyun.com/repo/Centos-7.repo
if [ $? -eq 0 ]; then
echo "Repository configuration downloaded successfully from Aliyun!"
echo ""
echo "Now run: yum clean all && yum makecache"
exit 0
else
echo "Failed to download from Aliyun, trying to modify existing file..."
METHOD="modify"
fi
fi
# Method 2: Modify existing repo file
if [ "$METHOD" = "modify" ]; then
if [ ! -f "$REPO_FILE" ]; then
echo "Error: $REPO_FILE not found"
echo "Please run this script as root on a CentOS 7 system"
exit 1
fi
# Backup the original file
BACKUP_TIMESTAMP=$(date +%Y%m%d_%H%M%S)
BACKUP_FILE="${REPO_FILE}.backup.${BACKUP_TIMESTAMP}"
cp "$REPO_FILE" "$BACKUP_FILE"
# Update baseurl to use Aliyun mirrors
# Comment out mirrorlist
sed -i 's|^mirrorlist=|#mirrorlist=|g' "$REPO_FILE"
# Replace baseurl to use Aliyun mirrors
# Handle commented baseurl lines
sed -i 's|^#baseurl=http://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
sed -i 's|^#baseurl=https://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
# Handle active baseurl lines
sed -i 's|^baseurl=http://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
sed -i 's|^baseurl=https://mirror.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
sed -i 's|^baseurl=http://vault.centos.org/centos|baseurl=http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
sed -i 's|^baseurl=http://vault.centos.org/7/|baseurl=http://mirrors.aliyun.com/centos/7/|g' "$REPO_FILE"
# Handle USTC mirror references
sed -i 's|https://mirrors.ustc.edu.cn/centos|http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
sed -i 's|http://mirrors.ustc.edu.cn/centos|http://mirrors.aliyun.com/centos|g' "$REPO_FILE"
echo "Repository configuration updated successfully to use Aliyun mirrors!"
echo "Backup saved to: $BACKUP_FILE"
echo ""
echo "Now run: yum clean all && yum makecache"
fi
chmod +x fix-centos7-repos.sh && sh ./fix-centos7-repos.sh && yum makecache重要提示
路径格式:vault.centos.org 的正确路径格式必须包含
/centos/前缀,即http://vault.centos.org/centos/7/...,而不是http://vault.centos.org/7/...使用特定版本号:为了更稳定,建议使用特定版本号(如
7.9.2009)而不是变量$releasever,这样可以避免路径解析问题HTTP vs HTTPS:归档库使用 HTTP(而非 HTTPS),这对于归档仓库来说是正常的
安全更新:请注意,Vault 仓库仅提供存档版本的软件包,不再接收安全更新。强烈建议尽快迁移到受支持的操作系统(如 Rocky Linux、AlmaLinux 等)
作者:Jeebiz 创建时间:2025-12-09 13:18
最后编辑:Jeebiz 更新时间:2025-12-10 11:49
最后编辑:Jeebiz 更新时间:2025-12-10 11:49