检测yum源可用性,并修复yum源,配置ali源
#!/bin/bash
# yum 仓库错误修复脚本
set -e
# 颜色定义
RED='\033[0;31m'
GREEN='\033[0;32m'
YELLOW='\033[1;33m'
NC='\033[0m'
echo -e "${GREEN}开始修复 yum 仓库错误...${NC}"
# 1. 首先清理缓存
echo -e "${YELLOW}步骤1: 清理 yum 缓存...${NC}"
yum clean all
rm -rf /var/cache/yum
# 2. 检查网络连接
echo -e "${YELLOW}步骤2: 检查网络连接...${NC}"
if ! ping -c 2 mirrors.aliyun.com &> /dev/null; then
echo -e "${RED}错误: 无法连接到 mirrors.aliyun.com${NC}"
echo "请检查网络连接和DNS配置"
exit 1
fi
# 3. 检查当前仓库配置
echo -e "${YELLOW}步骤3: 检查仓库配置...${NC}"
ls -la /etc/yum.repos.d/
# 4. 临时禁用所有仓库,然后逐个启用测试
echo -e "${YELLOW}步骤4: 检查问题仓库...${NC}"
# 禁用所有仓库
for repo in $(yum repolist all | awk '/repo id/ {getline; print $1}'); do
yum-config-manager --disable "$repo" >/dev/null 2>&1
done
# 5. 重新配置阿里云源
echo -e "${YELLOW}步骤5: 重新配置阿里云源...${NC}"
# 备份现有配置
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/ 2>/dev/null || true
# 创建新的阿里云源配置
cat > /etc/yum.repos.d/CentOS-Base.repo << 'EOF'
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
enabled=1
[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
enabled=1
[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
enabled=1
[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-$releasever
enabled=0
EOF
# 6. 配置EPEL源
echo -e "${YELLOW}步骤6: 配置EPEL源...${NC}"
cat > /etc/yum.repos.d/epel.repo << 'EOF'
[epel]
name=Extra Packages for Enterprise Linux $releasever - $basearch
baseurl=https://mirrors.aliyun.com/epel/$releasever/$basearch
enabled=1
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-$releasever
EOF
# 7. 设置跳过不可用仓库
echo -e "${YELLOW}步骤7: 配置仓库容错...${NC}"
# 设置跳过不可用仓库
yum-config-manager --save --setopt=*.skip_if_unavailable=true
# 8. 测试仓库可用性
echo -e "${YELLOW}步骤8: 测试仓库可用性...${NC}"
if yum repolist enabled; then
echo -e "${GREEN}仓库配置成功!${NC}"
else
echo -e "${RED}仓库配置仍有问题,尝试更简单的配置...${NC}"
# 使用更简单的配置
cat > /etc/yum.repos.d/CentOS-Minimal.repo << 'EOF'
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=0
enabled=1
EOF
yum clean all
yum makecache
fi
# 9. 最终测试
echo -e "${YELLOW}步骤9: 最终测试...${NC}"
if yum list available | head -10; then
echo -e "${GREEN}修复成功!yum 现在可以正常工作了。${NC}"
echo -e "${YELLOW}建议运行: yum update${NC}"
else
echo -e "${RED}修复失败,请检查以下可能的原因:${NC}"
echo "1. 网络连接问题"
echo "2. 防火墙阻止"
echo "3. DNS解析问题"
echo "4. 系统版本不匹配"
# 检查具体错误
echo -e "${YELLOW}尝试调试信息:${NC}"
curl -I https://mirrors.aliyun.com/centos/ || echo "curl失败"
nslookup mirrors.aliyun.com || echo "nslookup失败"
fi
870

被折叠的 条评论
为什么被折叠?



