使用如下脚本可以解决Centos系统源失效的问题,对应版本号替换成对应版本的源文件即可
# 关闭 fastestmirror 插件
sed -i 's/enabled=1/enabled=0/' /etc/yum/pluginconf.d/fastestmirror.conf
# 备份原有源文件
mv /etc/yum.repos.d/CentOS-Base.repo{,.backup} 2>/dev/null
# 下载阿里云 CentOS 6 源配置文件
if ! curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo; then
echo "❌ 下载阿里云源失败,请检查网络连接"
exit 1
fi
# 替换 EPEL 源(如果安装了)
if rpm -q epel-release &> /dev/null; then
echo " 替换 EPEL 源为阿里云版本..."
curl -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
fi
# 清理并重建缓存
echo " 清理 yum 缓存..."
yum clean all > /dev/null
echo " 重建 yum 缓存..."
yum makecache > /dev/null
echo "✅ CentOS 7成功更换为阿里云镜像源!"