Yum使用时报错
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
Could not retrieve mirrorlist http://mirrorlist.centos.org/?release=7&arch=x86_64&repo=os&infra=stock error was
14: curl#6 - "Could not resolve host: mirrorlist.centos.org; 未知的错误"One of the configured repositories failed (未知),and yum doesn't have enough cached data to continue. At this point the onlysafe 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 workingupstream. This is most often useful if you are using a newerdistribution release than is supported by the repository (and thepackages for the previous distribution release still work).3. Run the command with the repository temporarily disabledyum --disablerepo=<repoid> ...4. Disable the repository permanently, so yum won't use it by default. Yumwill then just ignore the repository until you permanently enable itagain or use --enablerepo for temporary usage:yum-config-manager --disable <repoid>orsubscription-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 muchslower). If it is a very temporary problem though, this is often a nicecompromise:yum-config-manager --save --setopt=<repoid>.skip_if_unavailable=trueCannot find a valid baseurl for repo: base/7/x86_64
这个错误表明您下载的 CentOS-Base.repo 文件内容不是有效的 Yum 仓库配置文件,而是一个 HTML 网页(可能是错误页面或跳转页面)。
问题原因:
您使用的下载链接 https://mirrors.aliyun.com/repo/Centos-vault-7.9.2009.repo 可能已经失效或不再提供直接的文件下载,而是返回了一个 HTML 页面。
解决方案
请按照以下步骤手动创建正确的仓库配置文件:
删除无效的配置文件
rm -f /etc/yum.repos.d/CentOS-Base.repo
直接手动创建配置文件
使用 vi 或 nano 编辑器创建新文件:
vi /etc/yum.repos.d/CentOS-Base.repo
将以下内容复制粘贴到文件中
这里提供阿里云和清华大学两个源的配置,您可以选择其中一个:
选项 A:阿里云 CentOS-Vault 源
ini
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.aliyun.com/centos-vault/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
选项 B:清华大学 CentOS-Vault 源
ini
[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
保存并退出编辑器
在 vi 中:按 Esc 键,然后输入 :wq,按回车。
在 nano 中:按 Ctrl+X,然后按 Y 确认保存,再按回车确认文件名。
清理缓存并重新生成
yum clean all
yum makecache
现在测试 Yum 是否正常工作
yum update
解释
我们手动创建了一个配置文件,直接指定了 baseurl(具体的镜像地址),而不是使用 mirrorlist(需要动态解析的镜像列表),这样就绕过了 DNS 解析问题。
由于 CentOS 7 已经停止维护,我们使用了 7.9.2009 这个最终版本的归档目录(vault),这些地址是稳定的,不会变动。
选择阿里云或清华大学的镜像源通常在国内访问速度较快。
完成以上步骤后,您的 Yum 应该就能正常工作了。如果还有其他仓库(如 epel)出现问题,也需要对 /etc/yum.repos.d/ 目录下的其他 .repo 文件进行类似的处理。