RPM包管理

RPM包概述

什么是rpm包

由红帽开发的软件 升级、安装、卸载、查询 工具

为什么要使用rpm

就像在windows系统中一样,如果你想要安装一个QQ,安装一个微信,安装一款游戏,首先要去该软件的官网上去
下载相关的软件包,通常都是.exe的安装包。还有那种绿色软件包,一下载下来,解压开就能使用无需安装。
那么Linux中也有很多种类的安装包,我们在Linux上也需要安装软件,软件包的种类也有很多,但是最方便,最简
单,最简易,速度最快的,也就是rpm包了。

rpm包命名规则

#rpm的格式
tree-1.6.0-10.el7.x86_64.rpm
tree # 软件名称
1.6.0 #版本号
10 #发布次数
el7 #使用系统版本(CentOS7.RedHalt7)向下兼
容
x86_64 #系统平台 64位 32位
rpm #包后缀名

rpm和yum的区别

#rpm:
rpm命令一旦遇到了依赖性的问题不会去解决依赖性,而是直接反馈给用户,让用户自行解决。
不能更新系统
不用依赖yum源
#yum:
yum命令安装时碰到了依赖性的问题会去主动尝试解决依赖性如果解决不了才会反馈给用户。
能更新系统
依赖于yum源

Linux中软件的安装方式

rpm安装:已经编译好的源码包,下载下来后,直接使用rpm命令安装即可        版本会低于官方版本
源码安装:先编译,将源代码翻译成系统认识的软件,再安装                 步骤复杂繁琐
二进制安装:绿色软件(免安装)                                       不是所有程序都有二进制包

安装方式选择

1.如果软件有二进制包,首要选择就是二进制安装方式
2.公司没有规定安装路径,选择yum安装
3.公司规定安装路径,源码安装,为了方便,建议:将源码,打成rpm包

## 现在大部分公司,能用yum就yum

RPM包获取途径

  • 系统光盘镜像/CentOS官网:CentOS Mirror
  • 其他各大镜像站(yum源)
  • rpm包搜索官方网站
  • 对应软件的官方网站

img

# 挂载语法
mount 源设备文件 目标路径

