起因
本人自建的服务器早期安装了Centos7的系统,但是最近使用过程中发现centos7已经在2024年6月30日,生命周期结束,官方不再进行支持维护,如果使用官方yum源下载环境服务,就会报下列的问题,从下面情况来看就是http://mirrorlist.centos.org已经解析不到了,我后面用浏览器访问了一下,确实网站已经关闭了。
Loaded plugins: fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org?arch=x86_64&release=7&repo=sclo-rh 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: centos-sclo-rh/x86_64
那么问题就来了!
怎么使用yum???
你肯定是这样想的:
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
然后再清理缓存,我刚开始也是这么弄的,但是发现依然没什么卵用,它还是会出现上面的问题,接下来我折腾了两天。
下面讲讲具体怎么解决的
- 备份
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
- 下载新的 CentOS-Base.repo 到 /etc/yum.repos.d/
centos8(centos8官方源已下线,建议切换centos-vault源)
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
centos6(centos6官方源已下线,建议切换centos-vault源)
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
或
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
- 运行 yum makecache 生成缓存
这里可能依然会出现上面的
Could not retrieve mirrorlist http://mirrorlist.centos.org?arch=x86_64&release=7&repo=sclo-rh
- 接下来查找一下mirrorlist相关的内容
grep -r 'mirrorlist' /etc/yum.repos.d/
你可能会得到类似的结果
/etc/yum.repos.d/CentOS-CR.repo:# NOTE: We do not use a mirrorlist for the CR repos, to ensure content is available
/etc/yum.repos.d/CentOS-Sources.repo:# If the mirrorlist= does not work for you, as a fall back you can try the
/etc/yum.repos.d/CentOS-fasttrack.repo:mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=fasttrack&infra=$infra
/etc/yum.repos.d/CentOS-x86_64-kernel.repo:mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=kernel&infra=$infra
/etc/yum.repos.d/CentOS-x86_64-kernel.repo:mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=experimental&infra=$infra
/etc/yum.repos.d/CentOS-SCLo-scl-rh.repo:mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-rh
/etc/yum.repos.d/CentOS-SCLo-scl.repo:mirrorlist=http://mirrorlist.centos.org?arch=$basearch&release=7&repo=sclo-sclo
/etc/yum.repos.d/CentOS-Base.repo:# If the mirrorlist= does not work for you, as a fall back you can try the
接下来一个一个就打开这些repo文件,记得sudo,因为这些文件权限比较高具体怎么修改就不一一展示了,下面举一个例子说明
cd /etc/yum.repos.d/ #先进入那个文件夹
ls #查看当前路径下的目录
然后你就能看到下面这一堆
CentOS-Base.repo CentOS-SCLo-scl-rh.repo epel-testing.repo
CentOS-CR.repo CentOS-Sources.repo mysql-community.repo
CentOS-Debuginfo.repo CentOS-Vault.repo mysql-community-source.repo
CentOS-fasttrack.repo CentOS-x86_64-kernel.repo nodesource-nodejs.repo
CentOS-Media.repo docker-ce.repo nodesource-nsolid.repo
CentOS-SCLo-scl.repo epel.repo
接着是说明示例
sudo vim CentOS-SCLo-scl-rh.repo #或者切到root用户,就不用sudo了
打开之后你就能看到下面这个,你看到的可能和这个些许不同,应该第8行是注释掉的,第9行是没注释的,接下来把第9行这个mirrorlist注释掉,然后把第8行的**mirror.centos.org ** 直接替换成mirrors.aliyun.com ,后面的不动,这里是因为centos的源已经关掉了。
以此类推,把与mirrorlist有关的源都这样替换,并注释掉mirrorlist行
貌似还有个办法就是上面说的打开 vault源
就是设置 enabled=1 ,这个方法我没有去验证,你们可以试下,可行的话可以在评论区留言
5. 上面这些修改完成之后,清理一下yum缓存
yum clean all
- 然后重新生成缓存
yum makecache
然后应该你就能看到yum又正常工作了
注意事项
- 阿里云ecs用户可参考下面的方法
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.backup
wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.cloud.aliyuncs.com/repo/Centos-vault-8.5.2111.repo
sed -i 's/mirrors.cloud.aliyuncs.com/url_tmp/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/mirrors.aliyun.com/mirrors.cloud.aliyuncs.com/g' /etc/yum.repos.d/CentOS-Base.repo && sed -i 's/url_tmp/mirrors.aliyun.com/g' /etc/yum.repos.d/CentOS-Base.repo
yum clean all && yum makecache
- 刚安装的Centos 7,8等已经停止维护的Centos系统版本可能需要先进行上面的一系列操作,才能使用yum工具,我在别的社区看到有人说遇到了这种情况,进入了死循环,没有yum下载不了wegt和curl,那可能就需要你到阿里云镜像的地址下载下来然后传到服务器上去覆盖
/etc/yum.repos.d/CentOS-Base.repo