CentOS系统上rpm命令管理程序包:
管理操作:安装、升级、卸载、查询和校验、数据库维护;
总结:
rpm常用选项(以软件包:zsh-5.1-1.gf.el7.x86_64.rpm 为例):
==========================================================
A.软件包未安装之前的查询操作(查询对象必须是软件包的完整名称):
~]# rpm -qp –changelog zsh-5.1-1.gf.el7.x86_64.rpm 查看软件包各个版本的changelog;
~]# rpm -qpi zsh-5.1-1.gf.el7.x86_64.rpm 查看软件包相关信息:版本号、大小、所属的包组等;
~]# rpm -qpl zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -qpc zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -qpd zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -qp –scripts zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -qpR zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -qp –provides zsh-5.1-1.gf.el7.x86_64.rpm
B.软件包安装之后的查询操作:
~]# rpm -qa 查看当前系统用’rpm’命令安装的所有程序包;
~]# rpm -q –changelog bash 查看指定的程序包的所有版本的更新日志信息;
~]# rpm -qi btrfs-progs 查看指定的程序包自身的相关信息;
~]# rpm -ql btrfs-progs 查看指定的程序包安装生成的所有文件;
~]# rpm -qf /usr/share/man/man1/wait.1.gz 查看指定的文件是由哪个程序包安装生成的;
~]# rpm -qc bash 查看指定的程序包提供的配置文件;
~]# rpm -qd bash 查看指定的程序包提供的相关文档;
~]# rpm -q –scripts bash 查看指定的程序包自带的脚本片段;
~]# rpm -qR btrfs-progs 查看指定的程序包的依赖关系(依赖于);
~]# rpm -q –provides btrfs-progs 查询列出指定程序包提供的所有’CAPABILITY’;
C.软件包安装与升级:
~]# rpm -{i|U|F}vh –test zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nodeps zsh-5.1-1.gf.el7.x86_64.rpm 一般不建议使用此选项,除非知道自己在做什么;
~]# rpm -{i|U|F}vh –replacepkgs zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nopre zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nopost zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nopreun zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nopostun zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –noscripts zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nosignature zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –nodigest zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –oldpackage zsh-5.1-1.gf.el7.x86_64.rpm
~]# rpm -{i|U|F}vh –force zsh-5.1-1.gf.el7.x86_64.rpm
D.程序包的卸载:
~]# rpm -e –test btrfs-progs
~]# rpm -e –nodeps btrfs-progs
~]# rpm -e –noscripts btrfs-progs
~]# rpm -e –nopreun btrfs-progs
~]# rpm -e –nopostun btrfs-progs
~]# rpm -e –allmatches btrfs-progs
***************************************************************************
***************************************************************************
rpm命令详细说明:
********************
rpm命令 – RPM Package Manager
rpm命令使用方式:rpm [OPTIONS] [PACKAGE_FILE]
主选项[OPTIONS]:
=================
安装:-i , –install
升级:-U , –update , -F , –freshen
卸载:-e , –erase
查询:-q , –query
校验:-V , –verify
数据库维护:–builddb , –initdb
通用选项:
================
-v : verbose,安装过程显示详细信息;
-vv : 安装过程显示更详细的信息;
子选项[SUB OPTIONS]:
=====================
1> 安装:rpm {-i|–install} [install-options] PACKAGE_FILE …
install-options:
—————–
-h : hash marks输出进度条,用50个’#’号表示,每个’#’号表示2%的进度;
–test : 测试安装,检查并报告程序包的依赖关系或冲突信息等;
–nodeps : 忽略依赖关系,不建议使用此选项;
–replacepkgs : 重新安装;如果修改了程序包安装后产生的某个文件,比如修改了配置文件,如果需要重新安装此软件包,最好先删除刚修改过的文件,然后在执行重新安装;如果不删除修改过的文件,重新安装后,此文件可能不会被新安装产生的文件所覆盖,重新安装操作无意义;
说明:rpm包可以自带4类脚本:
preinstall : 安装操作开始之前运行的脚本,用%pre表示;
postinstall : 安装操作完成后运行的脚本,用%post表示;
preuninstall : 卸载操作开始之前运行的脚本,用%preun表示;
postuninstall : 卸载操作完成后运行的脚本,用%postun表示;
–nopre : 不执行’安装操作开始之前运行的脚本’;
–nopost : 不执行’安装操作完成后运行的脚本’;
–nopreun : 不执行’卸载操作开始之前运行的脚本’;
–nopostun : 不执行’卸载操作开始之前运行的脚本’;
–noscripts : 不执行所有脚本;
–nosignature : 不检查软件包来源合法性、包签名信息;
–nodigest : 不检查软件包的完整性信息;
2> 升级:
rpm {-U|–upgrade} [install-options] PACKAGE_FILE …
rpm {-F|–freshen} [install-options] PACKAGE_FILE …
上面2个命令的区别:
-U : 升级或安装;
-f : 升级;必须存在原旧版本的软件包,否则不能升级;
install-options:
—————–
-h : hash marks输出进度条,用50个’#’号表示,每个’#’号表示2%的进度;
–test : 测试安装,检查并报告程序包的依赖关系或冲突信息等;
–nodeps : 忽略依赖关系,不建议使用此选项;
–replacepkgs : 重新安装;如果修改了程序包安装后产生的某个文件,比如修改了配置文件,如果需要重新安装此软件包,最好先删除刚修改过的文件,然后在执行重新安装;如果不删除修改过的文件,重新安装后,此文件可能不会被新安装产生的文件所覆盖,重新安装操作无意义;
说明:rpm包可以自带4类脚本:
preinstall : 安装操作开始之前运行的脚本,用%pre表示;
postinstall : 安装操作完成后运行的脚本,用%post表示;
preuninstall : 卸载操作开始之前运行的脚本,用%preun表示;
postuninstall : 卸载操作完成后运行的脚本,用%postun表示;
–nopre : 不执行’安装操作开始之前运行的脚本’;
–nopost : 不执行’安装操作完成后运行的脚本’;
–nopreun : 不执行’卸载操作开始之前运行的脚本’;
–nopostun : 不执行’卸载操作开始之前运行的脚本’;
–noscripts : 不执行所有脚本;
–nosignature : 不检查软件包来源合法性、包签名信息;
–nodigest : 不检查软件包的完整性信息;
–oldpackage : 降级安装;
–force : 强制升级;
NOTE:
1> 不要对内核做升级操作;Linux支持多内核版本并存,直接安装新版本内核即可;
2> 如果某程序包的配置文件安装后曾被修改过,升级时,新版本的程序提供的同一个位置文件不会覆盖原有版本的配置文件,而是吧新版本的配置文件重命名(FILENAME.rpmnew)后提供;
3> 卸载:rpm {-e|–erase} [–allmatches] [–justdb] [–nodeps] [–noscripts] [–notriggers] [–test] PACKAGE_NAME …
–allmatches : 软件包可以多版本共存于系统;卸载所有匹配到的指定名称的程序包的版本;
–nodeps : 忽略程序包的依赖关系;
–noscripts : 不执行所有脚本;
–test : 测试卸载,可以查看到是否有依赖关系;
4> 查询:rpm {-q|–query} [select-options] [query-options]
select-options:
——————–
PACKAGE_NAME :查询指定的程序包是否已经安装,及其版本;
-a , –all : 查询系统上所有已经安装的程序包;
-f FILE : 查询指定的文件(可以用文件的完整路径)由哪个程序包安装生成的;
-p , –package PACKAGE_FILE : 用于实现对未安装的程序包执行查询操作;
–whatprovides CAPABILITY : 查询指定的’CAPABILITY(能力)’由哪个程序包提供;
–whatrequires CAPABILITY : 查询指定的’CAPABILITY(能力)’被哪个程序包所依赖;
query-options:
———————
–changelog : 查询rpm包的changlog;
-l , –list : 程序包生成的所有文件列表;
-i , –info : 程序包相关的信息,包括版本号、程序包大小、所属的包组等;
-c , –configfiles : 查询指定的程序包提供的配置文件;
-d , –docfiles : 查询指定的程序包提供的文档,比如说明文档、使用手册等;
–provides : 列出指定的程序包提供的所有’CPPABILITY’;
-R , –requires : 查询指定的程序包的依赖关系(此程序包依赖于哪些程序包);
–scripts : 查看程序包自带的脚本片段;
5> 安装完软件包后的校验操作:
rpm {-V|–verify} [select-options] [verify-options]
软件包安装完成后会对系统写入若干的文件,如果没对任何文件做修改操作,则下面命令(简单用法)运行后不会输出任何信息;
如果对任何文件做了任何修改操作,则运行下面命令后会有提示,提示哪个文件被修改了;
~]# rpm -V PACKAGE_NAME
文件被修改过后,标准输出显示的信息中字符的表示意义:
总共9个字符段,点号’.’表示测试通过;问号’?’表示无法执行测试(例如,文件权限防止读取);
S file Size differs
M Mode differs (includes permissions and file type)
5 digest (formerly MD5 sum) differs
D Device major/minor number mismatch
L readLink(2) path mismatch
U User ownership differs
G Group ownership differs
T mTime differs
P caPabilities differ
select-options:
——————–
PACKAGE_NAME :查询指定的程序包是否已经安装,及其版本;
-a , –all : 查询系统上所有已经安装的程序包;
-f FILE : 查询指定的文件(可以用文件的完整路径)由哪个程序包安装生成的;
-p , –package PACKAGE_FILE : 用于实现对未安装的程序包执行查询操作;
–whatprovides CAPABILITY : 查询指定的’CAPABILITY(能力)’由哪个程序包提供;
–whatrequires CAPABILITY : 查询指定的’CAPABILITY(能力)’被哪个程序包所依赖;
verify-options:
———————-
--nodeps Don't verify dependencies of packages. --nofiles Don't verify any attributes of package files. --noscripts Don't execute the %verifyscript scriptlet (if any). --nosignature Don't verify package or header signatures when reading. --nosize --nomode --nofiledigest (formerly --nomd5) --nodigest Don't verify package or header digests when reading. --nordev --nolinkto --nouser --nogroup --nomtime
校验操作举例:
举例:先安装一个rpm包; ---------------------- [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm -ivh zsh-5.1-1.gf.el7.x86_64.rpm warning: zsh-5.1-1.gf.el7.x86_64.rpm: Header V4 RSA/SHA1 Signature, key ID da8b7718: NOKEY Preparing... ################################# [100%] Updating / installing... 1:zsh-5.1-1.gf.el7 ################################# [100%] [root@kouyuushinn ~]# 承上,刚安装完软件包后,校验程序包的安装: -------------------------------------- [root@kouyuushinn ~]# rpm -V zsh [root@kouyuushinn ~]# 承上,修改程序包安装后产生的其中一个文件,然后做校验: ------------------------------------------------ [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm -ql zsh | grep zfput /usr/share/zsh/5.1/functions/zfput [root@kouyuushinn ~]# [root@kouyuushinn ~]# vim /usr/share/zsh/5.1/functions/zfput # function zfput { # Simple put: dump every file under the same name, but stripping # off any directory parts to get the remote filename (i.e. always # goes into current remote directory). Use zfpcp to specify new # file name or new directory at remote end. # # -r means put recursively: any directories encountered will have # all their contents to arbitrary depth transferred. Note that # this creates the required directories. Any files in subdirectories # whose names begin with a `.' will also be included. # emulate -L zsh ...... [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm -V zsh S.5....T. /usr/share/zsh/5.1/functions/zfput [root@kouyuushinn ~]# 承上,修改某个文件后,加入校验参数'--nofiles',表示不对文件做任何校验,校验结果不会显示任何信息; ---------------------------------------------------------------------------------------- [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm -V --nofiles zsh [root@kouyuushinn ~]# [root@kouyuushinn ~]#
6> 包来源合法性验证(包的数字签名)和完整性验证
获取并导入信任的包制作者的秘钥(CentOS发行版):
CentOS 7 : ~]# rpm –import RPM-GPG-KEY-FILENAME
导入后,可在系统的固定目录中找到对应的秘钥文件:/etc/pki/rpm-gpg/
[root@kouyuushinn ~]# [root@kouyuushinn ~]# ll /etc/pki/rpm-gpg/ total 16 -rw-r--r--. 1 root root 1690 Aug 30 2017 RPM-GPG-KEY-CentOS-7 -rw-r--r--. 1 root root 1004 Aug 30 2017 RPM-GPG-KEY-CentOS-Debug-7 -rw-r--r--. 1 root root 1690 Aug 30 2017 RPM-GPG-KEY-CentOS-Testing-7 -rw-r--r-- 1 root root 1662 Oct 3 2017 RPM-GPG-KEY-EPEL-7 [root@kouyuushinn ~]#
验证方式:手动、自动;
导入秘钥文件后,可手动对程序包做验证:
~]# rpm -k PACKAGE_NAME
安装某组织签名的程序时,安装程序包时会自动执行验证;
7> rpm管理器的数据库
rpm管理器的数据库的路径:
/var/lib/rpm : 查询、卸载操作就是基于此数据进行操作的;
[root@kouyuushinn ~]# [root@kouyuushinn ~]# ll /var/lib/rpm total 76104 -rw-r--r--. 1 root root 2347008 May 11 02:11 Basenames -rw-r--r--. 1 root root 8192 May 9 06:42 Conflictname -rw-r--r-- 1 root root 270336 May 11 02:46 __db.001 -rw-r--r-- 1 root root 81920 May 11 02:46 __db.002 -rw-r--r-- 1 root root 1318912 May 11 02:46 __db.003 -rw-r--r--. 1 root root 643072 May 11 02:11 Dirnames -rw-r--r--. 1 root root 20480 May 11 02:11 Group -rw-r--r--. 1 root root 12288 May 11 02:11 Installtid -rw-r--r--. 1 root root 36864 May 11 02:11 Name -rw-r--r--. 1 root root 16384 May 9 05:16 Obsoletename -rw-r--r--. 1 root root 71254016 May 11 02:11 Packages -rw-r--r--. 1 root root 1773568 May 11 02:11 Providename -rw-r--r--. 1 root root 167936 May 11 02:11 Requirename -rw-r--r--. 1 root root 57344 May 11 02:11 Sha1header -rw-r--r--. 1 root root 40960 May 11 02:11 Sigmd5 -rw-r--r--. 1 root root 8192 May 9 06:42 Triggername [root@kouyuushinn ~]#
rpm管理器的数据库重建操作:
~]# man 8 rpmdb 查看帮助手册;
rpm {–initdb|–rebuilddb} [-v] [–dbpath DIRECTORY] [–root DIRECTORY]
–initdb : 初始化数据库,如果当前系统无任何数据库时,可初始化创建一个新的数据库;如果当前系统有数据库,则不执行任何操作;
–rebuilddb : rebuild the database indices from the installed package headers ;重建数据库,从已安装的程序包的包头部重新构建数据库索引;
举例,在临时目录创建、重建数据库操作:
[root@kouyuushinn ~]# [root@kouyuushinn ~]# mkdir /tmp/test_rpm_db 创建临时目录,测试是否可以构建rpm数据库; [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm --initdb --dbpath=/tmp/test_rpm_db/ 构建rpm数据库; [root@kouyuushinn ~]# [root@kouyuushinn ~]# ll /tmp/test_rpm_db/ 成功构建rpm数据库; total 388 -rw-r--r-- 1 root root 8192 May 11 03:09 Basenames -rw-r--r-- 1 root root 8192 May 11 03:09 Conflictname -rw-r--r-- 1 root root 270336 May 11 03:09 __db.001 -rw-r--r-- 1 root root 81920 May 11 03:09 __db.002 -rw-r--r-- 1 root root 107048 May 11 03:09 __db.003 -rw-r--r-- 1 root root 8192 May 11 03:09 Dirnames -rw-r--r-- 1 root root 8192 May 11 03:09 Group -rw-r--r-- 1 root root 8192 May 11 03:09 Installtid -rw-r--r-- 1 root root 8192 May 11 03:09 Name -rw-r--r-- 1 root root 8192 May 11 03:09 Obsoletename -rw-r--r-- 1 root root 12288 May 11 03:09 Packages -rw-r--r-- 1 root root 8192 May 11 03:09 Providename -rw-r--r-- 1 root root 8192 May 11 03:09 Requirename -rw-r--r-- 1 root root 8192 May 11 03:09 Sha1header -rw-r--r-- 1 root root 8192 May 11 03:09 Sigmd5 -rw-r--r-- 1 root root 8192 May 11 03:09 Triggername [root@kouyuushinn ~]# [root@kouyuushinn ~]# ll /var/lib/rpm 与系统默认的rpm数据库做比对; total 76104 -rw-r--r--. 1 root root 2347008 May 11 02:11 Basenames -rw-r--r--. 1 root root 8192 May 9 06:42 Conflictname -rw-r--r-- 1 root root 270336 May 11 02:46 __db.001 与rpm事务操作有关,重建数据库索引后,此3个db文件会自动删掉; -rw-r--r-- 1 root root 81920 May 11 02:46 __db.002 -rw-r--r-- 1 root root 1318912 May 11 02:46 __db.003 -rw-r--r--. 1 root root 643072 May 11 02:11 Dirnames -rw-r--r--. 1 root root 20480 May 11 02:11 Group -rw-r--r--. 1 root root 12288 May 11 02:11 Installtid -rw-r--r--. 1 root root 36864 May 11 02:11 Name -rw-r--r--. 1 root root 16384 May 9 05:16 Obsoletename -rw-r--r--. 1 root root 71254016 May 11 02:11 Packages -rw-r--r--. 1 root root 1773568 May 11 02:11 Providename -rw-r--r--. 1 root root 167936 May 11 02:11 Requirename -rw-r--r--. 1 root root 57344 May 11 02:11 Sha1header -rw-r--r--. 1 root root 40960 May 11 02:11 Sigmd5 -rw-r--r--. 1 root root 8192 May 9 06:42 Triggername [root@kouyuushinn ~]# [root@kouyuushinn ~]# [root@kouyuushinn ~]# rpm --rebuilddb --dbpath=/tmp/test_rpm_db/ 重建rpm数据库; [root@kouyuushinn ~]# [root@kouyuushinn ~]# ll /tmp/test_rpm_db/ total 104 -rw-r--r-- 1 root root 8192 May 11 03:09 Basenames -rw-r--r-- 1 root root 8192 May 11 03:09 Conflictname -rw-r--r-- 1 root root 8192 May 11 03:09 Dirnames -rw-r--r-- 1 root root 8192 May 11 03:09 Group -rw-r--r-- 1 root root 8192 May 11 03:09 Installtid -rw-r--r-- 1 root root 8192 May 11 03:09 Name -rw-r--r-- 1 root root 8192 May 11 03:09 Obsoletename -rw-r--r-- 1 root root 12288 May 11 03:10 Packages -rw-r--r-- 1 root root 8192 May 11 03:09 Providename -rw-r--r-- 1 root root 8192 May 11 03:09 Requirename -rw-r--r-- 1 root root 8192 May 11 03:09 Sha1header -rw-r--r-- 1 root root 8192 May 11 03:09 Sigmd5 -rw-r--r-- 1 root root 8192 May 11 03:09 Triggername [root@kouyuushinn ~]#