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

教程大全 2026-01-08 12:30:03 浏览
目录

CentOs7中使用yum命令时候报错:

问题原因

出现这个错误是因为使用的 CentOS 7 仓库已经被归档,当前的镜像地址无法找到所需的文件。CentOS 7 的官方支持已经结束,部分仓库已被移至归档库。这导致了你的 yum 命令无法找到所需的元数据文件。CentOS 7 的官方仓库在 2024 年 6 月 30 日之后已经停止维护。因此,使用最新的 CentOS 7 官方仓库可能会遇到问题。

解决方法

进入/etc/yum.repos.d目录下找到 CentOS-Base.repo

进入目录:

cd /etc/yum.repos.d

先备份下,然后编辑文件

cpCentOS-Base.repoCentOS-Base.repo.backupvi CentOS-Base.repo

修改内容为

# 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-$releasever - Base#mirrorlist=$releasever&arch=$basearch&repo=os&infra=$infra#baseurl=$releasever/os/$basearch/#baseurl=$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#released updates[updates]name=CentOS-$releasever - Updates#mirrorlist=$releasever&arch=$basearch&repo=updates&infra=$infra#baseurl=$releasever/updates/$basearch/#baseurl=$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that may be useful[extras]name=CentOS-$releasever - Extras#mirrorlist=$releasever&arch=$basearch&repo=extras&infra=$infra#$baseurl=$releasever/extras/$basearch/#baseurl=$basearch/gpgcheck=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7#additional packages that extend functionality of existing packages[centoSPLus]name=CentOS-$releasever - Plus#mirrorlist=$releasever&arch=$basearch&repo=centosplus&infra=$infra#baseurl=$releasever/centosplus/$basearch/#baseurl=$basearch/gpgcheck=1enabled=0gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

保存后执行

sudo yum clean allsudo yum makecache

等待完成

[root@localhost yum.repos.d]# sudo yum makecache已加载插件:fastestmirrorDetermining fastest mirrorsbase| 3.6 kB00:00:00extras| 2.9 kB00:00:00updates| 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元数据缓存已建立

然后执行以下命令

curl -o /etc/yum.repos.d/CentOS-Base.repo

查看文件

cat /etc/yum.repos.d
7

镜像更换成阿里云即可,建议再执行下

sudo yum clean allsudo yum makecache

Centos7配置本地Yum源以及网络YUM源保姆级教程

在虚拟机没有网络或者网络不稳定的情况下需要配置本地yum源

优点:由于数据从本地获取,下载和安装软件包的速度非常快,完全不需要网络

缺点:软件可能不是最新版,需要手动更新比较麻烦

一、配置本地yum源

首先将centos自带的网络yum源删掉(自带的默认从centos官网下载软件,服务器在国外,非常不稳定)

 [root@cloud ~]# rm -rf /etc/yum.repos.d/*

创建挂载点并挂载镜像

[root@cloud ~]# mkdir /opt/centos[root@cloud ~]# mount /dev/cdrom /opt/centos/mount: /dev/sr0 写保护,将以只读方式挂载

创建并编写本地yum源配置文件,编写完成之后wq保存退出

[root@cloud ~]# vi /etc/yum.repos.d/local.repo[centos]name=centos# Yum源的名称,用于描述这个源baseurl=file:///opt/centos# 本地Yum源的位置,指向挂载点目录gpgcheck=0# 不进行GPG签名检查enabled=1# 启用该Yum源

清除缓存原来的生成新的缓存并列出当前系统中已配置和启用的Yum仓库(repositories)及其状态

[root@cloud ~]# yum clean all已加载插件:fastestmirror, langpacks正在清理软件源: centosCleaning up list of fastest mirrorsOther repos take up 1.0 G of disk space (use --verbose for details)[root@cloud ~]# yum makecache已加载插件:fastestmirror, langpacksDetermining fastest mirrorscentos| 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元数据缓存已建立[root@cloud ~]# yum repolist已加载插件:fastestmirror, langpacksLoading mirror speeds from cached hostfile源标识源名称状态centoscentos4,070repolist: 4,070

下载httpd测试一下

