在Linux系统中,yum(Yellowdog Updater Modified)是一款常用的包管理工具,主要用于自动下载、安装、更新和删除软件包,同时依赖配置的yum源(软件仓库)来获取所需的软件包,正确查看和管理yum源是确保系统软件包管理正常进行的关键步骤,尤其在排查依赖问题、验证软件包可用性或切换镜像源时尤为重要,本文将详细介绍Linux系统中查看yum源的多种方法,包括基础命令、配置文件解析、工具辅助等,帮助用户全面掌握yum源的状态信息。

基础查看命令:yum repolist与yum repolist all
查看yum源最直接的方式是使用yum命令自带的相关参数。yum repolist和yum repolist all是最常用的两个命令,分别用于显示已启用的yum源和所有(包括未启用)的yum源。
查看已启用的yum源
执行yum repolist命令,系统会列出当前已启用(即enabled=1)的yum源,并显示每个源中可用的软件包数量。
[root@localhost ~]# yum repolist Loaded plugins: fastestmirror, langpacks repo id repo name status base/7/x86_64 CentOS-7 - Base 10,019 extras/7/x86_64 CentOS-7 - Extras 418 updates/7/x86_64 CentOS-7 - Updates 2,386 repolist: 12,823
输出结果中,repo id是yum源的标识符(如base/7/x86_64),repo name是源的描述名称(如CentOS-7 - Base),status显示该源中可用的软件包数量(如10,019),最后一行repolist是所有已启用源的总软件包数。
查看所有yum源(包括未启用的)
若需查看系统中配置的所有yum源(无论是否启用),可使用yum repolist all命令,该命令会显示每个源的启用状态(enabled: 1表示启用,enabled: 0表示未启用)及软件包数量。
[root@localhost ~]# yum repolist all Loaded plugins: fastestmirror, langpacks repo id repo name status base/7/x86_64 CentOS-7 - Base enabled: 10,019 base-debuginfo/7/x86_64 CentOS-7 - Base Debuginfo disabled: 0 base-source/7/x86_64 CentOS-7 - Base Source disabled: 0 extras/7/x86_64 CentOS-7 - Extras enabled: 418 extras-debuginfo/7/x86_64 CentOS-7 - Extras Debuginfo disabled: 0 extras-source/7/x86_64 CentOS-7 - Extras Source disabled: 0 updates/7/x86_64 CentOS-7 - Updates enabled: 2,386 updates-debuginfo/7/x86_64 CentOS-7 - Updates Debuginfo disabled: 0 updates-source/7/x86_64 CentOS-7 - Updates Source disabled: 0
通过对比可发现,未启用的源(如base-debuginfo/7/x86_64)状态为disabled: 0,这类源通常用于调试或源码包,默认不启用以节省资源。
查看yum源配置文件:解析.repo文件
yum源的配置信息存储在/etc/yum.repos.d/目录下的.repo文件中(如CentOS-Base.repo、epel.repo等),通过直接查看这些文件,可以了解每个yum源的详细配置参数,包括源名称、镜像地址、启用状态等。
定位配置文件
/etc/yum.repos.d/目录下可能存在多个.repo文件,不同文件对应不同的源(如官方源、第三方源)。
[root@localhost ~]# ls /etc/yum.repos.d/ CentOS-Base.repo CentOS-CR.repo CentOS-Debuginfo.repo CentOS-fasttrack.repo CentOS-Media.repo CentOS-Sources.repo CentOS-Vault.repo
CentOS-Base.repo是官方基础源,CentOS-Updates.repo是更新源,epel.repo是EPEL(Extra Packages for Enterprise Linux)社区源等。
解析配置文件内容
以CentOS-Base.repo为例,其内容通常包含多个[repository]段落,每个段落对应一个yum源:

