CentOS 停服后 Yum 无法使用的解决方案
自 2021 年底 CentOS 官方停止更新后,很多用户在使用 Yum 安装软件时会遇到如下错误:
错误:Failed to download metadata for repo 'AppStream': Cannot prepare internal mirrorlist: No URLs in mirrorlist
原因是官方源已过期或不可用。下面介绍几种实用的解决方法。
1️⃣ 修改 Yum 源指向 Vault 仓库
进入 Yum 源配置目录:
cd /etc/yum.repos.d/
注释掉
mirrorlist并修改baseurl:
sed -i 's/mirrorlist/#mirrorlist/g' /etc/yum.repos.d/CentOS-*
sed -i 's|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g' /etc/yum.repos.d/CentOS-*
更新缓存并测试安装:
yum clean all
yum makecache
yum install wget -y
2️⃣ 使用阿里云 CentOS Vault 镜像
下载阿里源:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
更新缓存:
yum clean all
yum makecache
测试安装:
yum install wget -y
3️⃣ 一键修改命令(替换为清华或中科大镜像)
清华源示例:
sed -e "s|^mirrorlist=|#mirrorlist=|g" \
-e "s|^#baseurl=http://mirror.centos.org/\$contentdir/|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/|g" \
-i.bak /etc/yum.repos.d/CentOS-*.repo
中科大源示例:
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=http://mirrors.ustc.edu.cn/centos-vault/$(awk '{print $4}' /etc/redhat-release)|g" \
-i.bak /etc/yum.repos.d/CentOS-*.repo
4️⃣ 使用脚本一键切换源
Coolbian 源:
curl https://static.coolbian.com/iprr/sh/centos7-yum.sh | bash
阿里云 CentOS 8:
sudo cp -a /etc/yum.repos.d /etc/yum.repos.d.bak
sudo rm -f /etc/yum.repos.d/*.repo
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-8.repo
sudo yum clean all
sudo yum makecache
阿里云 CentOS 7:
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
其他一键脚本:
curl -O https://file.tsyvps.com/yumcentos7.sh && chmod +x yumcentos7.sh && ./yumcentos7.sh
5️⃣ 总结
CentOS 官方源已经停止服务,Yum 安装失败是正常现象。
建议将源替换为 Vault 仓库 或国内镜像(阿里、清华、中科大等),可以快速恢复 Yum 功能。
对于经常需要部署的软件,可以直接使用一键脚本,节省调试时间。
评论区