问题

公司使用刀片机的系统版本是CentOS 7.9.2009(Core),本人在重新安装虚拟机时,也使用对应的系统版本,在安装软件时,yum无法正常使用,一开始觉得,centos的release版本默认配置和默认镜像不可能有问题,所有怀疑是网络问题,然后就试着折腾,各种配置,各种ping,折腾了好长时间,最后发现是yum的问题,然后就卸载重装默认的yum,结果还是不行,最终通过学习yum的相关配置才解决问题。
通过设置远程镜像url的参数值$releasever为7就可以了。

排查分析

  1. ping命名,看是否是网络配置问题;
  2. 查看/etc/resolv.conf,看配置是否正确,可以增加一行配置nameserver 114.114.114.114,执行命令chattr +i /etc/resolv.conf,然后重启systemctl restart network试下;
  3. 根据提示不能解析主机:mirrorlist.centos.org,开始以为是因为该yum所配置的镜像无法使用,在尝试更换为http://mirrors.ustc.edu.cn/之后还是不能使用;
  4. 跟着错误提示信息往下找,Cannot find a valid baseurl for repo: base/$releasever/x86_64,表示无效的url,然后就发现了$releasever参数,经过百度,发现该参数是指CentOS对应的版本号,(配置文件yum.conf中的distroverpkg=centos-release参数,通过这个配置可以获取到版本号),网上说是,centos-release没有安装,导致获取不到, 我通过命令rpm -qa centos-release查看该rpm包已经安装,但执行命令echo $releasever未输出任何内容,所有具体原因也不清楚,在网上也没有找到解决的方案,最后只能通过笨方法,直接修改repo仓库中的镜像url的参数值$releasever为7才解决问题。

如果增加远程仓库,需要确定是否包含参数$releasever,比如安装docker,也需要将/etc/yum.repos.d/docker-ce.repo中的$releasever替换掉

[root@localhost yum.repos.d]# yum install docker-ce docker-ce-cli containerd.io --installroot /usr/local/db-soft/docker
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=$releasever&arch=x86_64&repo=os&infra=$infra error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; Unknown error"


 One of the configured repositories failed (Unknown),
 and yum doesn't have enough cached data to continue. At this point the only
 safe thing yum can do is fail. There are a few ways to work "fix" this:

     1. Contact the upstream for the repository and get them to fix the problem.

     2. Reconfigure the baseurl/etc. for the repository, to point to a working
        upstream. This is most often useful if you are using a newer
        distribution release than is supported by the repository (and the
        packages for the previous distribution release still work).

     3. Run the command with the repository temporarily disabled
            yum --disablerepo=<repoid> ...

     4. Disable the repository permanently, so yum won't use it by default. Yum
        will then just ignore the repository until you permanently enable it
        again or use --enablerepo for temporary usage:

            yum-config-manager --disable <repoid>
        or
            subscription-manager repos --disable=<repoid>

     5. Configure the failing repository to be skipped, if it is unavailable.
        Note that yum will try to contact the repo. when it runs most commands,
        so will have to try and fail each time (and thus. yum will be be much
        slower). If it is a very temporary problem though, this is often a nice
        compromise:

            yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=true

Cannot find a valid baseurl for repo: base/$releasever/x86_64

yum相关知识

/etc/yum.repos.d/      # yum的默认仓库地址
/etc/yum.conf          # yum的默认配置文件

yum -y install         # 安装,--installroot=[file]指定安装位置,--downloadonly只下载,不按照,--downloaddir指定下载目录
yum list               # 查询安装列表,可以配合grep使用
yum update             # 更新系统的包

yum-config-manager --add-repo [url]   # 添加远程镜像url

但有个疑问:明明安装了centos-release,为啥在解析url时,无法将$releasever替换成对于的版本号(7)?如果有哪位大佬知道,可以分享下