[root@cloud ~]# yum install -y httpd已加载插件:fastestmirror, langpacksLoading mirror speeds from cached hostfile正在解决依赖关系--> 正在检查事务---> 软件包 httpd.x86_64.0.2.4.6-95.el7.centos 将被 安装--> 正在处理依赖关系 httpd-tools = 2.4.6-95.el7.centos,它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要--> 正在处理依赖关系 /etc/mime.types,它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要--> 正在处理依赖关系 libaprutil-1.so.0()(64bit),它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要--> 正在处理依赖关系 libapr-1.so.0()(64bit),它被软件包 httpd-2.4.6-95.el7.centos.x86_64 需要--> 正在检查事务---> 软件包 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 将被 安装--> 解决依赖关系完成依赖关系解决======================================================================================================================== Package架构版本源大小========================================================================================================================正在安装: httpdx86_642.4.6-95.el7.centoscentos2.7 M为依赖而安装: aprx86_641.4.8-7.el7centos104 k apr-utilx86_641.5.2-6.el7centos92 k httpd-toolsx86_642.4.6-95.el7.centoscentos93 k mailcapnoarch2.1.41-2.el7centos31 k事务概要========================================================================================================================安装1 软件包 (+4 依赖软件包)总下载量:3.0 M安装大小:10 MDownloading packages:------------------------------------------------------------------------------------------------------------------------总计95 MB/s | 3.0 MB00:00:00Running transaction checkRunning transaction testTransaction test succeededRunning transaction正在安装: apr-1.4.8-7.el7.x86_641/5正在安装: apr-util-1.5.2-6.el7.x86_642/5正在安装: httpd-tools-2.4.6-95.el7.centos.x86_643/5正在安装: mailcap-2.1.41-2.el7.noarch4/5正在安装: httpd-2.4.6-95.el7.centos.x86_645/5验证中: httpd-tools-2.4.6-95.el7.centos.x86_641/5验证中: mailcap-2.1.41-2.el7.noarch2/5验证中: apr-1.4.8-7.el7.x86_643/5验证中: httpd-2.4.6-95.el7.centos.x86_644/5验证中: apr-util-1.5.2-6.el7.x86_645/5已安装:httpd.x86_64 0:2.4.6-95.el7.centos作为依赖被安装:apr.x86_64 0:1.4.8-7.el7apr-util.x86_64 0:1.5.2-6.el7httpd-tools.x86_64 0:2.4.6-95.el7.centosmailcap.noarch 0:2.1.41-2.el7完毕!

下载成功

二、配置网络yum源

优点:软件包和元数据由源服务器自动更新,国内镜像下载速度非常快

缺点:需要稳定的网络连接,网络不稳定或断开时无法使用

首先找到国内的镜像站(我这里以阿里云举例)

找到自己的Linux版本(以centos为例)

找到对应的系统版本(我的是7)并且复制这段命令到虚拟机粘贴

同样需要先删除原有的yum仓库

列出当前系统中已配置和启用的Yum仓库(repositories)及其状态

[root@cloud ~]# rm -rf /etc/yum.repos.d/*[root@cloud ~]# wget -O /etc/yum.repos.d/CentOS-Base.repo12:36:26--正在解析主机 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) [Application/octet-stream]正在保存至: “/etc/yum.repos.d/CentOS-Base.repo”100%[======================================>] 2,523--.-K/s 用时 0.006s2024-06-09 12:36:26 (401 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523])[root@cloud ~]# yum repolist已加载插件:fastestmirror, langpacksLoading mirror speeds from cached hostfile * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com源标识源名称状态!base/7/x86_64CentOS-7 - Base - mirrors.aliyun.com10,072!extras/7/x86_64CentOS-7 - Extras - mirrors.aliyun.com526!updates/7/x86_64CentOS-7 - Updates - mirrors.aliyun.com5,890repolist: 16,488

可以看到已经保存成功,网络yum源就配置好了

以上就是CentOS 7 yum无法使用解决方法的详细内容,更多相关资料请阅读主机测评网其它文章!

本文版权声明本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,请联系本站客服,一经查实,本站将立刻删除。

发表评论

热门推荐