本文档风哥主要介绍RPM包基础命令的使用,包括RPM包的概念、RPM包结构、RPM与DNF的区别、RPM包基础操作详解、RPM包高级使用技巧、RPM包管理自动化、RPM包安装实战案例、RPM包查询实战案例、RPM包管理故障排查与解决等内容,参考Red Hat Enterprise Linux 10官方文档,适合Linux运维人员在学习和测试中使用,如果要应用于生产环境则需要自行确认。
Part01-基础概念与理论知识
1.1 RPM包的概念
RPM(Red Hat Package Manager)是Red Hat Linux系统中的软件包管理器,用于安装、卸载、查询、验证和管理软件包。RPM包是一种二进制软件包格式,包含软件文件、元数据和安装脚本。RPM包管理器可以自动处理软件包的依赖关系,确保软件包的正确安装和卸载。更多视频教程www.fgedu.net.cn
- 二进制软件包格式
- 包含软件文件、元数据和安装脚本
- 自动处理依赖关系
- 支持软件包验证
- 支持软件包升级
1.2 RPM包结构
RPM包结构:
- 头部信息:包含软件包名称、版本、发布号、架构等信息
- 文件列表:包含软件包中的所有文件
- 脚本:包含安装前、安装后、卸载前、卸载后脚本
- 依赖关系:包含软件包的依赖关系
- 文件内容:包含软件包的实际文件
1.3 RPM与DNF的区别
RPM与DNF的区别:
- RPM:底层软件包管理器,直接操作RPM包
- DNF:高级软件包管理器,基于RPM,自动处理依赖关系
- 依赖处理:RPM不自动处理依赖关系,DNF自动处理依赖关系
- 存储库支持:RPM不支持存储库,DNF支持存储库
- 使用场景:RPM适合本地软件包管理,DNF适合网络软件包管理
Part02-生产环境规划与建议
2.1 RPM包管理在生产环境的规划
RPM包管理在生产环境的规划要点:
– 使用DNF进行软件包管理
– 使用RPM进行本地软件包管理
– 定期检查软件包状态
– 验证软件包完整性
– 备份软件包配置
# RPM包管理注意事项
– 确保软件包来源可靠
– 检查软件包依赖关系
– 验证软件包完整性
– 备份重要配置文件
– 测试软件包安装
2.2 RPM包管理最佳实践
RPM包管理最佳实践:
- 使用DNF:使用DNF进行软件包管理
- 本地管理:使用RPM进行本地软件包管理
- 检查依赖:检查软件包依赖关系
- 验证完整性:验证软件包完整性
- 备份配置:备份重要配置文件
2.3 RPM包管理安全配置建议
RPM包管理安全配置建议:
- 来源可靠:确保软件包来源可靠
- GPG验证:验证软件包GPG签名
- 完整性验证:验证软件包完整性
- 权限控制:使用最小权限
- 备份恢复:备份重要配置文件
Part03-生产环境项目实施方案
3.1 RPM包基础操作详解
3.1.1 安装RPM包
# rpm -ivh nginx-1.22.1-2.el9.x86_64.rpm
warning: nginx-1.22.1-2.el9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:nginx-1:1.22.1-2.el9 ################################# [100%]
# 验证RPM包已安装
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.x86_64
3.1.2 升级RPM包
# rpm -Uvh nginx-1.22.2-1.el9.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:nginx-1:1.22.2-1.el9 ################################# [100%]
# 验证RPM包已升级
# rpm -qa | grep nginx
nginx-1.22.2-1.el9.x86_64
3.1.3 卸载RPM包
# rpm -e nginx
# 验证RPM包已卸载
# rpm -qa | grep nginx
(无输出)
3.1.4 查询RPM包
# rpm -qa
bash-5.1.8-6.el9_2.x86_64
coreutils-8.32-34.el9_2.x86_64
nginx-1.22.2-1.el9.x86_64
# 查询特定RPM包
# rpm -q nginx
nginx-1.22.2-1.el9.x86_64
# 查询RPM包详细信息
# rpm -qi nginx
Name : nginx
Version : 1.22.2
Release : 1.el9
Architecture: x86_64
Install Date: Thu Mar 31 10:00:00 2026
Group : System Environment/Daemons
Size : 1945678
License : BSD
Signature : RSA/SHA256, Thu Mar 31 09:00:00 2026, Key ID 199e2f91fd431d51
Source RPM : nginx-1.22.2-1.el9.src.rpm
Build Date : Thu Mar 31 08:00:00 2026
Build Host : x86-01-vm.build.eng.bos.redhat.com
Relocations : (not relocatable)
Packager : Red Hat, Inc.
Vendor : Red Hat, Inc.
URL : http://nginx.org/
Summary : A high performance web server and reverse proxy server
Description :
Nginx is a web server and a reverse proxy server for HTTP, HTTPS,
SMTP, POP3 and IMAP protocols, with a strong focus on high concurrency,
performance and low memory usage.
# 查询RPM包文件列表
# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/.build-id
/usr/lib/.build-id/01
/usr/lib/.build-id/01/01234567890abcdef1234567890abcdef123456
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/doc/nginx/CHANGES
/usr/share/doc/nginx/CHANGES.ru
/usr/share/doc/nginx/LICENSE
/usr/share/doc/nginx/README
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/lib/nginx
/var/log/nginx
# 查询文件属于哪个RPM包
# rpm -qf /etc/nginx/nginx.conf
nginx-1.22.2-1.el9.x86_64
3.1.5 验证RPM包
# rpm -V nginx
S.5….T. c /etc/nginx/nginx.conf
# 验证所有已安装的RPM包
# rpm -Va
S.5….T. c /etc/nginx/nginx.conf
…….T. /var/log/nginx/access.log
…….T. /var/log/nginx/error.log
3.2 RPM包高级使用技巧
3.2.1 强制安装RPM包
# rpm -ivh –nodeps nginx-1.22.1-2.el9.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:nginx-1:1.22.1-2.el9 ################################# [100%]
# 强制升级RPM包(忽略依赖关系)
# rpm -Uvh –nodeps nginx-1.22.2-1.el9.x86_64.rpm
Preparing… ################################# [100%]
Updating / installing…
1:nginx-1:1.22.2-1.el9 ################################# [100%]
3.2.2 查询RPM包依赖关系
# rpm -qR nginx
/bin/sh
/bin/sh
config(nginx) = 1.22.2-1.el9
libc.so.6()(64bit)
libc.so.6(GLIBC_2.14)(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.28)(64bit)
libc.so.6(GLIBC_2.3.4)(64bit)
libc.so.6(GLIBC_2.4)(64bit)
libcrypto.so.1.1()(64bit)
libcrypto.so.1.1(OPENSSL_1_1_0)(64bit)
libcrypto.so.1.1(OPENSSL_1_1_1)(64bit)
libpcre2-8.so.0()(64bit)
libssl.so.1.1()(64bit)
libssl.so.1.1(OPENSSL_1_1_0)(64bit)
libssl.so.1.1(OPENSSL_1_1_1)(64bit)
libz.so.1()(64bit)
libz.so.1(ZLIB_1.2.0)(64bit)
nginx-core(x86-64) = 1.22.2-1.el9
nginx-filesystem = 1.22.2-1.el9
rpmlib(CompressedFileNames) <= 3.0.4-1 rpmlib(FileDigests) <= 4.6.0-1 rpmlib(PayloadFilesHavePrefix) <= 4.0-1 rtld(GNU_HASH) rpmlib(PayloadIsXz) <= 5.2-1
3.2.3 查询RPM包脚本
# rpm -q –scripts nginx
postinstall scriptlet (using /bin/sh):
if [ $1 -eq 1 ]; then
# Initial installation
/usr/bin/systemd-tmpfiles –create nginx.conf || :
fi
preuninstall scriptlet (using /bin/sh):
if [ $1 -eq 0 ]; then
# Package removal, not upgrade
/usr/bin/systemctl –no-reload disable nginx.service > /dev/null 2>&1 || :
/usr/bin/systemctl stop nginx.service > /dev/null 2>&1 || :
fi
postuninstall scriptlet (using /bin/sh):
/usr/bin/systemctl daemon-reload >/dev/null 2>&1 || :
if [ $1 -ge 1 ]; then
# Package upgrade, not uninstall
/usr/bin/systemctl try-restart nginx.service >/dev/null 2>&1 || :
fi
3.3 RPM包管理自动化
3.3.1 批量安装RPM包
# cat > /usr/local/bin/batch-install.sh << 'EOF' #!/bin/bash # 批量安装RPM包脚本 RPM_DIR="/root/rpms" # 检查RPM目录是否存在 if [ ! -d "$RPM_DIR" ]; then echo "Error: RPM directory not found: $RPM_DIR" exit 1 fi # 批量安装RPM包 for rpm_file in $RPM_DIR/*.rpm; do if [ -f "$rpm_file" ]; then echo "Installing: $rpm_file" rpm -ivh "$rpm_file" fi done echo "Batch installation completed" EOF # 设置脚本执行权限 # chmod +x /usr/local/bin/batch-install.sh # 测试脚本 # /usr/local/bin/batch-install.sh Installing: /root/rpms/nginx-1.22.1-2.el9.x86_64.rpm warning: nginx-1.22.1-2.el9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:nginx-1:1.22.1-2.el9 ################################# [100%] Installing: /root/rpms/httpd-2.4.53-7.el9.x86_64.rpm warning: httpd-2.4.53-7.el9.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID fd431d51: NOKEY Preparing... ################################# [100%] Updating / installing... 1:httpd-2.4.53-7.el9 ################################# [100%] Batch installation completed
Part04-生产案例与实战讲解
4.1 RPM包安装实战案例
4.1.1 完整安装流程
# wget https://nginx.org/packages/rhel/9/x86_64/RPMS/nginx-1.22.1-2.el9.ngx.x86_64.rpm
–2026-03-31 10:00:00– https://nginx.org/packages/rhel/9/x86_64/RPMS/nginx-1.22.1-2.el9.ngx.x86_64.rpm
Resolving nginx.org (nginx.org)… 52.58.199.22
Connecting to nginx.org (nginx.org)|52.58.199.22|:443… connected.
HTTP request sent, awaiting response… 200 OK
Length: 823456 (804K) [application/x-rpm]
Saving to: ‘nginx-1.22.1-2.el9.ngx.x86_64.rpm’
nginx-1.22.1-2.el9.ngx.x86_64.rpm 100%[===================>] 804K 1.2MB/s in 0.7s
2026-03-31 10:00:00 (1.2 MB/s) – ‘nginx-1.22.1-2.el9.ngx.x86_64.rpm’ saved [823456/823456]
# 2. 验证RPM包完整性
# sha256sum nginx-1.22.1-2.el9.ngx.x86_64.rpm
a1b2c3d4e5f6g7h8i9j0k1l2m3n4o5p6q7r8s9t0u1v2w3x4y5z6 nginx-1.22.1-2.el9.ngx.x86_64.rpm
# 3. 查询RPM包信息
# rpm -qip nginx-1.22.1-2.el9.ngx.x86_64.rpm
Name : nginx
Version : 1.22.1
Release : 2.el9.ngx
Architecture: x86_64
Install Date: (not installed)
Group : System Environment/Daemons
Size : 1945678
License : BSD
Signature : (none)
Source RPM : nginx-1.22.1-2.el9.ngx.src.rpm
Build Date : Thu Mar 31 08:00:00 2026
Build Host : ip-10-0-1-234.ec2.internal
Relocations : (not relocatable)
Vendor : Nginx, Inc.
URL : http://nginx.org/
Summary : High performance web server
Description :
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server.
# 4. 查询RPM包依赖关系
# rpm -qpR nginx-1.22.1-2.el9.ngx.x86_64.rpm
/bin/sh
/bin/sh
config(nginx) = 1.22.1-2.el9.ngx
libc.so.6()(64bit)
libc.so.6(GLIBC_2.14)(64bit)
libc.so.6(GLIBC_2.2.5)(64bit)
libc.so.6(GLIBC_2.28)(64bit)
libc.so.6(GLIBC_2.3.4)(64bit)
libc.so.6(GLIBC_2.4)(64bit)
libcrypto.so.1.1()(64bit)
libcrypto.so.1.1(OPENSSL_1_1_0)(64bit)
libcrypto.so.1.1(OPENSSL_1_1_1)(64bit)
libpcre2-8.so.0()(64bit)
libssl.so.1.1()(64bit)
libssl.so.1.1(OPENSSL_1_1_0)(64bit)
libssl.so.1.1(OPENSSL_1_1_1)(64bit)
libz.so.1()(64bit)
libz.so.1(ZLIB_1.2.0)(64bit)
rpmlib(CompressedFileNames) <= 3.0.4-1
rpmlib(FileDigests) <= 4.6.0-1
rpmlib(PayloadFilesHavePrefix) <= 4.0-1
rtld(GNU_HASH)
rpmlib(PayloadIsXz) <= 5.2-1
# 5. 安装RPM包
# rpm -ivh nginx-1.22.1-2.el9.ngx.x86_64.rpm
warning: nginx-1.22.1-2.el9.ngx.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
Preparing... ################################# [100%]
Updating / installing...
1:nginx-1:1.22.1-2.el9.ngx ################################# [100%]
# 6. 验证RPM包已安装
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.ngx.x86_64
# 7. 启动nginx服务
# systemctl start nginx
# systemctl status nginx
● nginx.service - nginx - high performance web server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Thu 2026-03-31 10:00:00 CST; 5s ago
Docs: http://nginx.org/en/docs/
Process: 1234 ExecStart=/usr/sbin/nginx (code=exited, status=0/SUCCESS)
Main PID: 1235 (nginx)
Tasks: 2 (limit: 4915)
Memory: 2.4M
CGroup: /system.slice/nginx.service
├─1235 nginx: master process /usr/sbin/nginx
└─1236 nginx: worker process
Mar 31 10:00:00 localhost systemd[1]: Starting nginx - high performance web server...
Mar 31 10:00:00 localhost nginx[1234]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Mar 31 10:00:00 localhost systemd[1]: Started nginx - high performance web server.
4.2 RPM包查询实战案例
4.2.1 查询已安装的RPM包
# rpm -qa | head -20
bash-5.1.8-6.el9_2.x86_64
coreutils-8.32-34.el9_2.x86_64
nginx-1.22.1-2.el9.ngx.x86_64
httpd-2.4.53-7.el9.x86_64
systemd-252-14.el9_2.5.x86_64
# 2. 查询特定RPM包
# rpm -q nginx
nginx-1.22.1-2.el9.ngx.x86_64
# 3. 查询RPM包详细信息
# rpm -qi nginx
Name : nginx
Version : 1.22.1
Release : 2.el9.ngx
Architecture: x86_64
Install Date: Thu Mar 31 10:00:00 2026
Group : System Environment/Daemons
Size : 1945678
License : BSD
Signature : RSA/SHA256, Thu Mar 31 09:00:00 2026, Key ID 7bd9bf62
Source RPM : nginx-1.22.1-2.el9.ngx.src.rpm
Build Date : Thu Mar 31 08:00:00 2026
Build Host : ip-10-0-1-234.ec2.internal
Relocations : (not relocatable)
Packager : Nginx, Inc.
Vendor : Nginx, Inc.
URL : http://nginx.org/
Summary : High performance web server
Description :
nginx [engine x] is an HTTP and reverse proxy server, as well as
a mail proxy server.
# 4. 查询RPM包文件列表
# rpm -ql nginx
/etc/logrotate.d/nginx
/etc/nginx/fastcgi.conf
/etc/nginx/fastcgi.conf.default
/etc/nginx/fastcgi_params
/etc/nginx/fastcgi_params.default
/etc/nginx/koi-utf
/etc/nginx/koi-win
/etc/nginx/mime.types
/etc/nginx/mime.types.default
/etc/nginx/nginx.conf
/etc/nginx/nginx.conf.default
/etc/nginx/scgi_params
/etc/nginx/scgi_params.default
/etc/nginx/uwsgi_params
/etc/nginx/uwsgi_params.default
/etc/nginx/win-utf
/usr/bin/nginx-upgrade
/usr/lib/.build-id
/usr/lib/.build-id/01
/usr/lib/.build-id/01/01234567890abcdef1234567890abcdef123456
/usr/lib/systemd/system/nginx.service
/usr/lib64/nginx
/usr/lib64/nginx/modules
/usr/sbin/nginx
/usr/share/doc/nginx
/usr/share/doc/nginx/CHANGES
/usr/share/doc/nginx/CHANGES.ru
/usr/share/doc/nginx/LICENSE
/usr/share/doc/nginx/README
/usr/share/man/man8/nginx.8.gz
/usr/share/nginx/html/50x.html
/usr/share/nginx/html/index.html
/var/lib/nginx
/var/log/nginx
# 5. 查询文件属于哪个RPM包
# rpm -qf /etc/nginx/nginx.conf
nginx-1.22.1-2.el9.ngx.x86_64
4.3 RPM包管理故障排查与解决
4.3.1 依赖关系错误
# 分析步骤:
# 1. 查看错误信息
# rpm -ivh nginx-1.22.1-2.el9.ngx.x86_64.rpm
error: Failed dependencies:
libpcre2-8.so.0()(64bit) is needed by nginx-1.22.1-2.el9.ngx.x86_64
libssl.so.1.1()(64bit) is needed by nginx-1.22.1-2.el9.ngx.x86_64
# 2. 查询依赖包是否已安装
# rpm -qa | grep pcre2
pcre2-10.40-2.el9.x86_64
pcre2-devel-10.40-2.el9.x86_64
# rpm -qa | grep openssl
openssl-1.1.1k-7.el9_2.x86_64
openssl-libs-1.1.1k-7.el9_2.x86_64
# 3. 查询依赖包详细信息
# rpm -ql pcre2
/usr/lib64/libpcre2-8.so.0
/usr/lib64/libpcre2-8.so.0.10.40
/usr/lib64/libpcre2-16.so.0
/usr/lib64/libpcre2-16.so.0.10.40
/usr/lib64/libpcre2-32.so.0
/usr/lib64/libpcre2-32.so.0.10.40
/usr/share/doc/pcre2
/usr/share/doc/pcre2/AUTHORS
/usr/share/doc/pcre2/COPYING
/usr/share/doc/pcre2/ChangeLog
/usr/share/doc/pcre2/INSTALL
/usr/share/doc/pcre2/LICENCE
/usr/share/doc/pcre2/NEWS
/usr/share/doc/pcre2/README
/usr/share/doc/pcre2/Release
/usr/share/doc/pcre2/Release.txt
/usr/share/doc/pcre2/Syntax.txt
# 4. 使用DNF安装依赖包
# dnf install -y pcre2 openssl-libs
Updating Subscription Management repositories.
Last metadata expiration check: 0:00:01 ago on Thu Mar 31 10:00:00 2026.
Dependencies resolved.
================================================================================
Package Architecture Version Repository Size
================================================================================
Installing:
pcre2 x86_64 10.40-2.el9 baseos 234 k
openssl-libs x86_64 1:1.1.1k-7.el9_2 baseos 1.2 M
Transaction Summary
================================================================================
Install 2 Packages
Total download size: 1.4 M
Installed size: 4.2 M
Downloading Packages:
(1/2): pcre2-10.40-2.el9.x86_64.rpm 234 kB/s | 234 kB 00:00
(2/2): openssl-libs-1.1.1k-7.el9_2.x86_64.rpm 1.2 MB/s | 1.2 MB 00:00
——————————————————————————–
Total 1.4 MB/s | 1.4 MB 00:00
Running transaction check
Transaction test succeeded
Running transaction
Preparing : 1/1
Installing : pcre2-10.40-2.el9.x86_64 1/2
Installing : openssl-libs-1.1.1k-7.el9_2.x86_64 2/2
Running scriptlet: openssl-libs-1.1.1k-7.el9_2.x86_64 2/2
Verifying : pcre2-10.40-2.el9.x86_64 1/2
Verifying : openssl-libs-1.1.1k-7.el9_2.x86_64 2/2
Installed:
pcre2-10.40-2.el9.x86_64
openssl-libs-1.1.1k-7.el9_2.x86_64
Complete!
# 5. 重新安装RPM包
# rpm -ivh nginx-1.22.1-2.el9.ngx.x86_64.rpm
warning: nginx-1.22.1-2.el9.ngx.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID 7bd9bf62: NOKEY
Preparing… ################################# [100%]
Updating / installing…
1:nginx-1:1.22.1-2.el9.ngx ################################# [100%]
# 6. 验证RPM包已安装
# rpm -qa | grep nginx
nginx-1.22.1-2.el9.ngx.x86_64
# 7. 预防措施
# – 使用DNF进行软件包管理
# – 检查依赖关系
# – 安装依赖包
# – 验证软件包安装
Part05-风哥经验总结与分享
5.1 RPM包管理经验总结
RPM包管理经验总结:
- 使用DNF:使用DNF进行软件包管理
- 本地管理:使用RPM进行本地软件包管理
- 检查依赖:检查软件包依赖关系
- 验证完整性:验证软件包完整性
- 备份配置:备份重要配置文件
5.2 RPM包管理检查清单
RPM包管理检查清单:
- 安装前:验证RPM包完整性、检查依赖关系
- 安装时:使用正确的安装选项
- 安装后:验证RPM包已安装
- 查询时:使用正确的查询选项
- 卸载时:确认卸载RPM包
- 故障排查:检查依赖关系、重新安装
5.3 RPM包管理相关工具推荐
RPM包管理相关工具推荐:
- rpm:RPM包管理器
- dnf:DNF软件包管理器
- repoquery:软件包查询工具
- rpmbuild:RPM包构建工具
- rpmdevtools:RPM开发工具集
本文由风哥教程整理发布,仅用于学习测试使用,转载注明出处:http://www.fgedu.net.cn/10327.html
