CentOS 执行yum命令报错【错误:为仓库 ‘appstream‘ 下载元数据失败: …】

一、遇到问题

刚安装的CentOS 8服务器,在使用一些常见命令时,发现找不到这些命令,然后使用yum在线安装这些命令时报错:【错误:为仓库 ‘appstream’ 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist】

[root@Chen ~]# ifconfig
-bash: ifconfig: 未找到命令
[root@Chen ~]# wget
-bash: wget: 未找到命令
[root@Chen ~]# vim test.txt
-bash: vim: 未找到命令
[root@Chen ~]# yum -y install net-tools
CentOS Linux 8 - AppStream                                                                     15  B/s |  38  B     00:02
错误:为仓库 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist
[root@Chen ~]# yum list
CentOS Linux 8 - AppStream                                                                     79  B/s |  38  B     00:00
错误:为仓库 'appstream' 下载元数据失败 : Cannot prepare internal mirrorlist: No URLs in mirrorlist

二、原因分析

在网上查阅资料得知,CentOS Linux 8 版本的操作系统在 2021-12-31 就结束了生命周期(EOL),Linux 社区不再维护该版本的操作系统,CentOS Linux 8 的镜像及yum源一起迁移到了 https://vault.centos.org
End of Life

三、解决问题

原因已经明确了,CentOS 8 的yum源失效了,那就更新下yum源呗,下边提供几个解决方案供参考。
CentOS Linux 8yum 源仓库配置文件:

[root@Chen ~]# cd /etc/yum.repos.d/
[root@Chen yum.repos.d]# ls
CentOS-Linux-AppStream.repo	     CentOS-Linux-Devel.repo		 CentOS-Linux-Media.repo
CentOS-Linux-BaseOS.repo	     CentOS-Linux-Extras.repo		 CentOS-Linux-Plus.repo
CentOS-Linux-ContinuousRelease.repo  CentOS-Linux-FastTrack.repo	 CentOS-Linux-PowerTools.repo
CentOS-Linux-Debuginfo.repo	     CentOS-Linux-HighAvailability.repo  CentOS-Linux-Sources.repo

方案一:更新centos提供的yum源新地址

1. yum源仓库配置文件内容

先来看下源文件内容(删掉了一些注释),以CentOS-Linux-BaseOS.repo为例。

[root@Chen yum.repos.d]# cat CentOS-Linux-BaseOS.repo
[baseos]
name=CentOS Linux $releasever - BaseOS
mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
#baseurl=http://mirror.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

2. 更新yum源新地址

CentOS-Linux-*.repo 所有文件内容做如下修改:
1)注释掉 "mirrorlist=..." 配置内容
2)取消 "baseurl=..." 的注释,同时将 mirror.centos.org 替换为 vault.centos.org
执行如下命令,完成上述修改

[root@Chen yum.repos.d]# sed -i -e "s/mirrorlist=/#mirrorlist=/g" /etc/yum.repos.d/CentOS-Linux-*.repo
[root@Chen yum.repos.d]# sed -i -e "s|#baseurl=http://mirror.centos.org|baseurl=http://vault.centos.org|g" /etc/yum.repos.d/CentOS-Linux-*.repo
[root@Chen yum.repos.d]# cat CentOS-Linux-BaseOS.repo
[baseos]
name=CentOS Linux $releasever - BaseOS
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=BaseOS&infra=$infra
baseurl=http://vault.centos.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial

3. 测试yum安装命令

[root@Chen yum.repos.d]# yum -y install net-tools
CentOS Linux 8 - AppStream                                                                    3.1 MB/s | 8.4 MB     00:02
CentOS Linux 8 - BaseOS                                                                       2.6 MB/s | 4.6 MB     00:01
CentOS Linux 8 - Extras                                                                        11 kB/s |  10 kB     00:00
依赖关系解决。
==============================================================================================================================
 软件包                     架构                    版本                                        仓库                     大小
==============================================================================================================================
安装:
 net-tools                 x86_64                 2.0-0.52.20160912git.el8                    baseos                  322 k

事务概要
==============================================================================================================================
安装  1 软件包

总下载:322 k
安装大小:942 k
下载软件包:
net-tools-2.0-0.52.20160912git.el8.x86_64.rpm                                                 613 kB/s | 322 kB     00:00
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                          610 kB/s | 322 kB     00:00
CentOS Linux 8 - BaseOS                                                                       148 kB/s | 1.6 kB     00:00
导入 GPG 公钥 0x8483C65D:
 Userid: "CentOS (CentOS Official Signing Key) <[email protected]>"
 指纹: 99DB 70FA E1D7 CE22 7FB6 4882 05B5 55B3 8483 C65D
 来自: /etc/pki/rpm-gpg/RPM-GPG-KEY-centosofficial