[base] name=CentOS-$releasever - Base baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 enabled=1 [updates] name=CentOS-$releasever - Updates baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 enabled=1
关键参数说明:
[base]:yum源的标识符,与yum repolist中的repo id对应;name:源的描述信息,与repo name对应;baseurl:源的镜像地址,可以是http://、https://、ftp://或本地路径(如file:///mnt/cdrom/);gpgcheck:是否验证GPG签名(1是,0否);enabled:是否启用该源(1是,0否);gpgkey:GPG公钥的路径,用于验证软件包完整性。
通过修改enabled参数(如将enabled=1改为enabled=0),可手动启用或禁用对应yum源。
使用yum-config-manager管理yum源
yum-config-manager是yum-utils工具包中的命令,用于动态管理yum源的配置(如启用/禁用源、设置优先级等),若系统未安装该工具,可通过yum install yum-utils安装。
查看所有源的启用状态
执行yum-config-manager --set-enabled后跟repo id可启用源,--set-disabled可禁用源,查看所有源的启用状态:
[root@localhost ~]# yum-config-manager --list repo-id repo-name status base CentOS-7 - Base enabled: True updates CentOS-7 - Updates enabled: True epel Extra Packages for Enterprise Linux enabled: False
动态启用/禁用源
若需启用EPEL源(默认未启用),可执行:
[root@localhost ~]# yum-config-manager --enable epel Loaded plugins: fastestmirror, langpacks Repo epel enabled
禁用源则使用--disable参数:
[root@localhost ~]# yum-config-manager --disable epel Loaded plugins: fastestmirror, langpacks Repo epel disabled
查看yum源的元数据与详细信息
yum源在首次访问时会下载元数据(metadata),包含软件包列表、依赖关系等信息,通过查看元数据状态或使用yum info等命令,可进一步验证源的可用性。
检查元数据是否过期
执行yum makecache命令会强制重新加载所有源的元数据,若过程中出现错误(如无法连接镜像站),则说明源配置可能存在问题。
[root@localhost ~]# yum makecache Loaded plugins: fastestmirror, langpacks Determining fastest mirrors * base: mirrors.aliyun.com * extras: mirrors.aliyun.com * updates: mirrors.aliyun.com Metadata Cache Created
若输出Metadata Cache Created,表示元数据加载成功;若报错“Cannot retrieve repository metadata”,需检查baseurl是否正确或网络是否可达。

查看特定源的软件包信息
使用yum info命令结合--enablerepo参数,可查看特定源中的软件包信息,查看EPEL源中的htop工具(需先启用EPEL源):
[root@localhost ~]# yum info --enablerepo=epel htop
Loaded plugins: fastestmirror, langpacks
Available Packages
Name : htop
Arch : x86_64
Version : 3.0.5
Release : 1.el7
Size : 276 k
Repo : epel/x86_64
Summary : Interactive process viewer
URL : http://htop.sourceforge.net/
License : GPLv2
Description : htop is an interactive text-mode process viewer for Unix systems.
It is a cross-platform replacement for the 'top' utility.
输出中Repo: epel/x86_64表明该软件包来自EPEL源,若显示多个Repo,则按enabled状态和源优先级排序。
常用yum源查看命令总结
为方便用户快速查找,以下将常用命令及其作用整理为表格:
| 命令 | 作用 | 示例输出(关键部分) |
|---|---|---|
yum repolist |
查看已启用的yum源及软件包数量 | repolist: 12,823 |
yum repolist all |
查看所有yum源(含未启用)及状态 | base-debuginfo/7/x86_64 disabled: 0 |
cat /etc/yum.repos.d/*.repo |
查看所有yum源配置文件内容 | baseurl=http://mirror.centos.org/... |
yum-config-manager --list |
列出所有源的启用状态 | epel enabled: False |
yum makecache |
重新加载元数据并验证源可用性 | Metadata Cache Created |
相关问答FAQs
Q1: 如何查看yum源是否包含特定软件包?
A: 可使用yum search命令全局搜索软件包,或结合--enablerepo参数指定源,查看base源中是否存在nginx软件包:
yum search --enablerepo=base nginx
若输出nginx.x86_64 : High performance web server,则说明base源包含该包;若无结果,可尝试其他源(如epel):
yum search --enablerepo=epel nginx
Q2: 如何修改yum源的镜像地址?
A: 直接编辑/etc/yum.repos.d/目录下的对应.repo文件,修改baseurl参数,将CentOS官方源替换为阿里云镜像:
- 备份原配置文件:
cp /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.bak
- 编辑
CentOS-Base.repo,将baseurl中的mirror.centos.org替换为mirrors.aliyun.com:[base] name=CentOS-$releasever - Base baseurl=http://mirrors.aliyun.com/centos/$releasever/os/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7 enabled=1
- 清理旧元数据并重新加载:
yum clean all yum makecache
修改后,通过
yum repolist可验证新源是否生效。
原创文章,发布者:酷番叔,转转请注明出处:https://cloud.kd.cn/ask/28806.html