文章目录
- Linux 软件包管理 | yum仓库管理
-
- 引言
- 一、初识软件包管理
- 二、深入解析 RPM 包
-
- 1. RPM 包文件名格式
- 2. RPM 包的组成与数字签名
- 3. RPM 包安装与更新的核心原则
- 三、`rpm` 命令实战应用
-
- 1. 查询
- 2. 验证
- 3. 安装、卸载与重新安装
- 4. 提取 RPM 包中的文件
- 四、Yum命令
-
- 1. Yum 常用子命令详解
- 2. Yum 事务历史管理
- 3. 软件包组管理
- 五、Yum 仓库配置与管理
-
- 1. 查看与启用/禁用仓库
- 2. 仓库缓存管理
- 3. 添加常用第三方仓库
- 4. EPEL仓库
Linux 软件包管理 | yum仓库管理
引言
在 Linux 系统中,安装、更新和卸载软件是运维人员每天都要面对的基础操作。与 Windows 系统中常见的“绿色软件”不同,Linux 软件包通常具有复杂的依赖关系。今天我们将围绕 RPM 包管理、Yum 自动化依赖管理以及 Yum 仓库配置 三大核心板块,带你彻底掌握 Linux 软件的安装与维护。
一、初识软件包管理
在 Windows 系统中,我们通常有两种软件获取方式:
而在 Linux 生态中,不同的发行版本采用了不同的包管理系统:
- RPM 系:以 RHEL、CentOS、Fedora、Rocky Linux 为代表,使用 .rpm 格式包。
- DEB 系:以 Debian、Ubuntu、Kali Linux 为代表,使用 .deb 格式包。
本篇博客将以 CentOS 7 为例,深入剖析 RPM 系的软件管理。
二、深入解析 RPM 包
1. 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,代表专为 CentOS 7 打包的第 36 次发布)。
- architecture:编译的处理器架构。x86_64 代表 64 位系统,noarch 代表不限定架构(通用于所有平台)。
2. RPM 包的组成与数字签名
每个 RPM 包主要由三部分组成:
- 需要安装的文件。
- 元数据信息:包含名称、版本、架构、依赖关系、许可证、变更日志等。
- 安装/卸载脚本:在安装、更新、卸载时执行的预处理或后处理脚本。
核心安全机制:数字签名(非对称加密) 软件提供商使用 GPG 私钥 对 RPM 包进行数字签名。用户在安装时,RPM 系统会使用对应的 GPG 公钥 进行验证。如果签名不匹配,系统将拒绝安装,从而确保软件包的完整性和来源可信。
3. RPM 包安装与更新的核心原则
- 默认安装最高版本:通常一个软件只能安装一个版本。
- 例外(Kernel):为了在新内核无法启动时保留旧内核作为保底,kernel 包允许同时安装多个版本。
- 升级即覆盖:升级 RPM 包会删除旧版本并安装新版本,但通常会自动保留原有的配置文件(如 /etc/ 下的配置)。
三、rpm 命令实战应用
虽然现代运维更常用 yum,但 rpm 命令在离线环境、排查系统文件变动时依然有不可替代的作用。
1. 查询
查询是 rpm 最核心的功能,语法为 rpm -q [选项]。
| rpm -qa | 查询系统中所有已安装的软件包。 |
| rpm -q httpd | 查询特定软件(httpd)是否安装。 |
| rpm -qi coreutils | 查询已安装软件包的元数据(版本、大小、简介、协议等)。 |
| rpm -ql openssh-server | 查询已安装软件包包含的所有文件列表。 |
| rpm -qc openssh-server | 查询已安装软件包提供的配置文件(通常在 /etc/ 下)。 |
| rpm -qd openssh-server | 查询已安装软件包提供的文档文件(如 man 手册)。 |
| rpm -qf /etc/ssh/sshd_config | 反向查询:某个文件属于哪个已安装的软件包。 |
| rpm -q –scripts openssh-server | 查询软件包安装/卸载时执行的脚本内容。 |
| rpm -q –changelog openssh-server | 查看软件包的变更日志(用于了解修复了哪些漏洞)。 |
示例:
# 查询系统中安装的软件包
[root@server-xianyu ~ 17:01:27]# rpm -qa
libqmi-utils-1.18.0-2.el7.x86_64
libmpcdec-1.2.6-12.el7.x86_64
gtkmm30-3.22.2-1.el7.x86_64
google-crosextra-caladea-fonts-1.002-0.4.20130214.el7.noarch
...
# 查询系统中指定软件包是否安装
[root@server-xianyu ~ 17:25:58]# rpm -q python
python-2.7.5-94.el7_9.x86_64
# 查询指定软件包元数据
[root@server-xianyu ~ 17:26:10]# rpm -q python -i
Name : python
Version : 2.7.5
Release : 94.el7_9
Architecture: x86_64
...
# 查询已安装软件包包含的所有文件列表
[root@server-xianyu ~ 17:27:38]# rpm -ql python
/usr/bin/pydoc
/usr/bin/python
/usr/bin/python2
/usr/bin/python2.7
/usr/libexec/platform-python
/usr/share/doc/python-2.7.5
/usr/share/doc/python-2.7.5/LICENSE
/usr/share/doc/python-2.7.5/README
/usr/share/man/man1/python.1.gz
/usr/share/man/man1/python2.1.gz
/usr/share/man/man1/python2.7.1.gz
# 查询已安装软件包提供的配置文件
[root@server-xianyu ~ 17:30:01]# rpm -q openssh-server -c
/etc/pam.d/sshd
/etc/ssh/sshd_config
/etc/sysconfig/sshd
# 查询已安装软件包提供的文档文件
[root@server-xianyu ~ 17:30:13]# 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-xianyu ~ 18:25:03]# rpm -q /usr/bin/python -f
python-2.7.5-94.el7_9.x86_64
# 查询软件包包含的脚本内容
root@server-xianyu ~ 18:26:59]# rpm -q openssh-server –scripts
preinstall scriptlet (using /bin/sh):
getent group sshd >/dev/null || groupadd -g 74 -r sshd || :
getent passwd sshd >/dev/null || \\
useradd -c "Privilege-separated SSH" -u 74 -g sshd \\
-s /sbin/nologin -r -d /var/empty/sshd sshd 2> /dev/null || :
postinstall scriptlet (using /bin/sh):
...
# 查看软件包的变更日志
[root@server-xianyu ~ 18:27:09]# rpm -q openssh-server –changelog
* Thu Jul 20 2023 Dmitry Belyavskiy <dbelyavs@redhat.com> – 7.4p1-23 + 0.10.3-2
– Avoid remote code execution in ssh-agent PKCS#11 support
Resolves: CVE-2023-38408
...
2. 验证
如果你的系统文件被意外修改(或怀疑被入侵),可以用 -V 验证。
- 实验:修改 /etc/ssh/sshd_config 文件,执行 rpm -V openssh-server。
[root@server-xianyu ~ 18:37:14]# sed -i 's/PermitRootLogin yes/PermitRootLogin no/g' /etc/ssh/sshd_config
[root@server-xianyu ~ 18:37:25]# rpm -V openssh-server
S.5....T. c /etc/ssh/sshd_config
- 输出解读:S.5….T. c /etc/ssh/sshd_config。其中 S 代表文件大小改变,5 代表 MD5 校验值改变,T 代表修改时间改变,c 代表这是一个配置文件。如果文件被删除了,会显示 遗漏。
3. 安装、卸载与重新安装
- 下载软件包
[root@server-xianyu ~ 18:38:35]# repoquery –location lrzsz
http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm
[root@server-xianyu ~ 18:44:37]# wget http://mirrors.aliyun.com/centos/7/os/x86_64/Packages/lrzsz-0.12.20-36.el7.x86_64.rpm
- 安装:rpm -ivh lrzsz-0.12.20-36.el7.x86_64.rpm(-i 安装,-v 显示进度,-h 显示井号进度条)。
[root@server-xianyu ~ 18:45:10]# rpm -ivh lrzsz-0.12.20-36.el7.x86_64.rpm
Preparing... ################################# [100%]
Updating / installing...
1:lrzsz-0.12.20-36.el7 ################################# [100%]
- 卸载:rpm -evh lrzsz(-e 卸载,-v 进度,-h 井号)。
[root@server-xianyu ~ 18:46:22]# rpm -evh lrzsz
Preparing... ################################# [100%]
Cleaning up / removing...
1:lrzsz-0.12.20-36.el7 ################################# [100%]
- 重新安装:rpm –reinstall -vh lrzsz*(常用于恢复丢失的配置文件)。
[root@server-xianyu ~ 18:46:29]# rpm –reinstall -vh lrzsz*
warning: package lrzsz-0.12.20-36.el7.x86_64 was already added, skipping lrzsz-0.12.20-36.el7.x86_64
Preparing... ################################# [100%]
Updating / installing...
1:lrzsz-0.12.20-36.el7 ################################# [100%]
⚠️ RPM 的致命弱点:依赖问题 当你尝试用 rpm -ivh 安装 httpd 时,可能会遇到 错误:依赖检测失败: libapr-1.so.0()(64bit) 被 httpd 需要。这是因为 RPM 不会自动解决依赖问题,你必须手动下载并安装所有依赖包,这通常是个噩梦。正是为了解决这个问题,Yum 应运而生。
4. 提取 RPM 包中的文件
如果你不需要安装整个软件,只想从 RPM 包里提取某个特定文件,可以这样做:
- 分步法:rpm2cpio httpd.rpm > httpd.cpio 转为 cpio 格式,再用 cpio -id ./etc/httpd/conf/httpd.conf < httpd.cpio 提取特定文件。
- 一步法:rpm2cpio httpd.rpm | cpio -id '*' 直接解压所有文件。
四、Yum命令
yum(Yellowdog Updater Modified)旨在克服 RPM 无法处理依赖和存储库的缺陷。它能够自动从配置的 Yum 仓库中下载并安装所有必需的依赖包。
1. Yum 常用子命令详解
- yum help:查看 yum 命令的帮助文档。
[root@server-xianyu ~ 18:47:42]# yum help
Loaded plugins: fastestmirror, langpacks
Usage: yum [options] COMMAND
List of Commands:
check Check for problems in the rpmdb
check-update Check for available package updates
clean Remove cached data
deplist List a package's dependencies
distribution-synchronization Synchronize installed packages to the lates
...
- yum info httpd:查看仓库中软件包的详细元数据信息(包括是否已安装)。
[root@server-xianyu ~ 18:50:50]# yum info httpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
Installed Packages
Name : httpd
Arch : x86_64
Version : 2.4.6
Release : 99.el7.centos.1
Size : 9.4 M
Repo : installed
From repo : updates
Summary : Apache HTTP Server
URL : http://httpd.apache.org/
License : ASL 2.0
Description : The Apache HTTP Server is a powerful, efficient, and extensible
: web server.
- yum list 'http\\*':查看仓库中所有以 http 开头的软件包清单。
[root@server-xianyu ~ 18:50:57]# yum list 'httpd*'
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
Installed Packages
httpd.x86_64 2.4.6-99.el7.centos.1 @updates
httpd-tools.x86_64 2.4.6-99.el7.centos.1 @updates
Available Packages
httpd-devel.x86_64 2.4.6-99.el7.centos.1 updates
httpd-itk.x86_64 2.4.7.04-2.el7 epel
httpd-manual.noarch 2.4.6-99.el7.centos.1 updates
- yum list httpd –showduplicates:查看仓库中该软件包的所有历史版本。
[root@server-xianyu ~ 18:51:43]yum list httpd –showduplicates
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
Installed Packages
httpd.x86_64 2.4.6-99.el7.centos.1 @updates
Available Packages
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
- yum search 'web server':根据关键字(简介或描述)搜索软件包。
[root@server-xianyu ~ 18:54:01]# yum search 'web server'
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
==================================== N/S matched: web server =====================================
...
# yum search all 'web server' 可查看web server全部简介与描述
- yum provides sar:反向查找。当你只知道命令(如 sar),却不知道它属于哪个包时,这个命令能告诉你 sar 包含在 sysstat 包里。
[root@server-xianyu ~ 18:57:46]# yum provides sar
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
base/7/x86_64/filelists_db | 7.2 MB 00:00:08
...
- yum install vsftpd:自动从仓库安装 vsftpd 及其依赖。
[root@server-xianyu ~ 19:01:49]# yum install vsftpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
...
- yum install vsftpd –downloadonly –downloaddir=/root/:只下载不安装。将主包和依赖包一并下载到指定目录,适合内网离线部署。
[root@server-xianyu ~ 19:02:43]# yum install vsftpd –downloadonly –downloaddir=/root/
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
...
- yum localinstall ./vsftpd-3.0.2-29.el7_9.x86_64.rpm:安装本地文件系统中的 RPM 包,并利用 Yum 仓库解决其依赖关系。
[root@server-xianyu ~ 19:06:05]# yum localinstall ./vsftpd-3.0.2-29.el7_9.x86_64.rpm
Loaded plugins: fastestmirror, langpacks
Examining ./vsftpd-3.0.2-29.el7_9.x86_64.rpm: vsftpd-3.0.2-29.el7_9.x86_64
Marking ./vsftpd-3.0.2-29.el7_9.x86_64.rpm as an update to vsftpd-3.0.2-28.el7.x86_64
Resolving Dependencies
—> Running transaction check
—> Package vsftpd.x86_64 0:3.0.2-28.el7 will be updated
—> Package vsftpd.x86_64 0:3.0.2-29.el7_9 will be an update
—> Finished Dependency Resolution
...
- yum downgrade -y vsftpd-3.0.2-28.el7:将软件降级到指定旧版本(需要注意依赖包也需同时降级)。
[root@server-xianyu ~ 19:08:09]# yum downgrade -y vsftpd
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
* base: mirrors.aliyuncs.com
* extras: mirrors.aliyuncs.com
* updates: mirrors.aliyuncs.com
...
- yum update:升级系统中所有软件至最新版本(生产环境需谨慎)。
- yum remove -y vsftpd:卸载软件包及其依赖。
[root@server-xianyu ~ 19:08:20]# yum remove -y vsftpd
Loaded plugins: fastestmirror, langpacks
Resolving Dependencies
—> Running transaction check
—> Package vsftpd.x86_64 0:3.0.2-28.el7 will be erased
—> Finished Dependency Resolution
...
2. Yum 事务历史管理
yum 记录了你所有的安装、升级、卸载操作。如果你执行了误操作,可以通过历史记录回滚。
- yum history:列出所有事务 ID(ID、用户、时间、操作类型、影响的包数)。
- yum history info 2:查看 ID 为 2 的事务的详细安装列表和依赖处理过程。
- yum history undo 4:撤销第 4 个事务的操作(比如你刚才安装了 vsftpd,撤销会帮你卸载它)。
- yum history redo 4:重做第 4 个事务的操作。
3. 软件包组管理
在部署基础环境(如“最小安装”、“带 GUI 的服务器”)时,一用一个包组极其方便。
- yum grouplist:查看可用和已安装的软件包组。
- yum grouplist -v:详细模式,会显示分组的中文名和内部的英文 ID(如 graphical-server-environment)。后续操作强烈建议使用英文 ID,以免编码冲突。
- yum groupinfo 'Server with GUI':查看特定包组包含的具体软件包清单。
- yum groupinstall 'Server with GUI':安装整个包组。
- yum groupremove 'Server with GUI':卸载整个包组。
五、Yum 仓库配置与管理
Yum 仓库(Repository)是存放 RPM 包和元数据的“软件超市”。配置文件存放在 /etc/yum.repos.d/ 目录下(例如 CentOS-Base.repo)。
1. 查看与启用/禁用仓库
- yum repolist:查看当前已启用的仓库及软件包数量。
- yum repolist all:查看所有仓库(包含已禁用的)。
- 启用/禁用:可以通过 yum-config-manager –enable 仓库名 或 –disable 仓库名 操作,也可以直接编辑 .repo 文件,修改 enabled=1(启用)或 enabled=0(禁用)。
2. 仓库缓存管理
- yum clean all:清理本地下载的缓存元数据,释放空间。
- yum makecache:重新拉取并建立元数据缓存。注意:执行完此命令后,yum install 时按 Tab 键就能自动补全软件包名了。
3. 添加常用第三方仓库
很多开源软件默认不在 CentOS 的官方源中,需要通过添加第三方仓库来安装。
添加 Kubernetes 仓库(阿里云源):
yum-config-manager –add-repo=https://mirrors.aliyun.com/kubernetes/yum/repos/kubernetes-el7-x86_64/
# 安装 kubectl(因为阿里 K8s 源无 GPG,需跳过校验或手动导入)
yum install kubectl –nogpgcheck
添加 Docker CE 仓库(阿里云源):
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
yum install docker-ce
4. EPEL仓库
EPEL(Extra Packages for Enterprise Linux)是由 Fedora 社区维护的、为 RHEL/CentOS 提供额外高质量软件包的扩展仓库。很多非核心软件(如 nginx、stress)都在 EPEL 里。 配置方式(极简):
curl -s -o /etc/yum.repos.d/epel.repo http://mirrors.aliyun.com/repo/epel-7.repo
网硕互联帮助中心




评论前必须登录!
注册