导入公钥成功
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                               1/1
  安装    : net-tools-2.0-0.52.20160912git.el8.x86_64                                                                     1/1
  运行脚本: net-tools-2.0-0.52.20160912git.el8.x86_64                                                                     1/1
  验证    : net-tools-2.0-0.52.20160912git.el8.x86_64                                                                     1/1

已安装:
  net-tools-2.0-0.52.20160912git.el8.x86_64

完毕!

验证yum命令安装软件已正常可用!

撒花 ✿✿ヽ(°▽°)ノ✿

方案二:更新为 国内的yum源

在演示方案二之前,先将已有的yum源仓库配置文件备份

[root@Chen yum.repos.d]# mkdir backup && mv CentOS-Linux-*.repo backup
[root@Chen yum.repos.d]# yum install wget
错误:在"/etc/yum.repos.d", "/etc/yum/repos.d", "/etc/distro.repos.d"中没有被启用的仓库。

1. 使用 阿里的yum源

1)使用 wget 或 curl 命令 下载阿里的yum源仓库配置文件

wget -O /etc/yum.repos.d/CentOS-Linux-Base-ali.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
curl -o /etc/yum.repos.d/CentOS-Linux-Base-ali.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo

(我的Linux系统,wget命令不可用,我用curl)

[root@Chen yum.repos.d]# curl -o /etc/yum.repos.d/CentOS-Linux-Base-ali.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
  % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                                 Dload  Upload   Total   Spent    Left  Speed
100  2495  100  2495    0     0   4346      0 --:--:-- --:--:-- --:--:--  4339
[root@Chen yum.repos.d]# ls
backup  CentOS-Linux-Base-ali.repo

2)验证yum安装命令

[root@Chen yum.repos.d]# yum -y install wget
CentOS-8.5.2111 - Base - mirrors.aliyun.com                                                    39 kB/s | 3.9 kB     00:00
CentOS-8.5.2111 - Extras - mirrors.aliyun.com                                                  20 kB/s | 1.5 kB     00:00
CentOS-8.5.2111 - AppStream - mirrors.aliyun.com                                               31 kB/s | 4.3 kB     00:00
依赖关系解决。
==============================================================================================================================
 软件包                         架构                      版本                             仓库                          大小
==============================================================================================================================
安装:
 wget                           x86_64                    1.19.5-10.el8                    AppStream                    734 k
安装依赖关系:
 libmetalink                    x86_64                    0.1.3-7.el8                      base                          32 k

事务概要
==============================================================================================================================
安装  2 软件包

总下载:766 k
安装大小:2.8 M
下载软件包:
(1/2): libmetalink-0.1.3-7.el8.x86_64.rpm                                                     181 kB/s |  32 kB     00:00
(2/2): wget-1.19.5-10.el8.x86_64.rpm                                                          2.5 MB/s | 734 kB     00:00
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                          2.6 MB/s | 766 kB     00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                               1/1
  安装    : libmetalink-0.1.3-7.el8.x86_64                                                                                1/2
  安装    : wget-1.19.5-10.el8.x86_64                                                                                     2/2
  运行脚本: wget-1.19.5-10.el8.x86_64                                                                                     2/2
  验证    : libmetalink-0.1.3-7.el8.x86_64                                                                                1/2
  验证    : wget-1.19.5-10.el8.x86_64                                                                                     2/2

已安装:
  libmetalink-0.1.3-7.el8.x86_64                                   wget-1.19.5-10.el8.x86_64

完毕!

2. 使用 网易的yum源

1)新建一个 CentOS-Linux-Base-tsinghua.repo 文件,使用vi命令编辑并保存如下内容:

[baseos]
name=CentOS-8.5.2111 - BaseOS - mirrors.163.com
baseurl=http://mirrors.163.com/centos-vault/8.5.2111/BaseOS/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-8.5.2111 - Extras - mirrors.163.com
baseurl=http://mirrors.163.com/centos-vault/8.5.2111/extras/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-8.5.2111 - Plus - mirrors.163.com
baseurl=http://mirrors.163.com/centos-vault/8.5.2111/centosplus/$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-8.5.2111 - PowerTools - mirrors.163.com
baseurl=http://mirrors.163.com/centos-vault/8.5.2111/PowerTools/$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

[AppStream]
name=CentOS-8.5.2111 - AppStream - mirrors.163.com
baseurl=http://mirrors.163.com/centos-vault/8.5.2111/AppStream/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-Official

2)测试网易163的yum源
注意:在测试163的yum源之前,先把之前的阿里yum源仓库配置文件移动到backup目录

