Loading ...
CentOS 7 yum 无法使用?两种方案轻松解决软件源问题

一、问题现象

在 CentOS 7 系统中执行 yum install、yum update 等命令时,出现以下典型错误:

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; 未知的错误"

核心特征:无法解析官方镜像列表域名,导致 yum 无法获取软件源元数据。

二、问题根源

CentOS 7 官方生命周期于 2024 年 6 月 30 日 结束,官方停止维护后:

  1. 原镜像列表服务(mirrorlist.centos.org)停止解析,导致域名无法访问;

  2. 官方软件仓库(http://mirror.centos.org)被归档至历史仓库(vault.centos.org),旧配置文件中的地址失效。

三、解决方法(推荐使用root账户操作,两种方案任选)

方案一:切换至官方归档仓库(适合需要原汁原味官方源的场景)

步骤详解

  1. 进入仓库配置目录

cd /etc/yum.repos.d  
  1. 备份原始配置(关键!防止操作失误)

cp CentOS-Base.repo CentOS-Base.repo.bak  
  1. 编辑配置文件

使用 vi 或 nano 打开文件:

vi CentOS-Base.repo  
  1. 替换仓库地址

(若系统版本不是 7.9,需将 URL 中的 7.9.2009 替换为对应版本,如 7.8.2003)

# CentOS-Base.repo
#
# The mirror system uses the connecting IP address of the client and the
# update status of each mirror to pick mirrors that are updated to and
# geographically close to the client.  You should use this for CentOS updates
# unless you are manually picking other mirrors.
#
# If the mirrorlist= does not work for you, as a fall back you can try the 
# remarked out baseurl= line instead.
#
#
 
[base]
name=CentOS-$releasever - Base
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#released updates 
[updates]
name=CentOS-$releasever - Updates
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#additional packages that may be useful
[extras]
name=CentOS-$releasever - Extras
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=extras&infra=$infra
#$baseurl=http://mirror.centos.org/centos/$releasever/extras/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
 
#additional packages that extend functionality of existing packages
[centosplus]
name=CentOS-$releasever - Plus
#mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=centosplus&infra=$infra
#baseurl=http://mirror.centos.org/centos/$releasever/centosplus/$basearch/
#baseurl=http://vault.centos.org/7.9.2009/x86_64/os/
baseurl=http://vault.centos.org/7.9.2009/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  1. 生效配置

清理旧缓存并生成新缓存:

yum clean all && yum makecache  
方案二:使用阿里云镜像源(推荐,速度更快、稳定性更高)

步骤详解

  1. 下载阿里云配置文件

若系统安装了 curl 或 wget,直接执行:

# 方法1:通过 curl 下载  
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo  
# 方法2:通过 wget 下载  
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo  

(若上述工具未安装,可手动访问 URL 下载文件,上传至 /etc/yum.repos.d/ 目录)

  1. 验证配置有效性

查看文件内容,确认包含阿里云镜像地址:

grep "mirrors.aliyun.com" /etc/yum.repos.d/CentOS-Base.repo  
  1. 生效配置

清理缓存并重建:

yum clean all && yum makecache  

四、验证解决效果

执行以下命令,若能正常列出仓库信息或安装软件,说明问题解决:

# 查看仓库列表  
yum repolist  
# 测试安装一个基础工具(如 tree)  
yum install -y tree  
暂无评论

发送评论 编辑评论


				
|´・ω・)ノ
ヾ(≧∇≦*)ゝ
(☆ω☆)
(╯‵□′)╯︵┴─┴
 ̄﹃ ̄
(/ω\)
∠( ᐛ 」∠)_
(๑•̀ㅁ•́ฅ)
→_→
୧(๑•̀⌄•́๑)૭
٩(ˊᗜˋ*)و
(ノ°ο°)ノ
(´இ皿இ`)
⌇●﹏●⌇
(ฅ´ω`ฅ)
(╯°A°)╯︵○○○
φ( ̄∇ ̄o)
ヾ(´・ ・`。)ノ"
( ง ᵒ̌皿ᵒ̌)ง⁼³₌₃
(ó﹏ò。)
Σ(っ °Д °;)っ
( ,,´・ω・)ノ"(´っω・`。)
╮(╯▽╰)╭
o(*////▽////*)q
>﹏<
( ๑´•ω•) "(ㆆᴗㆆ)
😂
😀
😅
😊
🙂
🙃
😌
😍
😘
😜
😝
😏
😒
🙄
😳
😡
😔
😫
😱
😭
💩
👻
🙌
🖕
👍
👫
👬
👭
🌚
🌝
🙈
💊
😶
🙏
🍦
🍉
😣
Source: github.com/k4yt3x/flowerhd
颜文字
Emoji
小恐龙
花!
上一篇
下一篇