CentOS 7 yum无法使用的解决方法
在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
先备份下,然后编辑文件
cp CentOS-Base.repo CentOS-Base.repo.backup vi 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=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&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 [updates] name=CentOS-$releasever - Updates #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&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 [extras] name=CentOS-$releasever - Extras #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&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 [centosplus] name=CentOS-$releasever - Plus #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&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
保存后执行
sudo yum clean all sudo yum makecache
等待完成
[root@localhost yum.repos.d]# sudo yum makecache 已加载插件:fastestmirror Determining fastest mirrors base | 3.6 kB 00:00:00 extras | 2.9 kB 00:00:00 updates | 2.9 kB 00:00:00 (1/10): base/x86_64/group_gz | 153 kB 00:00:00 (2/10): base/x86_64/primary_db | 6.1 MB 00:00:01 (3/10): base/x86_64/other_db | 2.6 MB 00:00:00 (4/10): extras/x86_64/primary_db | 253 kB 00:00:01 (5/10): extras/x86_64/other_db | 154 kB 00:00:00 (6/10): updates/x86_64/filelists_db | 15 MB 00:00:06 (7/10): updates/x86_64/other_db | 1.6 MB 00:00:00 (8/10): updates/x86_64/primary_db | 27 MB 00:00:07 (9/10): base/x86_64/filelists_db | 7.2 MB 00:00:17 (10/10): extras/x86_64/filelists_db | 305 kB 00:00:16 元数据缓存已建立
然后执行以下命令
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
查看文件
cat /etc/yum.repos.d
镜像更换成阿里云即可,建议再执行下
sudo yum clean all sudo 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 正在清理软件源: centos Cleaning up list of fastest mirrors Other repos take up 1.0 G of disk space (use --verbose for details) [root@cloud ~]# yum makecache 已加载插件:fastestmirror, langpacks Determining fastest mirrors centos | 3.6 kB 00:00:00 (1/4): centos/group_gz | 153 kB 00:00:00 (2/4): centos/primary_db | 3.3 MB 00:00:00 (3/4): centos/other_db | 1.3 MB 00:00:00 (4/4): centos/filelists_db | 3.3 MB 00:00:00 元数据缓存已建立 [root@cloud ~]# yum repolist 已加载插件:fastestmirror, langpacks Loading mirror speeds from cached hostfile 源标识 源名称 状态 centos centos 4,070 repolist: 4,070
下载httpd测试一下
[root@cloud ~]# yum install -y httpd 已加载插件:fastestmirror, langpacks Loading 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 架构 版本 源 大小 ======================================================================================================================== 正在安装: 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 MB 00: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 完毕!
下载成功
二、配置网络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.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) [application/octet-stream] 正在保存至: “/etc/yum.repos.d/CentOS-Base.repo” 100%[======================================>] 2,523 --.-K/s 用时 0.006s 2024-06-09 12:36:26 (401 KB/s) - 已保存 “/etc/yum.repos.d/CentOS-Base.repo” [2523/2523]) [root@cloud ~]# 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
可以看到已经保存成功,网络yum源就配置好了
以上就是CentOS 7 yum无法使用解决方法的详细内容,更多相关资料请阅读主机测评网其它文章!
本文由主机测评网发布,不代表主机测评网立场,转载联系作者并注明出处:https://zhuji.jb51.net/centos/8641.html