解决CentOS7 yum失效的问题

142 阅读2分钟

背景

centos7已经停止支持,yum源也失效了 ,但是我们服务用的都是CentOS7 ,也不会轻易升级。so 我们只能找找替代方案,很幸运发现阿里云的yum源还可以使用,记录下来方便下次可能用到,顺便有缘人搜到这篇文章的话,也能给你一个参考。

1. 查看当前有哪些是在使用的

# 查看所有仓库配置文件
ls -l  /etc/yum.repos.d/

总用量 56
-rw-r--r--. 1 root root 2523 8月   2 2024 CentOS-Base.repo
-rw-r--r--. 1 root root 1309 10月 23 2020 CentOS-CR.repo
-rw-r--r--. 1 root root  649 10月 23 2020 CentOS-Debuginfo.repo
-rw-r--r--. 1 root root  314 10月 23 2020 CentOS-fasttrack.repo
-rw-r--r--. 1 root root  630 10月 23 2020 CentOS-Media.repo
-rw-r--r--. 1 root root 1331 10月 23 2020 CentOS-Sources.repo
-rw-r--r--. 1 root root 8515 10月 23 2020 CentOS-Vault.repo
-rw-r--r--. 1 root root  616 10月 23 2020 CentOS-x86_64-kernel.repo
-rw-r--r--. 1 root root 2081 8月   2 2024 docker-ce.repo
-rw-r--r--. 1 root root  951 10月  3 2017 epel.repo
-rw-r--r--. 1 root root 1050 10月  3 2017 epel-testing.repo
-rw-r--r--. 1 root root    0 7月  23 2024 jenkins.repo
-rw-r--r--. 1 root root  477 4月  27 2014 nux-dextop.repo

# 查看当前启用的仓库
yum repolist all

2. 备份配置

# 创建备份目录
mkdir -p /root/yum-repos-backup

# 备份所有仓库配置
cp /etc/yum.repos.d/*.repo /root/yum-repos-backup/

3. 安装阿里云yum源

# 删除之前的所有配置
rm -rf /etc/yum.repos.d/*

# 配置阿里云
# 检查阿里云网络是否正常
curl -I http://mirrors.aliyun.com/centos-vault/7.9.2009/os/x86_64/

# 创建新的完整仓库配置
cat > /etc/yum.repos.d/CentOS-Base.repo << 'EOF'
[base]
name=CentOS-7 - Base
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/os/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-7 - Updates
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/updates/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-7 - Extras
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/extras/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-7 - Plus
baseurl=http://mirrors.aliyun.com/centos-vault/7.9.2009/centosplus/x86_64/
enabled=0
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos-vault/RPM-GPG-KEY-CentOS-7
EOF

创建EPEL仓库配置

# 创建EPEL仓库配置
cat > /etc/yum.repos.d/epel.repo << 'EOF'
[epel]
name=Extra Packages for Enterprise Linux 7 - x86_64
baseurl=http://mirrors.aliyun.com/epel/7/x86_64/
enabled=1
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/epel/RPM-GPG-KEY-EPEL-7
EOF

4. 清理缓存并测试

# 清理所有缓存
yum clean all

# 重新生成缓存
yum makecache

# 测试仓库连接
yum repolist enabled