纳达尔 發表於 2025-7-6 00:00:00

CentOS 7 yum无法使用的解决方法

<div id="navCategory"><h5 class="catalogue">目录</h5><ul class="first_class_ul"><li>问题原因</li><li>解决方法</li><li>Centos7配置本地Yum源以及网络YUM源保姆级教程<ul class="second_class_ul"><li>一、配置本地yum源</li><li>二、配置网络yum源</li></ul></li></ul></div><p>在centos7中使用yum命令时候报错:</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="780" src="https://zhuji.jb51.net/uploads/allimg/20250706/2-250F6161930648.png" width="937" /></p>
<p class="maodian"></p><h2>问题原因</h2>
<p>出现这个错误是因为使用的 CentOS 7 仓库已经被归档,当前的镜像地址无法找到所需的文件。CentOS 7 的官方支持已经结束,部分仓库已被移至归档库。这导致了你的 yum 命令无法找到所需的元数据文件。CentOS 7 的官方仓库在 2024 年 6 月 30 日之后已经停止维护。因此,使用最新的 CentOS 7 官方仓库可能会遇到问题。</p>
<p class="maodian"></p><h2>解决方法</h2>
<p>进入/etc/yum.repos.d目录下找到 CentOS-Base.repo</p>
<p>进入目录:</p>
<div class="dxycode"><pre class="brush:bash;">cd /etc/yum.repos.d</pre></div>
<p>先备份下,然后编辑文件</p>
<div class="dxycode"><pre class="brush:bash;">cpCentOS-Base.repo   CentOS-Base.repo.backup
vi CentOS-Base.repo</pre></div>
<p>修改内容为</p>
<div class="dxycode"><pre class="brush:bash;"># 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.
#
#

name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=os&amp;infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#released updates

name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=updates&amp;infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that may be useful

name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=extras&amp;infra=$infra
#$baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
#additional packages that extend functionality of existing packages

name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&amp;arch=$basearch&amp;repo=centosplus&amp;infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7</pre></div>
<p>保存后执行</p>
<div class="dxycode"><pre class="brush:bash;">sudo yum clean all
sudo yum makecache</pre></div>
<p>等待完成</p>
<div class="dxycode"><pre class="brush:bash;"># sudo yum makecache
已加载插件:fastestmirror
Determining fastest mirrors
base                                                                            | 3.6 kB00:00:00   
extras                                                                        | 2.9 kB00:00:00   
updates                                                                         | 2.9 kB00:00:00   
(1/10): base/x86_64/group_gz                                                    | 153 kB00:00:00   
(2/10): base/x86_64/primary_db                                                | 6.1 MB00:00:01   
(3/10): base/x86_64/other_db                                                    | 2.6 MB00:00:00   
(4/10): extras/x86_64/primary_db                                                | 253 kB00:00:01   
(5/10): extras/x86_64/other_db                                                | 154 kB00:00:00   
(6/10): updates/x86_64/filelists_db                                             |15 MB00:00:06   
(7/10): updates/x86_64/other_db                                                 | 1.6 MB00:00:00   
(8/10): updates/x86_64/primary_db                                             |27 MB00:00:07   
(9/10): base/x86_64/filelists_db                                                | 7.2 MB00:00:17   
(10/10): extras/x86_64/filelists_db                                             | 305 kB00:00:16   
元数据缓存已建立</pre></div>
<p>然后执行以下命令</p>
<div class="dxycode"><pre class="brush:bash;">curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo</pre></div>
<p>查看文件</p>
<div class="dxycode"><pre class="brush:bash;">cat /etc/yum.repos.d</pre></div>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" height="961" src="https://zhuji.jb51.net/uploads/allimg/20250706/2-250F6161931C7.png" width="1114" /></p>
<p>镜像更换成阿里云即可,建议再执行下</p>
<div class="dxycode"><pre class="brush:bash;">sudo yum clean all
sudo yum makecache</pre></div>
<p class="maodian"></p><h2>Centos7配置本地Yum源以及网络YUM源保姆级教程</h2>
<p>在虚拟机没有网络或者网络不稳定的情况下需要配置本地yum源</p>
<p>优点:由于数据从本地获取,下载和安装软件包的速度非常快,完全不需要网络</p>
<p>缺点:软件可能不是最新版,需要手动更新比较麻烦</p>
<p class="maodian"></p><h3>一、配置本地yum源</h3>
<p>首先将centos自带的网络yum源删掉(自带的默认从centos官网下载软件,服务器在国外,非常不稳定)</p>
<div class="dxycode"><pre class="brush:bash;"> # rm -rf /etc/yum.repos.d/*</pre></div>
<p>创建挂载点并挂载镜像</p>
<div class="dxycode"><pre class="brush:bash;"># mkdir /opt/centos
# mount /dev/cdrom /opt/centos/
mount: /dev/sr0 写保护,将以只读方式挂载</pre></div>
<p>创建并编写本地yum源配置文件,编写完成之后wq保存退出</p>
<div class="dxycode"><pre class="brush:bash;"># vi /etc/yum.repos.d/local.repo

