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 复制代码
在这里插入代码片
相关推荐
撰卢14 分钟前
【个人笔记】负载均衡
运维·笔记·负载均衡
lilian1291 小时前
linux系统mysql性能优化
linux·运维·mysql
共享家95271 小时前
linux_线程概念
linux·开发语言·jvm
乌云暮年2 小时前
Linux常用命令
linux·服务器·ssh·php
weixin_516023072 小时前
Geant4 安装---Ubuntu
linux·运维·ubuntu
稀液蟹-plus2 小时前
zynq-PS篇——bperez77中DMA驱动注意事项
linux·fpga
wanhengidc2 小时前
企业选择大带宽服务器租用的原因有哪些?
运维·服务器
罗技1232 小时前
用Filebeat OSS 7.10.2将收集日志到Easysearch
运维·es
brave_zhao3 小时前
JavaBeanUtils javaBean转map, 实体类转map,实体集合转List<Map>
linux·windows·python
egoist20233 小时前
【Linux仓库】虚拟地址空间【进程·陆】
linux·运维·服务器·操作系统·进程·虚拟地址空间·fork