新手必看!Linux RPM与Yum全套命令+第三方源(Docker/K8s/EPEL)配置详解

摘要:本文详细介绍了 Linux 系统中 RPM 包的管理方法。首先,梳理了使用 RPM 包管理系统的 Linux 发行版本(如 RHEL、CentOS、Fedora 等),并解释了 RPM 包的文件名格式和组成部分。接着,深入讲解了 rpm 命令的查询、验证、安装、卸载及文件提取等操作。随后,文章介绍了更高级的包管理工具 yum,包括其常用命令(如 installupdateremovehistory 等)和软件包组管理。最后,详细说明了如何配置和管理 yum 仓库,包括查看、启用/禁用仓库、缓存管理以及添加第三方仓库(如 Kubernetes、Docker CE、EPEL)的方法。

一、分析 RPM 包

(一)Linux 发行版本

以 RHEL 为代表的发行版本,使用rpm包管理系统:
• RHEL (Red Hat Enterprise Linux)
• Fedora(由原来的RedHat桌面版本发展而来,免费版本)
• CentOS(RHEL的社区克隆版本,免费)
• CentOS Stream(RHEL的社区克隆版本,免费)
• Rocky(RHEL的社区克隆版本,免费)
• OEL(Oralce Enterprise Linux)
• 以 Debian 为代表的发行版本,使用deb包管理系统:
• Debian,社区Linux的典范,迄今为止最遵循GNU规范的Linux系统。
• Ubuntu,Debian衍生版,是一个以桌面应用为主的Linux操作系统。
• Kali Linux,Debian衍生版,旨在渗透测试和数字取证。
• 其他版本:ArchLinux Gentoo 等等…

在这里插入图片描述

(二)RPM 包文件名格式

redhat 开发了 rpm 包管理,提供一个标准的软件版本管理方法,比从归档包解压文件到系统简单多了。CentOS 提供的所有软件都是rpm格式。
RPM软件包文件名格式:name-version-release.architecture.rpm
示例:lrzsz-0.12.20-36.el7.x86_64.rpm
• name,是描述其内容的一个或多个词语(lrzsz)。
• version,是原始软件的版本号(0.12.20)。
• release,是基于该版本的发行版号,由软件打包商设置,后者不一定是原始软件开发商(36.el7)。
• architecture,是编译的软件包运行的处理器架构。
• noarch,表示此软件包不限定架构。
• x86_64,表示此软件包限定x86 64位。
• aarch64,表示此软件包限定ARM 64位。

(三)RPM 包组成

每个rpm包涵三个部分:
• 需要安装的文件。
• 包的元数据信息,包括包的名称、版本、架构等;软件包说明;软件包依赖关系;许可证;更变日志;其他信息。
• 脚本:软件包安装,更新,卸载需要执行的脚本。
通常,软件提供商使用GPG密钥对RPM软件包进行数字签名(Red Hat会对其发布的所有软件包进行数字签名)。 RPM系统通过确认包由相应的GPG密钥签名来验证包的完整性。 如果GPG签名不匹配,RPM系统拒绝安装包。

(四)非对称加密

非对称加密:有一对公钥和私钥。
• 公钥:分享给别人,用来加密数据。
• 私钥:自己保留,用来解密公钥加密的数据。
Linux中rpm包会被私钥签名,客户端使用公钥验证签名,确保文件的完整性。

(五)RPM 包安装和更新

•如果同一个软件有多个版本,只需安装最高版本。
• 在大多数情况下,一个软件只能安装一个版本。 kernel是个例外。如果构建包的文件名没有冲突,则可以安装多个版本。 由于只能通过引导到该内核来测试新内核,因此特定设计了包,以便可以一次安装多个版本。如果内核无法启动,则旧内核仍然可用且可引导。
• 软件包升级只需要安装最新版本,不需要逐步升级。
• 升级RPM包将删除旧版本的软件包并安装新版本,通常会保留配置文件。

(六)rpm 命令

1.查询(重点)

基本语法:

rpm {-q|--query} [select-options] [query-options]

   select-options
        [PACKAGE_NAME] [-a,--all] [-f,--file FILE]
        [-g,--group GROUP] {-p,--package PACKAGE_FILE]
        [--hdrid SHA1] [--pkgid MD5] [--tid TID]
        [--querybynumber HDRNUM] [--triggeredby PACKAGE_NAME]
        [--whatprovides CAPABILITY] [--whatrequires CAPABILITY]

   query-options
        [--changelog] [-c,--configfiles] [--conflicts]
        [-d,--docfiles] [--dump] [--filesbypkg] [-i,--info]
        [--last] [-l,--list] [--obsoletes] [--provides]
        [--qf,--queryformat QUERYFMT] [-R,--requires]
        [--scripts] [-s,--state] [--triggers,--triggerscripts]

