centos7中使用任何与yum相关的命令都报错

在CentOS7虚拟机中安装Docker时遇到yum报错,表现为无法解析yum.dockerproject.org域名。该问题并非由于网络或权限导致,而是yum仓库的问题。解决方案是删除yum.repos.d目录下的所有内容,然后下载阿里云的CentOS-Base.repo文件,以替换原有的Docker仓库。执行相关命令后,通过yumupdate完成系统更新。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

背景

尝试在虚拟机中安装docker,在进行yum update时报错
后来发现不仅yum update,尝试任何跟yum有关的命令都报错,例如yum list

我的centos7配置如下:

[root@localhost mayxue]# uname -a
Linux localhost.localdomain 3.10.0-693.el7.x86_64 #1 SMP Tue Aug 22 21:09:27 UTC 2017 x86_64 x86_64 x86_64 GNU/Linux

温馨提示:参考此篇博客的解决方法之前请仔细对比报错信息是否一致!
先贴出来报错信息如下:

[root@localhost mayxue]# yum list
已加载插件:fastestmirror, langpacks
base                                                     | 3.6 kB     00:00     
docker-ce-stable                                         | 3.5 kB     00:00     
https://yum.dockerproject.org/repo/main/centos/7/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: yum.dockerproject.org; 未知的名称或服务"
正在尝试其它镜像。


 One of the configured repositories failed (Docker Repository),
 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=dockerrepo ...

     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 dockerrepo
        or
            subscription-manager repos --disable=dockerrepo

     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=dockerrepo.skip_if_unavailable=true

failure: repodata/repomd.xml from dockerrepo: [Errno 256] No more mirrors to try.
https://yum.dockerproject.org/repo/main/centos/7/repodata/repomd.xml: [Errno 14] curl#6 - "Could not resolve host: yum.dockerproject.org; 未知的名称或服务"

解决方案

在采用本方案之前首先注意以下几点是否有问题

  1. 虚拟机是否联网。在虚拟机中ping baidu.com是否能ping通
  2. 是否是在root用户下操作

如果联网了,并且是在root用户下操作还是出现了上述报错,那么采取接下来的解决方案

删除/etc/yum.repos.d 目录下的所有内容,重新下载国内的源repo文件即可解决问题

rm -rf  /etc/yum.repos.d/*.repo

然后下载

curl -o /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo

测试一下:输入yum update(期间要选择确认,输入 y 即可)

在这里插入图片描述

注:输入y有出现缺失秘钥等,又重复了一次yum update,最后更新成功,如下图

更新成功!

在这里插入图片描述

### 解决 CentOS 中 `yum` 命令报错的方法 对于 CentOS 8 和 CentOS 7 用户,在执行 `yum` 命令时可能会遇到不同的错误消息。以下是针对这些错误的具体解决方案。 #### 错误一:Failed to set locale 和 Failed to download metadata for repo 'appstream' 当运行 `yum` 命令报错 “Failed to set locale” 或者 “Failed to download metadata for repo ‘appstream’”,这通常发生在 CentOS 8 上。这类问题可以通过更新系统的区域设置或者更换镜像源来解决[^1]。 为了更改镜像源,可以按照如下操作: ```bash sudo mv /etc/yum.repos.d/CentOS-Linux-AppStream.repo /etc/yum.repos.d/CentOS-Linux-AppStream.repo.bak sudo curl -o /etc/yum.repos.d/CentOS-Linux-AppStream.repo http://mirror.centos.org/centos/$releasever/AppStream/x86_64/os/ ``` #### 错误二:Another app is currently holding the yum lock; waiting for it to exit 如果收到此提示,则意味着有另一个进程正在占用 YUM 数据库锁文件 `/var/run/yum.pid` 或者 `/var/lib/rpm/__db.*` 文件被锁定。此时应该等待其他程序完成其任务后再尝试,也可以手动删除上述提到的 PID 文件或 RPM 数据库中的临时文件以解锁[^2]。 #### 错误三:Could not resolve host: mirrorlist.centos.org;Name or service not known 该问题是由于 CentOS 7 的官方支持已终止,并且默认仓库已被迁移至归档位置所致。这意味着即使使用最新版的 CentOS 7,默认情况下也无法正常访问官方提供的软件包资源。建议采取以下措施之一来修复这个问题[^3]: - 更改本地主机上的 DNS 设置; - 使用第三方提供的活跃镜像站点作为新的下载源; - 将现有的基础配置替换为来自阿里云或其他可靠提供商的新版本。 具体做法是在终端里输入下面几条指令来进行修改: ```bash cd /etc/yum.repos.d/ sudo cp CentOS-Base.repo CentOS-Base.repo.backup sudo curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo ``` 通过以上步骤能够有效地解决问题并恢复正常的包管理功能。
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值