[root@Chen yum.repos.d]# mv CentOS-Linux-Base-ali.repo backup
[root@Chen yum.repos.d]# yum -y install tree
CentOS-8.5.2111 - BaseOS - mirrors.163.com                                                    252 kB/s | 4.6 MB     00:18
CentOS-8.5.2111 - Extras - mirrors.163.com                                                     27 kB/s |  10 kB     00:00
CentOS-8.5.2111 - AppStream - mirrors.163.com                                                 131 kB/s | 8.4 MB     01:06
依赖关系解决。
==============================================================================================================================
 软件包                    架构                        版本                                 仓库                         大小
==============================================================================================================================
安装:
 tree                      x86_64                      1.7.0-15.el8                         baseos                       59 k

事务概要
==============================================================================================================================
安装  1 软件包

总下载:59 k
安装大小:109 k
下载软件包:
tree-1.7.0-15.el8.x86_64.rpm                                                                   59 kB/s |  59 kB     00:01
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                           59 kB/s |  59 kB     00:01
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                               1/1
  安装    : tree-1.7.0-15.el8.x86_64                                                                                      1/1
  运行脚本: tree-1.7.0-15.el8.x86_64                                                                                      1/1
  验证    : tree-1.7.0-15.el8.x86_64                                                                                      1/1

已安装:
  tree-1.7.0-15.el8.x86_64

完毕!

3. 使用 清华的yum源

1)新建一个 CentOS-Linux-Base-tsinghua.repo 文件,使用vi命令编辑并保存如下内容:

[baseos]
name=CentOS-8.5.2111 - BaseOS - mirrors.tuna.tsinghua.edu.cn
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/BaseOS/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Official

#additional packages that may be useful
[extras]
name=CentOS-8.5.2111 - Extras - mirrors.tuna.tsinghua.edu.cn
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/extras/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Official

[AppStream]
name=CentOS-8.5.2111 - AppStream - mirrors.tuna.tsinghua.edu.cn
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/AppStream/$basearch/os/
gpgcheck=0
gpgkey=http://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Official

[PowerTools]
name=CentOS-8.5.2111 - PowerTools - mirrors.tuna.tsinghua.edu.cn
baseurl=http://mirrors.tuna.tsinghua.edu.cn/centos-vault/8.5.2111/PowerTools/$basearch/os/
gpgcheck=0
enabled=0
gpgkey=http://mirrors.tuna.tsinghua.edu.cn/centos/RPM-GPG-KEY-CentOS-Official

2)测试清华的yum源
注意:在测试清华的yum源之前,先把之前的163的yum源仓库配置文件移动到backup目录

[root@Chen yum.repos.d]# mv CentOS-Linux-Base-163.repo backup
[root@Chen yum.repos.d]# vim
-bash: vim: 未找到命令
[root@Chen yum.repos.d]# yum -y install vim
CentOS-8.5.2111 - BaseOS - mirrors.tuna.tsinghua.edu.cn                                       5.3 MB/s | 4.6 MB     00:00
CentOS-8.5.2111 - Extras - mirrors.tuna.tsinghua.edu.cn                                        21 kB/s |  10 kB     00:00
CentOS-8.5.2111 - AppStream - mirrors.tuna.tsinghua.edu.cn                                    6.3 MB/s | 8.4 MB     00:01
依赖关系解决。
==============================================================================================================================
 软件包                          架构                    版本                                仓库                        大小
==============================================================================================================================
安装:
 vim-enhanced                    x86_64                  2:8.0.1763-16.el8                   AppStream                  1.4 M
安装依赖关系:
 gpm-libs                        x86_64                  1.20.7-17.el8                       AppStream                   39 k
 vim-common                      x86_64                  2:8.0.1763-16.el8                   AppStream                  6.3 M
 vim-filesystem                  noarch                  2:8.0.1763-16.el8                   AppStream                   49 k

事务概要
==============================================================================================================================
安装  4 软件包

总下载:7.8 M
安装大小:30 M
下载软件包:
(1/4): gpm-libs-1.20.7-17.el8.x86_64.rpm                                                      303 kB/s |  39 kB     00:00
(2/4): vim-filesystem-8.0.1763-16.el8.noarch.rpm                                              951 kB/s |  49 kB     00:00
(3/4): vim-enhanced-8.0.1763-16.el8.x86_64.rpm                                                3.0 MB/s | 1.4 MB     00:00
(4/4): vim-common-8.0.1763-16.el8.x86_64.rpm                                                  7.2 MB/s | 6.3 MB     00:00
------------------------------------------------------------------------------------------------------------------------------
总计                                                                                          8.8 MB/s | 7.8 MB     00:00
运行事务检查
事务检查成功。
运行事务测试
事务测试成功。
运行事务
  准备中  :                                                                                                               1/1
  安装    : vim-filesystem-2:8.0.1763-16.el8.noarch                                                                       1/4
  安装    : vim-common-2:8.0.1763-16.el8.x86_64                                                                           2/4
  安装    : gpm-libs-1.20.7-17.el8.x86_64                                                                                 3/4
  运行脚本: gpm-libs-1.20.7-17.el8.x86_64                                                                                 3/4
  安装    : vim-enhanced-2:8.0.1763-16.el8.x86_64                                                                         4/4
  运行脚本: vim-enhanced-2:8.0.1763-16.el8.x86_64                                                                         4/4
  运行脚本: vim-common-2:8.0.1763-16.el8.x86_64                                                                           4/4
  验证    : gpm-libs-1.20.7-17.el8.x86_64                                                                                 1/4
  验证    : vim-common-2:8.0.1763-16.el8.x86_64                                                                           2/4
  验证    : vim-enhanced-2:8.0.1763-16.el8.x86_64                                                                         3/4
  验证    : vim-filesystem-2:8.0.1763-16.el8.noarch                                                                       4/4