示例:

# 查询系统中安装了哪些软件包
[root@server ~ 09:09:05]# rpm -qa
virt-what-1.18-4.el7_9.1.x86_64
grub2-common-2.02-0.87.0.1.el7.centos.9.noarch
firewalld-filesystem-0.6.3-13.el7_9.noarch
setup-2.8.71-11.el7.noarch
sg3_utils-1.37-19.el7.x86_64
......

# 查询系统中某个软件包是否安装
[root@server ~ 09:58:59]# rpm -q kernel
kernel-3.10.0-1160.71.1.el7.x86_64
[root@server ~ 09:59:14]# rpm -q kernelhjsk
未安装软件包 kernelhjsk 

# 查询系统中某个已安装的软件包元数据信息
[root@server ~ 09:59:56]# rpm -q httpd -i
Name        : httpd
Version     : 2.4.6
Release     : 99.el7.centos.1
Architecture: x86_64
Install Date: 2026年05月17日 星期日 20时58分51秒
Group       : System Environment/Daemons
Size        : 9829328
License     : ASL 2.0
Signature   : RSA/SHA256, 2023年05月30日 星期二 23时15分45秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : httpd-2.4.6-99.el7.centos.1.src.rpm
Build Date  : 2023年05月30日 星期二 22时02分56秒
Build Host  : x86-01.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://httpd.apache.org/
Summary     : Apache HTTP Server
Description :
The Apache HTTP Server is a powerful, efficient, and extensible
web server.

# 查询系统中某个已安装的软件包包涵哪些文件
[root@server ~ 10:00:08]# rpm -q openssh-server -l
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
/usr/lib/systemd/system/sshd-keygen.service
/usr/lib/systemd/system/sshd.service
/usr/lib/systemd/system/sshd.socket
/usr/lib/systemd/system/sshd@.service
/usr/lib64/fipscheck/sshd.hmac
/usr/libexec/openssh/sftp-server
/usr/sbin/sshd
/usr/sbin/sshd-keygen
/usr/share/man/man5/moduli.5.gz
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz
/var/empty/sshd

# 查询系统中某个已安装的软件包包涵哪些配置文件
[root@server ~ 12:34:18]# rpm -q openssh-server -c
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd

# 查询系统中某个已安装的软件包包涵哪些文档文件
[root@server ~ 10:03:10]# rpm -q openssh-server -d
/usr/share/man/man5/moduli.5.gz
/usr/share/man/man5/sshd_config.5.gz
/usr/share/man/man8/sftp-server.8.gz
/usr/share/man/man8/sshd.8.gz

# 查询系统中某个某个文件属于哪个已安装的软件包
[root@server ~ 10:03:31]# rpm -q -f /etc/ssh/sshd_config 
openssh-server-7.4p1-22.el7_9.x86_64

# 查询系统中某个已安装的软件包包涵的脚本
[root@server ~ 10:04:03]# rpm -q openssh-server --scripts 
preinstall scriptlet (using /bin/sh):
......

