【CentOS7.6】yum 报错:Could not retrieve mirrorlist http://mirrorlist.centos.org

2024.10.22 更新:不知道是否出现了阿里云不再支持 centos 镜像的问题,评论区有反馈。

如果大家还能使用下面的方法解决问题,请在评论区留言,谢谢。
如果大家还能使用下面的方法解决问题,请在评论区留言,谢谢。
如果不能的话请务必在评论区告诉我我来更新。谢谢各位
如果不能的话请务必在评论区告诉我我来更新。谢谢各位

一、报错

1.报错内容如下

在使用 yum makecache 命令时报错,在 yum install -y xxx 的时候报错等等

[root@hcss-ecs-a901 yum.repos.d]# yum makecache
Loaded plugins: fastestmirror
Determining fastest mirrors
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

2.原因分析

我们的服务器没法访问外面的网络,所以我们需要配置镜像源

二、解决

1.进入目录

先进入目录 /etc/yum.repos.d

cd /etc/yum.repos.d
2.打开 xftp

在这里插入图片描述

3.备份文件

备份这三个文件就行了
(注意!!很多人只有 CentOS-Base.repo 文件,没有后两个,这也没问题,有几个备份几个,不用额外创建!)

在这里插入图片描述

4.编辑 CentOS-Base.repo 文件(必选)

!!! 警告,在编辑文件的时候,你肯定是复制黏贴下面的内容的。但是,vim 编辑器会有个黏贴反应时间,所以大概率你在黏贴了内容之后,最开头的内容会缺少 1-5 个字符,你需要手动补全,下面的编辑也是一样 !!!
编辑 CentOS-Base.repo 文件:

vi /etc/yum.repos.d/CentOS-Base.repo

替换内容为:

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.aliyun.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.aliyun.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.aliyun.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
5.编辑 CentOS-SCLo-scl-rh.repo 文件(如果没有这个文件,不用管)

编辑 CentOS-SCLo-scl-rh.repo 文件:

vi /etc/yum.repos.d/CentOS-SCLo-scl-rh.repo

替换内容为:

[centos-sclo-rh]
name=CentOS-$releasever - SCLo rh
baseurl=https://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/rh/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
6.编辑 CentOS-SCLo-scl.repo 文件(如果没有这个文件,不用管)

编辑 CentOS-SCLo-scl.repo 文件:

vi /etc/yum.repos.d/CentOS-SCLo-scl.repo

替换内容为:

[centos-sclo-sclo]
name=CentOS-$releasever - SCLo sclo
baseurl=https://mirrors.aliyun.com/centos/$releasever/sclo/$basearch/sclo/
gpgcheck=1
enabled=1
gpgkey=https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-7
7.清理 yum 缓存
yum clean all
6.重新生成 yum 缓存
yum makecache

请注意!如果在你之后的 yum install 过程中发生了 GPGKEY 报错,请参考这篇博客:
请注意!如果在你之后的 yum install 过程中发生了 GPGKEY 报错,请参考这篇博客:
请注意!如果在你之后的 yum install 过程中发生了 GPGKEY 报错,请参考这篇博客:

https://blog.csdn.net/qq_43768851/article/details/140113001?spm=1001.2014.3001.5501

三、结果

1.再次 yum makecache 的结果如下

圆满成功

在这里插入图片描述

### CentOS7 Yum 安装 Wget 报错 `Could not resolve host: mirrorlist.centos.org` 的解决方案 当执行 `yum install wget` 或其他基于 Yum 的操作时,如果遇到报错 `Could not resolve host: mirrorlist.centos.org`,这通常是因为系统的 DNS 配置不正确或 Yum 源配置存在问题。以下是具体的解决方法: #### 修改 Yum 源配置 1. **备份原始的 Yum 源文件** 将现有的 `/etc/yum.repos.d/CentOS-Base.repo` 文件重命名为 `.bak` 后缀以便备份: ```bash sudo mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak ``` 2. **下载新的 Yum 源文件** 使用阿里云或其他稳定镜像站点替换默认的 Yum 源文件。例如,通过以下命令获取阿里云的 Yum 源文件: ```bash sudo wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo ``` 如果希望使用华为云镜像,则可以运行如下命令: ```bash sudo wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.huaweicloud.com/repository/conf/CentOS-7-anon.repo ``` 3. **清理并重建缓存** 替换完成后,清除旧的缓存数据并重新生成新缓存: ```bash sudo yum clean all sudo yum makecache ``` 4. **验证网络连接** 确认主机能够正常访问外部互联网资源。可以通过测试 DNS 和 Ping 命令来完成此步骤: ```bash nslookup mirrors.aliyun.com ping mirrors.aliyun.com ``` 若发现无法解析域名或超时现象,需进一步排查本地网络设置。 5. **尝试再次安装软件包** 执行初始失败的操作以确认问题已修复: ```bash sudo yum install -y wget ``` 以上过程解决了由于官方镜像不可达而导致的问题[^3]。 --- ### 注意事项 - 更改后的 Yum 源应始终指向可靠的第三方镜像地址(如阿里云、腾讯云等),这些服务提供更稳定的国内访问支持。 - 对于某些特殊环境下的机器可能还需要调整防火墙规则允许必要的端口通信以及禁用 SELinux 来减少冲突可能性。 ```python print("成功解决问题后即可正常使用YUM工具进行各种应用部署工作啦!") ```
评论 42
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

MicroLindb

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值