[root@web ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only  # 挂载成功

# 查看磁盘信息
df -h 查看磁盘分区情况,磁盘使用情况,也能查看到挂载设备及目录

[root@web ~]# mount /dev/cdrom /mnt
/dev/sr0        4.4G  4.4G     0 100% /mnt

# 卸载语法
umount 目标路径
[root@web ~]# mount /dev/cdrom /mnt
mount: /dev/sr0 is write-protected, mounting read-only
mount: /dev/sr0 is already mounted or /mnt busy
       /dev/sr0 is already mounted on /mnt
  • RPM包查询官方网站[TP](Rpmfind mirror)
  • 其他软件官方网站

rpm命令使用-安装

# 语法:
rpm [选项] rpm包名

rpm包名:
    - 本地包名:在系统中某个目录下的rpm包
    - 远程包名:不用下载,直接使用网站上的rpm包

# 选项:
-i:安装
[root@web ~]# yum remove tree -y # 删除tree程序包
[root@web ~]# rpm -i /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm

-v:显示安装的过程
[root@web ~]# yum remove tree -y
[root@web ~]# rpm -iv /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing packages...
tree-1.6.0-10.el7.x86_64

-h:显示安装的进度条
[root@web ~]# yum remove tree -y
[root@web ~]# rpm -ivh /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]

--test:仅测试,不安装
[root@web ~]# yum remove tree -y
[root@web ~]# rpm -ivh --test /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
[root@web ~]# tree
-bash: tree: command not found  # 测试安装成功 但没真正的安装

--force:强制安装
--nodeps:忽略依赖关系
[root@web ~]# rpm -ivh --force --nodeps /mnt/Packages/tree-1.6.0-10.el7.x86_64.rpm 
Preparing...                          ################################# [100%]
Updating / installing...
   1:tree-1.6.0-10.el7                ################################# [100%]
## 注意:虽然安装成功了 但无法解决依赖关系 依赖是没安装的 还是没用的

# 也可以直接加阿里云网站想要程序的网址下载程序包
[root@web ~]# rpm -ivh https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/zip-3.0-11.el7.x86_64.rpm
Retrieving https://mirrors.aliyun.com/centos/7.9.2009/os/x86_64/Packages/zip-3.0-11.el7.x86_64.rpm
Preparing...                          ################################# [100%]
Updating / installing...
   1:zip-3.0-11.el7                   ################################# [100%]

rpm命令使用-查询

# 语法
rpm [选项] rpm包名

# 选项
-q:查看指定的安装包是否安装
[root@web ~]# rpm -q tree
tree-1.6.0-10.el7.x86_64  # 显示该程序的包名
[root@web ~]# rpm -q tree
package tree is not installed  # 未安装

-----------------以下选项皆要配合-q使用 ---------------
-a:全部安装过的包
[root@web ~]# rpm -qa
NetworkManager-team-1.18.8-1.el7.x86_64
grub2-common-2.02-0.86.el7.centos.noarch
NetworkManager-tui-1.18.8-1.el7.x86_64
..........

[root@web ~]# rpm -qa|grep 'tools'
kernel-tools-3.10.0-1160.el7.x86_64
sysvinit-tools-2.88-14.dsf.el7.x86_64
httpd-tools-2.4.6-98.el7.centos.7.x86_64
kernel-tools-libs-3.10.0-1160.el7.x86_64
nss-tools-3.44.0-7.el7_7.x86_64
grub2-tools-2.02-0.86.el7.centos.x86_64
grub2-tools-extra-2.02-0.86.el7.centos.x86_64
open-vm-tools-11.0.5-3.el7.x86_64
kexec-tools-2.0.15-51.el7.x86_64
net-tools-2.0-0.25.20131004git.el7.x86_64
grub2-tools-minimal-2.02-0.86.el7.centos.x86_64
alsa-tools-firmware-1.1.0-1.el7.x86_64

-i:详细信息
[root@web ~]# rpm -qi zip
Name        : zip
Version     : 3.0
Release     : 11.el7
Architecture: x86_64
Install Date: Thu 06 Jul 2023 04:01:08 PM CST
Group       : Applications/Archiving
Size        : 815173
License     : BSD
Signature   : RSA/SHA256, Mon 21 Nov 2016 05:04:58 AM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : zip-3.0-11.el7.src.rpm
Build Date  : Sun 06 Nov 2016 12:49:55 AM CST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.info-zip.org/Zip.html
Summary     : A file compression and packaging utility compatible with PKZIP
Description :
The zip program is a compression and file packaging utility.  Zip is
analogous to a combination of the UNIX tar and compress commands and
is compatible with PKZIP (a compression and file packaging utility for
MS-DOS systems).

Install the zip package if you need to compress files using the zip
program.

## -q -i -a组合使用
[root@web ~]# rpm -qa|grep 'tools'|xargs rpm -qi
[root@web ~]# rpm -qi `rpm -qa|grep 'tools'
[root@web ~]# rpm -qi $(rpm -qa|grep 'net')

-l:列出rpm包相关的配置文件和目录
[root@web ~]# rpm -ql zip
/usr/bin/zip
/usr/bin/zipcloak
/usr/bin/zipnote
/usr/bin/zipsplit
/usr/share/doc/zip-3.0
/usr/share/doc/zip-3.0/CHANGES
/usr/share/doc/zip-3.0/LICENSE
/usr/share/doc/zip-3.0/README
/usr/share/doc/zip-3.0/README.CR
/usr/share/doc/zip-3.0/TODO
/usr/share/doc/zip-3.0/WHATSNEW
/usr/share/doc/zip-3.0/WHERE
/usr/share/doc/zip-3.0/algorith.txt
/usr/share/man/man1/zip.1.gz
/usr/share/man/man1/zipcloak.1.gz
/usr/share/man/man1/zipnote.1.gz
/usr/share/man/man1/zipsplit.1.gz

-c:只查看该软件包相关的配置文件
[root@web ~]# rpm -qc 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

-d:只查看该软件的帮助文档文件
[root@web ~]# rpm -qd nginx
/usr/share/doc/nginx-1.20.1/CHANGES
/usr/share/doc/nginx-1.20.1/README
/usr/share/doc/nginx-1.20.1/README.dynamic
/usr/share/doc/nginx-1.20.1/UPGRADE-NOTES-1.6-to-1.10
/usr/share/man/man3/nginx.3pm.gz
/usr/share/man/man8/nginx-upgrade.8.gz
/usr/share/man/man8/nginx.8.gz

-f:反向查找,根据配置文件,目录,帮助文档,查找该文件属于哪个包
[root@web ~]# rpm -qf /etc/nginx/conf.d/
nginx-filesystem-1.20.1-10.el7.noarch
# 不知道包属于哪个文件 想要知道文件内容时 可以组合查找
[root@web ~]# rpm -ql $(rpm -qf /etc/nginx)
/etc/nginx
/etc/nginx/conf.d
/etc/nginx/default.d
/etc/systemd/system/nginx.service.d
/usr/lib/systemd/system/nginx.service.d
/usr/share/nginx
/usr/share/nginx/html

--scripts:查看安装包,安装之前之后,和卸载之前之后要执行的脚本
[root@web ~]# rpm -q --scripts nginx
postinstall scriptlet (using /bin/sh):

if [ $1 -eq 1 ] ; then 
        # Initial installation 
        systemctl preset nginx.service >/dev/null 2>&1 || : 
fi
preuninstall scriptlet (using /bin/sh):

if [ $1 -eq 0 ] ; then 
        # Package removal, not upgrade 
        systemctl --no-reload disable nginx.service > /dev/null 2>&1 || : 
        systemctl stop nginx.service > /dev/null 2>&1 || : 
fi
postuninstall scriptlet (using /bin/sh):

systemctl daemon-reload >/dev/null 2>&1 || : 

if [ $1 -ge 1 ]; then
    /usr/bin/nginx-upgrade >/dev/null 2>&1 || :
fi

------------  -p选项和上面选项配合使用  -------------
-p:查询未安装的包相关信息

-qip:查看没有安装包的详细信息(指定一个已存在的完整包名或者远程包名

[root@web ~]# cd /mnt/Packages/
[root@web Packages]# rpm -qip tree-1.6.0-10.el7.x86_64.rpm
Name        : tree
Version     : 1.6.0
Release     : 10.el7
Architecture: x86_64
Install Date: (not installed)
Group       : Applications/File
Size        : 89505
License     : GPLv2+
Signature   : RSA/SHA256, Fri 04 Jul 2014 01:36:46 PM CST, Key ID 24c6a8a7f4a80eb5
Source RPM  : tree-1.6.0-10.el7.src.rpm
Build Date  : Tue 10 Jun 2014 03:28:53 AM CST
Build Host  : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://mama.indstate.edu/users/ice/tree/
Summary     : File system tree viewer
Description :
The tree utility recursively displays the contents of directories in a
tree-like format.  Tree is basically a UNIX port of the DOS tree
utility.

-qlp:查看没有安装包会生成那些文件(指定一个已存在的完整包名或者远程包名)
[root@web Packages]# rpm -qlp tree-1.6.0-10.el7.x86_64.rpm
/usr/bin/tree
/usr/share/doc/tree-1.6.0
/usr/share/doc/tree-1.6.0/LICENSE
/usr/share/doc/tree-1.6.0/README
/usr/share/man/man1/tree.1.gz

-qcp:查看没有安装包会生成哪些配置文件(指定一个已存在的完整包名或者远程包名)
[root@web Packages]# rpm -qdp tree-1.6.0-10.el7.x86_64.rpm
/usr/share/doc/tree-1.6.0/LICENSE
/usr/share/doc/tree-1.6.0/README
/usr/share/man/man1/tree.1.gz

rpm命令使用-升级

# 选项
-U:update升级,如果老版本不存在,则安装,如果老版本存在则升级
-f:必须有老版本存在

rpm命令使用-删除

# 选项
-e:后面接卸载的包名即可

镜像网站地址

1.阿里云镜像站:阿里巴巴开源镜像站-OPSX镜像站-阿里云开发者社区 (aliyun.com)
2.网易镜像站:欢迎访问网易开源镜像站 (163.com)
3.清华源:清华大学开源软件镜像站 | Tsinghua Open Source Mirror
4.中科大源:USTC Open Source Software Mirror