# 查询系统中某个已安装的软件包变更日志
[root@server ~ 10:04:26]# rpm -q openssh-server --changelog 
* 四 930 2021 Dmitry Belyavskiy <dbelyavs@redhat.com> - 7.4p1-22 + 0.10.3-2
- avoid segfault in Kerberos cache cleanup (#1999263)
- fix CVE-2021-41617 (#2008884)
......

# 下载 httpd 软件包,以及依赖的其他软件包
[root@server ~ 10:05:12]# yum install -y yum-utils
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
......
[root@server ~ 10:08:25]# yumdownloader httpd
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
httpd-2.4.6-99.el7.centos.1.x86_64.rpm                                                 | 2.7 MB  00:00:03     
[root@server ~ 10:08:44]# ls httpd-*
httpd-2.4.6-99.el7.centos.1.x86_64.rpm

# 查询系统中某个软件包(package)文件包涵哪些配置文件
[root@server ~ 10:09:07]# rpm -q -p httpd-2.4.6-99.el7.centos.1.x86_64.rpm -c
/etc/httpd/conf.d/autoindex.conf
/etc/httpd/conf.d/userdir.conf
/etc/httpd/conf.d/welcome.conf
......
# 同样-l -d -i也可以配合-p使用

# 查询系统中某个软件包组中包涵哪些软件包
[root@server ~ 10:09:14]# rpm -qg 'System Environment/Base'
grub2-common-2.02-0.87.0.1.el7.centos.9.noarch
centos-release-7-9.2009.1.el7.centos.x86_64
setup-2.8.71-11.el7.noarch
filesystem-3.2-25.el7.x86_64
......

扩展

# 查看软件包所在仓库精确位置
[root@server ~ 10:09:50]# repoquery --location httpd
http://mirrors.aliyun.com/centos/7/updates/x86_64/Packages/httpd-2.4.6-99.el7.centos.1.x86_64.rpm

# 此时可以通过 wget 下载特定文件
[root@server ~ 10:27:45]# wget \
> http://mirrors.aliyun.com/centos/7/updates/x86_64/Packages/httpd-2.4.6-99.el7.centos.1.x86_64.rpm
2.验证

基本语法:

rpm {-V|--verify} [select-options] [verify-options]

   select-options
        [PACKAGE_NAME] [-a,--all] [-f,--file FILE]
        [-g,--group GROUP] {-p,--package PACKAGE_FILE]
        [--hdrid SHA1] [--pkgid MD5] [--tid TID]
        [--querybynumber HDRNUM] [--triggeredby PACKAGE_NAME]
        [--whatprovides CAPABILITY] [--whatrequires CAPABILITY]

   verify-options
        [--nodeps] [--nofiles] [--noscripts]
        [--nodigest] [--nosignature]
        [--nolinkto] [--nofiledigest] [--nosize] [--nouser]
        [--nogroup] [--nomtime] [--nomode] [--nordev]
        [--nocaps] [--noconfig] [--noghost]

示例:

# 查看openssh-server软件安装是否有问题
[root@server ~ 10:28:47]# rpm -V openssh-server

# 修改(change)
[root@server ~ 10:29:19]# sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
[root@server ~ 10:29:31]# rpm -V openssh-server
S.5....T.  c /etc/ssh/sshd_config
[root@server ~ 10:29:34]# sed -i 's/PermitRootLogin no/PermitRootLogin yes/g' /etc/ssh/sshd_config

# 移走 /etc/ssh/sshd_config 文件,再次查看
[root@server ~ 10:29:51]# mv /etc/ssh/sshd_config .
[root@server ~ 10:30:11]# rpm -V openssh-server
遗漏   c /etc/ssh/sshd_config

# 恢复该文件到原来位置
[root@server ~ 10:30:16]# mv sshd_config /etc/ssh/sshd_config
[root@server ~ 10:30:51]# rpm -V openssh-server
S.5....T.  c /etc/ssh/sshd_config
3.安装和卸载

基本语法:

rpm {-i|--install} [install-options] PACKAGE_FILE ...

   install-options
        [--allfiles] [--badreloc] [--excludepath OLDPATH]
        [--excludedocs] [--force] [-h,--hash]
        [--ignoresize] [--ignorearch] [--ignoreos]
        [--includedocs] [--justdb] [--nocollections]
        [--nodeps] [--nodigest] [--nosignature] [--noplugins]
        [--noorder] [--noscripts] [--notriggers]
        [--oldpackage] [--percent] [--prefix NEWPATH]
        [--relocate OLDPATH=NEWPATH]
        [--replacefiles] [--replacepkgs]
        [--test]

示例:

# 下载软件包
[root@server ~ 10:30:55]# repoquery --location lrzsz
http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm
[root@server ~ 10:31:21]# wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm

# 安装
[root@server ~ 10:31:42]# rpm -i lrzsz-0.12.20-36.el7.x86_64.rpm
[root@server ~ 10:32:05]# rpm -q lrzsz 
lrzsz-0.12.20-36.el7.x86_64

# 卸载
[root@server ~ 10:32:15]# rpm -e lrzsz 
[root@server ~ 10:32:25]# rpm -q lrzsz
未安装软件包 lrzsz 

# 友好方式安装
[root@server ~ 10:32:57]# rpm -ivh lrzsz-0.12.20-36.el7.x86_64.rpm
准备中...                          ################################# [100%]
正在升级/安装...
   1:lrzsz-0.12.20-36.el7             ################################# [100%]

# 友好方式卸载
[root@server ~ 10:33:09]# rpm -evh lrzsz 
准备中...                          ################################# [100%]
正在清理/删除...
   1:lrzsz-0.12.20-36.el7             ################################# [100%]

# 安装 httpd,报错如下,下一节讲解如下解决
# 安装准备:确保系统中没有提供httpd需要的依赖包
[root@server ~ 10:33:23]# rpm -e apr apr-util httpd-tools mailcap
错误:依赖检测失败:
	libapr-1.so.0()(64bit)(已安?) httpd-2.4.6-99.el7.centos.1.x86_64 需要
	libaprutil-1.so.0()(64bit)(已安?) httpd-2.4.6-99.el7.centos.1.x86_64 需要
	httpd-tools = 2.4.6-99.el7.centos.1 被 (已安?) httpd-2.4.6-99.el7.centos.1.x86_64 需要
	/etc/mime.types 被 (已安?) httpd-2.4.6-99.el7.centos.1.x86_64 需要
[root@server ~ 10:33:42]# rpm -ivh httpd-2.4.6-99.el7.centos.1.x86_64.rpm 
4.重新安装
rpm {--reinstall} [install-options] PACKAGE_FILE ...

示例:

[root@server ~ 10:34:11]# rpm --reinstall -vh lrzsz*
准备中...                          ################################# [100%]
正在升级/安装...
   1:lrzsz-0.12.20-36.el7             ################################# [100%]
5.提取 RPM 包中文件
  • Windows 提取

使用压缩工具(例如360压缩)打开rpm包,浏览和提取。

  • Linux 提取

• 方法一:分步骤提取

 # 将文件转换为cpio格式 
[root@server ~ 12:35:03]# rpm2cpio httpd-2.4.6-99.el7.centos.1.x86_64.rpm > httpd-2.4.6-99.el7.centos.1.x86_64.cpio

# 查询 cpio 格式(一种打包格式)文件中有哪些文件 
[root@server ~ 12:40:04]# cpio -t < httpd-2.4.6-99.el7.centos.1.x86_64.cpio
./etc/httpd
./etc/httpd/conf
./etc/httpd/conf.d
./etc/httpd/conf.d/README
......

# 配置管道查询特定文件
[root@server ~ 12:40:25]# cpio -t < httpd-2.4.6-99.el7.centos.1.x86_64.cpio |grep 'httpd.conf'
./etc/httpd/conf
./etc/httpd/conf.d
./etc/httpd/conf.d/README
......

# 提取所有文件,-d选项指明提取过程中创建对应的目录分层结构 
[root@server ~ 12:42:25]# cpio -id '*' < httpd-2.4.6-99.el7.centos.1.x86_64.cpio

# 提取特定文件 
[root@server ~ 12:42:59]# cpio -id ./etc/httpd/conf/httpd.conf < httpd-2.4.6-99.el7.centos.1.x86_64.cpio

•方法二:一步提取所有文件

[root@server ~ 11:32:26]# rpm2cpio httpd-2.4.6-99.el7.centos.1.x86_64.rpm | cpio -id '*'
19347

二、使用 yum 管理软件包

(一)yum 介绍

• rpm 命令是一个管理软件包的工具,不适用于软件包存储库或自动解决来自多个源的依赖项。
• Yum(Yellowdog Updater Modified)旨在成为管理基于RPM的软件安装和更新的更好系统。yum命令允许安装,更新,删除和获取有关软件包及其依赖项的信息。

(二)yum 命令

1.help

作用:

# 查看yum命令帮助信息
[root@server ~ 12:44:44]# yum help
2.info

作用:查看仓库中某个软件包信息

# 查看仓库中httpd包信息
[root@server ~ 16:39:28]# yum info httpd
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
可安装的软件包
名称    :httpd
架构    :x86_64
版本    :2.4.6
发布    :99.el7.centos.1
大小    :2.7 M
源    :updates/7/x86_64
简介    : Apache HTTP Server
网址    :http://httpd.apache.org/
协议    : ASL 2.0
描述    : The Apache HTTP Server is a powerful, efficient, and extensible
         : web server.
3.list

作用:查询软件包清单

# 查看仓库中http开头的包有哪些
[root@server ~ 16:39:32]# yum list 'http*'
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
已安装的软件包
httpd-tools.x86_64                                        2.4.6-99.el7.centos.1                       @updates
可安装的软件包
http-parser.i686                                          2.7.1-9.el7                                 base    
http-parser.x86_64                                        2.7.1-9.el7                                 base    
http-parser-devel.i686                                    2.7.1-9.el7                                 base    
......

# 查看仓库中httpd包有哪些版本
[root@server ~ 16:40:25]# yum list httpd --showduplicates
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
可安装的软件包
httpd.x86_64                                   2.4.6-95.el7.centos                                     base   
httpd.x86_64                                   2.4.6-97.el7.centos                                     updates
httpd.x86_64                                   2.4.6-97.el7.centos.1                                   updates
httpd.x86_64                                   2.4.6-97.el7.centos.2                                   updates
httpd.x86_64                                   2.4.6-97.el7.centos.4                                   updates
httpd.x86_64                                   2.4.6-97.el7.centos.5                                   updates
httpd.x86_64                                   2.4.6-98.el7.centos.6                                   updates
httpd.x86_64                                   2.4.6-98.el7.centos.7                                   updates
httpd.x86_64                                   2.4.6-99.el7.centos.1                                   updates
4.search(了解)

作用:根据关键字搜索软件包

# 查看简介包含关键字的软件包
[root@server ~ 16:41:18]# yum search 'web server'

# 查看描述和简介包含关键字的软件包
[root@server ~ 16:41:49]# yum search all 'web server'
5.provides

作用:查看仓库中哪个包可以提供相应文件

# 查看仓库中哪个包可以提供文件sar程序
[root@server ~ 16:42:40]# yum provides sar
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
docker-ce-stable/7/x86_64/filelists_db                                                 |  66 kB  00:00:00     
mirrors.aliyun.com_kubernetes_yum_repos_kubernetes-el7-x86_64_/filelists               |  45 kB  00:00:00     
sysstat-10.1.5-19.el7.x86_64 : Collection of performance monitoring tools for Linux
源    :base
匹配来源:
文件名    :/usr/bin/sar



sysstat-10.1.5-20.el7_9.x86_64 : Collection of performance monitoring tools for Linux
源    :updates
匹配来源:
文件名    :/usr/bin/sar



sysstat-10.1.5-20.el7_9.x86_64 : Collection of performance monitoring tools for Linux
源    :@updates
匹配来源:
文件名    :/usr/bin/sar
6.install

作用:安装软件包

[root@server ~ 16:43:37]# yum install -y vsftpd

# 只下载不安装,将依赖软件包一并下载
# 前提是系统中没安装对应软件包,才会下载
[root@server ~ 16:45:13]# yum install vsftpd --downloadonly --downloaddir=/root/
[root@server ~ 16:45:48]# ls vsftpd-*
vsftpd-3.0.2-29.el7_9.x86_64.rpm
7.localinstall

作用:安装本地文件系统中软件包

# 安装本地文件系统中vsftpd包
[root@server ~ 16:45:56]# yum localinstall ./vsftpd-3.0.2-29.el7_9.x86_64.rpm 
8.downgrade

作用:软件包降级

# 降级 vsftpd 版本为 3.0.2-28.el7
[root@server ~ 16:46:47]# yum downgrade -y vsftpd-3.0.2-28.el7
# 降级的时候,要一并降级依赖软件包

依赖降级示例:

# 环境准备:安装最新版httpd
[root@server ~ 16:47:33]# yum install -y httpd

# 降级到特定版本:降级失败,因为已安装的 httpd-tools 无法满足当前httpd版本
[root@server ~ 16:48:54]# yum downgrade -y httpd-2.4.6-97.el7.centos 
已加载插件:fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
正在解决依赖关系
There are unfinished transactions remaining. You might consider running yum-complete-transaction, or "yum-complete-transaction --cleanup-only" and "yum history redo last", first to finish them. If those don't work you'll have to try removing/installing packages by hand (maybe package-cleanup can help).
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-97.el7.centos 将被 降级
--> 正在处理依赖关系 httpd-tools = 2.4.6-97.el7.centos,它被软件包 httpd-2.4.6-97.el7.centos.x86_64 需要
---> 软件包 httpd.x86_64.0.2.4.6-99.el7.centos.1 将被 删除
--> 解决依赖关系完成
错误:软件包:httpd-2.4.6-97.el7.centos.x86_64 (updates)
          需要:httpd-tools = 2.4.6-97.el7.centos
          已安装: httpd-tools-2.4.6-99.el7.centos.1.x86_64 (@updates)
              httpd-tools = 2.4.6-99.el7.centos.1
          可用: httpd-tools-2.4.6-98.el7.centos.7.x86_64 (updates)
              httpd-tools = 2.4.6-98.el7.centos.7
              ......
 您可以尝试添加 --skip-broken 选项来解决该问题
 您可以尝试执行:rpm -Va --nofiles --nodigest

 # 解决方法:httpd-tools与httpd一起降级到相同版本
 [root@server ~ 16:49:42]# yum downgrade -y httpd-tools-2.4.6-97.el7.centos httpd-2.4.6-97.el7.centos
9.update

作用:软件包升级

# 升级 vsftpd 版本为 3.0.2-29.el7_9
[root@server ~ 16:50:48]# yum update vsftpd-3.0.2-29.el7_9

# 升级系统中所有软件为最新版本
[root@server ~ 16:54:11]# yum update
10.remove

作用:软件包卸载

# 卸载软件包
[root@server ~ 16:54:37]# yum remove -y vsftpd

(三)history 子命令

1.list

作用:查询yum事务历史记录

[root@server ~ 16:54:11]# yum history list
已加载插件:fastestmirror, langpacks
ID     | 命令行                   | 日期和时间       | 操作           | 变更数 
-------------------------------------------------------------------------------
    29 | remove -y vsftpd         | 2026-05-18 16:55 | Erase          |    1 PP
    28 | update                   | 2026-05-18 16:53 | I, U           |   64 **
......
history list

[root@server ~ 16:56:17]# yum install -y vsftpd
[root@server ~ 16:57:12]# yum downgrade -y vsftpd

# list 可以省略
[root@server ~ 16:57:25]# yum history 
已加载插件:fastestmirror, langpacks
ID     | 命令行                   | 日期和时间       | 操作           | 变更数 
-------------------------------------------------------------------------------
    31 | downgrade -y vsftpd      | 2026-05-18 16:57 | Downgrade      |    1   
    30 | install -y vsftpd        | 2026-05-18 16:57 | Install        |    1   
......
history list
2.info

作用:特定yum事务详情

[root@server ~ 16:58:09]# yum history info 2
已加载插件:fastestmirror, langpacks
事务 ID: 2
起始时间    : Wed May 13 14:22:49 2026
启动 RPM 数据库     : 312:499b8befefcbbb73843ba57c528196865f88451d
结束时间       :            14:23:00 2026 (11 秒)
结束 RPM 数据库      : 348:bc9b692c0e326e6ab21641fc36c579df62811fe9
用户           : root <root>
返回码    : 成功
命令行   : install -y bash-completion vim open-vm-tools lrzsz unzip rsync sshpass
事务完成属主:
    已安装 rpm-4.11.3-48.el7_9.x86_64                      @anaconda
    已安装 yum-3.4.3-168.el7.centos.noarch                 @anaconda
    已安装 yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch @anaconda
已变更的包:
    安装 bash-completion-1:2.1-8.el7.noarch         @base
    依赖安装 gpm-libs-1.20.7-6.el7.x86_64               @base
    安装 lrzsz-0.12.20-36.el7.x86_64                @base
    更新完毕 open-vm-tools-11.0.5-3.el7_9.3.x86_64      @anaconda
    更新               11.0.5-3.el7_9.9.x86_64      @updates
    依赖安装 perl-4:5.16.3-299.el7_9.x86_64             @updates
    依赖安装 perl-Carp-1.26-244.el7.noarch              @base
    依赖安装 perl-Encode-2.51-7.el7.x86_64              @base
......
3.undo

作用:反向操作第4个事务

[root@server ~ 16:58:59]# yum history undo 4
4.redo

作用:重做操作第4个事务

[root@server ~ 17:00:26]# yum history redo 4

(四)软件包组管理

# 查看仓库中软件包组清单
[root@server ~ 17:22:47]# yum grouplist
......
可用的环境分组:
   最小安装
   基础设施服务器
   ......
   带 GUI 的服务器
   GNOME 桌面
   KDE Plasma Workspaces
   开发及生成工作站
可用组:
   Cinnamon
   Fedora Packager
   Haskell
   ......
   通用桌面
完成

[root@server ~ 17:42:52]# yum grouplist -v
......
可用的环境分组:
   最小安装 (minimal)
   基础设施服务器 (infrastructure-server-environment)
   ......
   带 GUI 的服务器 (graphical-server-environment)
   GNOME 桌面 (gnome-desktop-environment)
   KDE Plasma Workspaces (kde-desktop-environment)
   开发及生成工作站 (developer-workstation-environment)
可用组:
   Cinnamon (cinnamon-desktop)
   Fedora Packager (fedora-packager)
   Haskell (haskell)
   ......
   通用桌面 (general-desktop)
完成

[root@server ~ 17:43:42]# unset LANG
[root@server ~ 17:44:14]# yum group list -lv
......
Available Environment Groups:
   Minimal Install (minimal)
   Compute Node (compute-node-environment)
   ......
   Server with GUI (graphical-server-environment)
   GNOME Desktop (gnome-desktop-environment)
   KDE Plasma Workspaces (kde-desktop-environment)
   Development and Creative Workstation (developer-workstation-environment)
Available Groups:
   Cinnamon (cinnamon-desktop)
   Compatibility Libraries (compat-libraries)
   Console Internet Tools (console-internet)
   ......
   Xfce (xfce-desktop)
Done

# 查看仓库中软件包组信息
[root@server ~ 17:44:23]# yum groupsinfo <tab><tab>
[root@server ~ 17:44:23]# yum groupinfo 'Server with GUI'

# 安装仓库中软件包组
[root@server ~ 17:46:19]# yum groupinstall 'Server with GUI'

# 卸载仓库中软件包组
[root@server ~ 17:46:45]# yum groupremove 'Server with GUI'

三、配置 yum 仓库

(一)yum 仓库配置

yum 工具的配置文件是 /etc/yum.conf,该文件包括两部分:
• 全局配置:main 块,提供每个仓库默认配置。
• 每个仓库的独立配置。

[root@server ~ 14:05:11]# cat /etc/yum.conf 
[main]
cachedir=/var/cache/yum/$basearch/$releasever
keepcache=0
debuglevel=2
logfile=/var/log/yum.log
exactarch=1
obsoletes=1
gpgcheck=1
plugins=1
installonly_limit=5
bugtracker_url=http://bugs.centos.org/set_project.php?project_id=23&ref=http://bugs.centos.org/bug_report_page.php?category=yum
distroverpkg=centos-release

#  This is the default, if you make this bigger yum won't see if the metadata
# is newer on the remote and so you'll "gain" the bandwidth of not having to
# download the new metadata and "pay" for it by yum not having correct
# information.
#  It is esp. important, to have correct metadata, for distributions like
# Fedora which don't keep old packages around. If you don't like this checking
# interupting your command line usage, it's much better to have something
# manually check the metadata once an hour (yum-updatesd will do this).
# metadata_expire=90m

# PUT YOUR REPOS HERE OR IN separate files named file.repo
# in /etc/yum.repos.d

使用如下命令查看完整的配置内容:

[root@server ~ 14:47:52]# yum install -y yum-utils

# 导出 yum 完整配置
[root@server ~ 14:48:15]# yum-config-manager 
Loaded plugins: fastestmirror, langpacks
==================================================== main ====================================================
[main]
alwaysprompt = True
assumeno = False
assumeyes = False
......
================================================= repo: base =================================================
[base]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/7
......

main 部分设置全局配置:
• enabled = 1,仓库默认启用。
• gpgcheck = 1,仓库默认启用gpg校验。
• keepcache = 0,不保存缓存。
• cachedir = /var/cache/yum,缓存文件保存的位置。
• reposdir = /etc/yum.repos.d, /etc/yum/repos.d,yum仓库配置文件。
yum管理的软件包存放在yum仓库,yum仓库配置文件存放在/etc/yum.repos.d目录。

[root@server ~ 14:48:34]# ls /etc/yum.repos.d/
CentOS-Base.repo  CentOS-Debuginfo.repo  CentOS-Sources.repo  CentOS-fasttrack.repo      epel.repo
CentOS-CR.repo    CentOS-Media.repo      CentOS-Vault.repo    CentOS-x86_64-kernel.repo
[root@server ~ 14:48:49]# ls /etc/yum/repos.d /etc/distro.repos.d
ls: cannot access /etc/yum/repos.d: No such file or directory
ls: cannot access /etc/distro.repos.d: No such file or directory

(二)yum 仓库管理

1.查看 yum 仓库
# 查看启用的yum仓库
[root@server ~ 14:49:39]# yum repolist
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                              repo name                                                          status
base/7/x86_64                        CentOS-7 - Base - mirrors.aliyun.com                               10072
epel/x86_64                          Extra Packages for Enterprise Linux 7 - x86_64                     13791
extras/7/x86_64                      CentOS-7 - Extras - mirrors.aliyun.com                               526
updates/7/x86_64                     CentOS-7 - Updates - mirrors.aliyun.com                             6173
repolist: 30562

# 查看 yum 所有仓库,包括未启用的仓库
[root@server ~ 14:49:53]# yum repolist all
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
repo id                               repo name                                                 status
C7.0.1406-base/x86_64                 CentOS-7.0.1406 - Base                                    disabled
......
2.启用和禁用仓库
# 启用
[root@server ~ 14:50:10]# yum-config-manager --enable epel-debuginfo

# 禁用
[root@server ~ 14:50:22]# yum-config-manager --disable epel-debuginfo

也可以编辑仓库配置文件,将仓库中enabled值设置为1启用,设置为0禁用。

# 查看禁用的仓库清单
[root@server ~ 14:50:53]# vim /etc/yum.repos.d/epel.repo 
[root@server ~ 14:51:32]# cat /etc/yum.repos.d/epel.repo 
[epel]
name=Extra Packages for Enterprise Linux 7 - $basearch
baseurl=http://mirrors.aliyun.com/epel/7/$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
 
[epel-debuginfo]
name=Extra Packages for Enterprise Linux 7 - $basearch - Debug
baseurl=http://mirrors.aliyun.com/epel/7/$basearch/debug
failovermethod=priority
# 将enabled设置为1启用,设置为0禁用
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
 
[epel-source]
name=Extra Packages for Enterprise Linux 7 - $basearch - Source
baseurl=http://mirrors.aliyun.com/epel/7/SRPMS
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-7
gpgcheck=0
3.仓库缓存管理

更新缓存后,yum install命令可补全软件包名称。

# 清理缓存数据
[root@server ~ 14:51:34]# yum clean <tab><tab>
all           cache         dbcache       expire-cache  headers       metadata      packages

# 清理所有缓存数据
[root@server ~ 14:51:34]# yum clean all

# 创建缓存数据
[root@server ~ 14:52:06]# yum makecache 
Loaded plugins: fastestmirror, langpacks
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
base                                                                                   | 3.6 kB  00:00:00     
epel                                                                                   | 4.3 kB  00:00:00     
epel-debuginfo                                                                         | 3.0 kB  00:00:00     
extras                                                                                 | 2.9 kB  00:00:00     
updates                                                                                | 2.9 kB  00:00:00     
(1/19): base/7/x86_64/group_gz                                                         | 153 kB  00:00:00     
(2/19): base/7/x86_64/primary_db                                                       | 6.1 MB  00:00:04     
(3/19): epel/x86_64/group                                                              | 399 kB  00:00:00     
(4/19): base/7/x86_64/filelists_db                                                     | 7.2 MB  00:00:06     
(5/19): epel/x86_64/updateinfo                                                         | 1.0 MB  00:00:00     
(6/19): base/7/x86_64/other_db                                                         | 2.6 MB  00:00:01     
(7/19): epel/x86_64/prestodelta                                                        |  592 B  00:00:00     
(8/19): epel/x86_64/primary_db                                                         | 8.7 MB  00:00:06     
(9/19): epel-debuginfo/x86_64/primary_db                                               | 1.1 MB  00:00:01     
(10/19): extras/7/x86_64/filelists_db                                                  | 305 kB  00:00:00     
(11/19): epel-debuginfo/x86_64/other_db                                                | 1.1 MB  00:00:01     
(12/19): extras/7/x86_64/primary_db                                                    | 253 kB  00:00:00     
(13/19): extras/7/x86_64/other_db                                                      | 154 kB  00:00:00     
(14/19): epel/x86_64/other_db                                                          | 4.1 MB  00:00:04     
(15/19): epel/x86_64/filelists_db                                                      |  15 MB  00:00:16     
(16/19): epel-debuginfo/x86_64/filelists_db                                            | 6.6 MB  00:00:08     
(17/19): updates/7/x86_64/filelists_db                                                 |  15 MB  00:00:12     
(18/19): updates/7/x86_64/other_db                                                     | 1.6 MB  00:00:01     
(19/19): updates/7/x86_64/primary_db                                                   |  27 MB  00:00:23     
Metadata Cache Created
4.添加 kubernetes 仓库
[root@server ~ 14:53:21]# yum-config-manager --add-repo=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/

[root@server ~ 15:07:41]# cat /etc/yum.repos.d/mirrors.aliyun.com_kubernetes_yum_repos_kubernetes-el7-x86_64_.repo 

[mirrors.aliyun.com_kubernetes_yum_repos_kubernetes-el7-x86_64_]
name=added from: https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
baseurl=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
enabled=1

# 安装 kubectl 包
[root@server ~ 14:53:36]# yum install kubectl --nogpgcheck
5.添加 docker-ce 仓库
[root@server ~ 14:54:26]# cat << 'EOF' > /etc/yum.repos.d/docker-ce.repo
> [docker-ce-stable]
> name=Docker CE Stable - $basearch
> baseurl=https://mirrors.aliyun.com/docker-ce/linux/centos/$releasever/$basearch/stable
> enabled=1
> gpgcheck=0
> EOF

# 查看docker版本
[root@server ~ 14:54:53]# yum list docker-ce
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: mirrors.aliyun.com
 * updates: mirrors.aliyun.com
docker-ce-stable                                                                       | 3.5 kB  00:00:00     
(1/2): docker-ce-stable/7/x86_64/updateinfo                                            |   55 B  00:00:00     
(2/2): docker-ce-stable/7/x86_64/primary_db                                            | 152 kB  00:00:00     
Available Packages
docker-ce.x86_64                                3:26.1.4-1.el7                                docker-ce-stable

(三)epel 仓库

EPEL(Extra Packages for Enterprise Linux),是由Fedora Special Interest Group维护的Enterprise Linux(RHEL、CentOS)中经常用到的包。

[root@server ~ 14:55:07]# curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
Logo

汇聚全球AI编程工具,助力开发者即刻编程。

更多推荐