CentOS 7.x 的 YUM 仓库问题

背景

CentOS Linux 7 的生命周期(EOL)已经于 2024 年 6 月 30 日终止这意味着 CentOS 7.x 的官方镜像站点将不再提供服务,导致在使用 yum 安装或更新程序时可能会遇到 错误。本文将介绍如何解决这一问题,使得你可以继续在 CentOS 7.x 系统上使用 yum 进行软件包管理。

问题描述

在尝试使用 yum 进行更新或安装软件包时,可能会遇到如下错误信息:

复制代码
Could not retrieve mirrorlist http://mirrorlist.centos.org?arch=x86_64&release=7&repo=sclo-rh error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"
...
Cannot find a valid baseurl for repo: centos-sclo-rh/x86_64

解决步骤

使用centos-vault作为停止支持的CentOS的yum源, 国内还有很多centos镜像网站,例如,阿里云,清华源、中科大、上海交大、电子科大源等等。

系统版本可以通过执行cat /etc/redhat-release命令查看,我的系统是CentOS 7.9.2009

决定选用清华源镜像, 官方说明文档: https://mirrors.tuna.tsinghua.edu.cn/help/centos-vault

为了修复上述问题,我们需要将 CentOS 7.x 的仓库配置指向新的镜像源 vault.centos.org

1. 清空现有的 CentOS-Base.repo 文件

首先,我们需要清空当前的 CentOS-Base.repo 文件,以便进行新的配置:

bash 复制代码
echo > /etc/yum.repos.d/CentOS-Base.repo
2. 编辑 CentOS-Base.repo 文件

接下来,打开 CentOS-Base.repo 文件并将新的仓库配置粘贴到文件中

bash 复制代码
sudo vim /etc/yum.repos.d/CentOS-Base.repo

在文件中添加以下内容:

ini 复制代码
# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the
# remarked out baseurl= line instead.
#
#

[base]
name=CentOS-7 - Base - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/os/$basearch/
        http://mirrors.aliyuncs.com/centos/7/os/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/7/os/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#released updates
[updates]
name=CentOS-7 - Updates - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/updates/$basearch/
        http://mirrors.aliyuncs.com/centos/7/updates/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/7/updates/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that may be useful
[extras]
name=CentOS-7 - Extras - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/extras/$basearch/
        http://mirrors.aliyuncs.com/centos/7/extras/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/7/extras/$basearch/
gpgcheck=1
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-7 - Plus - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/centosplus/$basearch/
        http://mirrors.aliyuncs.com/centos/7/centosplus/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/7/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

#contrib - packages by Centos Users
[contrib]
name=CentOS-7 - Contrib - mirrors.aliyun.com
failovermethod=priority
baseurl=http://mirrors.aliyun.com/centos/7/contrib/$basearch/
        http://mirrors.aliyuncs.com/centos/7/contrib/$basearch/
        http://mirrors.cloud.aliyuncs.com/centos/7/contrib/$basearch/
gpgcheck=1
enabled=0
gpgkey=http://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
3. 删除其他无关的 .repo 文件

为了避免冲突,只保留 CentOS-Base.repo 文件,其它无关的 .repo 文件可以删除:

bash 复制代码
cd /etc/yum.repos.d/
ls | grep -v 'CentOS-Base.repo' | xargs rm
4. 重建 yum 缓存

最后,清理 yum 缓存并重新生成缓存,以确保新的配置生效:

bash 复制代码
sudo yum clean all
sudo yum makecache

结果

通过以上步骤,你应该能够成功地将 CentOS 7.x 的 YUM 仓库指向新的镜像源 vault.centos.org,并且可以继续使用 yum 进行软件包的安装和更新。

注意事项

  • 安全性:CentOS 7.x 已经不再受官方支持,建议尽快迁移到新的操作系统,这样才能继续获得安全更新、补丁和新功能。
  • 备份:在修改仓库配置文件之前,最好备份原有的配置文件,以便在需要时可以恢复。

希望这些信息对你有所帮助。如果你有任何问题或需要进一步的帮助,如果有任何调整或补充,欢迎告知!


bash 复制代码
在这里插入代码片
相关推荐
少妇的美梦10 小时前
logstash教程
运维
chen94511 小时前
k8s集群部署vector日志采集器
运维
chen94511 小时前
aws ec2部署harbor,使用s3存储
运维
轻松Ai享生活15 小时前
5 节课深入学习Linux Cgroups
linux
christine-rr15 小时前
linux常用命令(4)——压缩命令
linux·服务器·redis
三坛海会大神55516 小时前
LVS与Keepalived详解(二)LVS负载均衡实现实操
linux·负载均衡·lvs
東雪蓮☆16 小时前
深入理解 LVS-DR 模式与 Keepalived 高可用集群
linux·运维·服务器·lvs
qq_2642208916 小时前
LVS负载均衡群集和LVS+Keepalived群集
运维·负载均衡·lvs
乌萨奇也要立志学C++16 小时前
【Linux】进程概念(二):进程查看与 fork 初探
linux·运维·服务器
雨落Liy16 小时前
Nginx 从入门到进阶:反向代理、负载均衡与高性能实战指南
运维·nginx·负载均衡