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 复制代码
在这里插入代码片
相关推荐
YRr YRr2 小时前
CMake 中 add_definitions() 使用的注意事项及替代方案
linux·c++·windows·cmake
遥逖3 小时前
Linux上的C/C++编程
linux·c语言·c++
鼠鼠龙年发大财3 小时前
【tbNick专享】虚拟机域控、成员服务器、降级等管理
运维·服务器
夏微凉.4 小时前
【RabbitMQ】RabbitMQ 的概念以及使用RabbitMQ编写生产者消费者代码
linux·学习·rabbitmq
Lilixxs4 小时前
MobaXterm基本使用 -- 服务器状态、批量操作、显示/切换中文字体、修复zsh按键失灵
运维·服务器·前端
醉颜凉4 小时前
银河麒麟桌面操作系统如何添加WPS字体
运维·服务器·kylin·wps·麒麟·wps添加字体
-SGlow-4 小时前
Linux相关概念和重要知识点(7)(git、冯诺依曼体系结构)
linux·运维·git
EterNity_TiMe_4 小时前
【Linux基础IO】深入解析Linux基础IO缓冲区机制:提升文件操作效率的关键
linux·运维·服务器·开发语言·学习·性能优化·学习方法
0xwang5 小时前
Ubuntu24.04桌面版下的网络管理
linux·ubuntu
苏湘涵5 小时前
进程的那些事--实现shell
linux·运维·服务器