已安装:
  gpm-libs-1.20.7-17.el8.x86_64              vim-common-2:8.0.1763-16.el8.x86_64    vim-enhanced-2:8.0.1763-16.el8.x86_64
  vim-filesystem-2:8.0.1763-16.el8.noarch

完毕!

四、总结

  1. 方案一是基于CentOS系统旧的yum源仓库配置文件直接做了修改,不同仓库是独立的配置文件,如 BaseOS、AppStream 仓库配置文件分别是 CentOS-Linux-BaseOS.repo、CentOS-Linux-AppStream.repo,然后修改配置时可以根据需要选择对应的仓库配置文件进行修改,当然文中是将所有配置文件都一并修改(推荐都修改);
  2. 方案二是下载或新建了一个 XXX.repo 文件,然后将一些重要仓库配置 合并到了一个配置文件里,然后将yum源地址更新为国内的yum源地址,当然国内有很多镜像网站都有提供,文中列举了几个镜像网站案例供参考;
  3. 也可以参考方案一的方式,将yum源改为国内的,方法都类似,以mirrors.aliyun.com 为例:
    1)编辑 /etc/yum.repos.d/ 中的相应文件,在 mirrorlist= 开头行前面加 # 注释掉;并将 #baseurl= 开头行取消#注释
    2)对于 CentOS 8 之前版本,把域名及路径 mirror.centos.org/centos/$releasever替换为 mirrors.aliyun.com/centos-vault/$minorver
    3)对于 CentOS 8 版本,把域名及路径 mirror.centos.org/$contentdir/$releasever替换为 mirrors.aliyun.com/centos-vault/$minorver
    $minorver 为 centos 完整版本号,如 CentOS 7 的最后一个版本为 7.9.2009,CentOS 8 的最后一个版本为 8.5.2111)
    具体命令如下:
# Centos 8 之前版本
# 以 CentOS 7.9.2009 为例
minorver=7.9.2009
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" 
         -e "s|^#baseurl=http://mirror.centos.org/centos/$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" 
         -i.bak 
         /etc/yum.repos.d/CentOS-*.repo

# CentOS 8 版本
# 以 CentOS 8.5.2111 为例
minorver=8.5.2111
sudo sed -e "s|^mirrorlist=|#mirrorlist=|g" 
         -e "s|^#baseurl=http://mirror.centos.org/$contentdir/$releasever|baseurl=https://mirrors.aliyun.com/centos-vault/$minorver|g" 
         -i.bak 
         /etc/yum.repos.d/CentOS-*.repo
  1. 配置文件中,如果需要启用一些 repo,需要将其中的 enabled=0 改为 enabled=1enabled 省略默认等效为 enabled=1
  2. 在切换yum源后,如果之前的yum源在本地有缓存,需要先清理下缓存 yum clean all,然后如果有需要再重新更新缓存 yum makecache
  3. 不推荐不同的yum源混用,除了如下提示,也可能会在软件升级等操作中出现一些未知错误!
[root@Chen yum.repos.d]# ls
backup  CentOS-Linux-Base-163.repo  CentOS-Linux-Base-tsinghua.repo
[root@Chen yum.repos.d]# yum repolist
Repository baseos is listed more than once in the configuration
Repository extras is listed more than once in the configuration
Repository AppStream is listed more than once in the configuration
Repository PowerTools is listed more than once in the configuration
仓库 id                                      仓库名称
AppStream                                    CentOS-8.5.2111 - AppStream - mirrors.163.com
baseos                                       CentOS-8.5.2111 - BaseOS - mirrors.163.com
extras                                       CentOS-8.5.2111 - Extras - mirrors.163.com


参考资料:
https://developer.aliyun.com/mirror/centos-vault
https://mirrors.163.com/.help/centos.html
https://mirrors.tuna.tsinghua.edu.cn/help/centos-vault/

阿里镜像
网易镜像
清华镜像

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
THE END
分享
二维码
< <上一篇
下一篇>>