由于网络问题导致 yum 无法解析 CentOS 的镜像源地址(“Could not resolve host: mirrorlist.centos.org; 未知的错误“)

报错:

已加载插件:fastestmirror, langpacks
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 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/7/x86_64

这个错误通常是由于网络问题导致 yum 无法解析 CentOS 的镜像源地址。以下是一些可能的解决方法:

1. 检查网络连接

  • 你可以先检查虚拟机的网络是否正常,使用 ping 测试一下外网连接,比如:
ping 8.8.8.8

或者

ping mirrorlist.centos.org

如果 ping 不通,可能是虚拟机的网络配置问题。

2. 检查虚拟机的网络配置

如果网络连接失败,可能需要检查虚拟机的网络模式,确保它能够连接到外部网络:

  • 如果虚拟机使用的是 仅主机模式(Host-Only Mode),尝试将网络模式切换为 NAT 模式桥接模式
  • 切换完模式后,重启网络服务:
sudo systemctl restart network

3. 配置DNS

如果网络连接正常但仍然无法解析域名,可能是 DNS 配置有问题。可以手动修改 /etc/resolv.conf 文件,添加一个公共 DNS 服务器,例如 Google 的 DNS:

  1. 编辑 /etc/resolv.conf
sudo nano /etc/resolv.conf
  1. 添加以下内容:
nameserver 8.8.8.8
nameserver 8.8.4.4
  1. 保存文件并退出。然后再次尝试运行 yum 命令。

4. 更换 CentOS 的镜像源

如果依然报错,可以尝试更换镜像源。

  1. 备份现有的 yum 源配置:
sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
  1. 下载新的镜像源配置文件(例如阿里云的镜像源):
sudo curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
  1. 运行 yum clean all 清理缓存,并再次尝试安装软件包:
sudo yum clean all
sudo yum makecache
sudo yum install epel-release

如果你能解决 DNS 问题或网络问题,应该可以正常使用 yum

我自己是按照1.->3.->4.进行操作后能正常使用yum


谢谢观看!希望能帮到你。

猜你喜欢

转载自blog.csdn.net/m0_74412436/article/details/142947797