centos7诊断并修复yum脚本

检测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
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

科学熊

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值