問題
公司使用刀片機的系統版本是CentOS 7.9.2009(Core),本人在重新安裝虛擬機時,也使用對應的系統版本,在安裝軟件時,yum無法正常使用,一開始覺得,centos的release版本默認配置和默認鏡像不可能有問題,所有懷疑是網絡問題,然后就試着折騰,各種配置,各種ping,折騰了好長時間,最后發現是yum的問題,然后就卸載重裝默認的yum,結果還是不行,最終通過學習yum的相關配置才解決問題。
通過設置遠程鏡像url的參數值$releasever為7就可以了。
排查分析
- ping命名,看是否是網絡配置問題;
- 查看/etc/resolv.conf,看配置是否正確,可以增加一行配置nameserver 114.114.114.114,執行命令chattr +i /etc/resolv.conf,然后重啟systemctl restart network試下;
- 根據提示不能解析主機:mirrorlist.centos.org,開始以為是因為該yum所配置的鏡像無法使用,在嘗試更換為http://mirrors.ustc.edu.cn/之后還是不能使用;
- 跟着錯誤提示信息往下找,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)?如果有哪位大佬知道,可以分享下