name=centos               # Yum源的名称,用于描述这个源
baseurl=file:///opt/centos# 本地Yum源的位置,指向挂载点目录
gpgcheck=0                  # 不进行GPG签名检查
enabled=1                   # 启用该Yum源</pre></div>
<p>清除缓存原来的生成新的缓存并列出当前系统中已配置和启用的Yum仓库(repositories)及其状态</p>
<div class="dxycode"><pre class="brush:bash;"># yum clean all
已加载插件:fastestmirror, langpacks
正在清理软件源: centos
Cleaning up list of fastest mirrors
Other repos take up 1.0 G of disk space (use --verbose for details)
# yum makecache
已加载插件:fastestmirror, langpacks
Determining fastest mirrors
centos                                                                                           | 3.6 kB00:00:00   
(1/4): centos/group_gz                                                                           | 153 kB00:00:00   
(2/4): centos/primary_db                                                                         | 3.3 MB00:00:00   
(3/4): centos/other_db                                                                           | 1.3 MB00:00:00   
(4/4): centos/filelists_db                                                                     | 3.3 MB00:00:00   
元数据缓存已建立
# yum repolist
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
源标识                                                   源名称                                                    状态
centos                                                   centos                                                    4,070
repolist: 4,070</pre></div>
<p>下载httpd测试一下</p>
<div class="dxycode"><pre class="brush:bash;"># yum install -y httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
正在解决依赖关系
--&gt; 正在检查事务
---&gt; 软件包 httpd.x86_64.0.2.4.6-95.el7.centos 将被 安装
--&gt; 正在处理依赖关系 httpd-tools = 2.4.6-95.el7.centos,它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要
--&gt; 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要
--&gt; 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要
--&gt; 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要
--&gt; 正在检查事务
---&gt; 软件包 apr.x86_64.0.1.4.8-7.el7 将被 安装
---&gt; 软件包 apr-util.x86_64.0.1.5.2-6.el7 将被 安装
---&gt; 软件包 httpd-tools.x86_64.0.2.4.6-95.el7.centos 将被 安装
---&gt; 软件包 mailcap.noarch.0.2.1.41-2.el7 将被 安装
--&gt; 解决依赖关系完成
依赖关系解决
========================================================================================================================
Package                     架构                   版本                                 源                      大小
========================================================================================================================
正在安装:
httpd                     x86_64               2.4.6-95.el7.centos                  centos               2.7 M
为依赖而安装:
apr                         x86_64               1.4.8-7.el7                            centos               104 k
apr-util                  x86_64               1.5.2-6.el7                            centos                  92 k
httpd-tools               x86_64               2.4.6-95.el7.centos                  centos                  93 k
mailcap                     noarch               2.1.41-2.el7                           centos                  31 k
事务概要
========================================================================================================================
安装1 软件包 (+4 依赖软件包)
总下载量:3.0 M
安装大小:10 M
Downloading packages:
------------------------------------------------------------------------------------------------------------------------
总计                                                                                     95 MB/s | 3.0 MB00:00:00   
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
正在安装    : apr-1.4.8-7.el7.x86_64                                                                              1/5
正在安装    : apr-util-1.5.2-6.el7.x86_64                                                                         2/5
正在安装    : httpd-tools-2.4.6-95.el7.centos.x86_64                                                            3/5
正在安装    : mailcap-2.1.41-2.el7.noarch                                                                         4/5
正在安装    : httpd-2.4.6-95.el7.centos.x86_64                                                                  5/5
验证中      : httpd-tools-2.4.6-95.el7.centos.x86_64                                                            1/5
验证中      : mailcap-2.1.41-2.el7.noarch                                                                         2/5
验证中      : apr-1.4.8-7.el7.x86_64                                                                              3/5
验证中      : httpd-2.4.6-95.el7.centos.x86_64                                                                  4/5
验证中      : apr-util-1.5.2-6.el7.x86_64                                                                         5/5
已安装:
httpd.x86_64 0:2.4.6-95.el7.centos                                                                                    
作为依赖被安装:
apr.x86_64 0:1.4.8-7.el7            apr-util.x86_64 0:1.5.2-6.el7       httpd-tools.x86_64 0:2.4.6-95.el7.centos      
mailcap.noarch 0:2.1.41-2.el7      
完毕!</pre></div>
<p>下载成功</p>
<p class="maodian"></p><h3>二、配置网络yum源</h3>
<p>优点:软件包和元数据由源服务器自动更新,国内镜像下载速度非常快</p>
<p>缺点:需要稳定的网络连接,网络不稳定或断开时无法使用</p>
<p>首先找到国内的镜像站(我这里以阿里云举例)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250706/2-250F6161931O6.png" /></p>
<p>找到自己的Linux版本(以centos为例)</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250706/2-250F6161931Z0.png" /></p>
<p>找到对应的系统版本(我的是7)并且复制这段命令到虚拟机粘贴</p>
<p style="text-align:center"><img style="max-width:100%!important;height:auto!important;"alt="" src="https://zhuji.jb51.net/uploads/allimg/20250706/2-250F6161931Y0.png" /></p>
<p>同样需要先删除原有的yum仓库</p>
<p>列出当前系统中已配置和启用的Yum仓库(repositories)及其状态</p>
<div class="dxycode"><pre class="brush:bash;"># rm -rf /etc/yum.repos.d/*
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
--2024-06-09 12:36:26--https://mirrors.aliyun.com/repo/Centos-7.repo
正在解析主机 mirrors.aliyun.com (mirrors.aliyun.com)... 183.215.53.217, 183.215.53.222, 183.215.53.224, ...
正在连接 mirrors.aliyun.com (mirrors.aliyun.com)|183.215.53.217|:443... 已连接。
已发出 HTTP 请求,正在等待回应... 200 OK
长度:2523 (2.5K)
正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”
100%[======================================&gt;] 2,523       --.-K/s 用时 0.006s
2024-06-09 12:36:26 (401 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” )
# yum repolist
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyun.com
* extras: mirrors.aliyun.com
* updates: mirrors.aliyun.com
源标识                              源名称                                                      状态
!base/7/x86_64                        CentOS-7 - Base - mirrors.aliyun.com                        10,072
!extras/7/x86_64                      CentOS-7 - Extras - mirrors.aliyun.com                         526
!updates/7/x86_64                     CentOS-7 - Updates - mirrors.aliyun.com                      5,890
repolist: 16,488</pre></div>
<p>可以看到已经保存成功,网络yum源就配置好了</p>
<p>以上就是CentOS 7 yum无法使用解决方法的详细内容,更多相关资料请阅读琼殿技术社区其它文章!</p>
頁: [1]
查看完整版本: CentOS 7 yum无法使用的解决方法