一、rpm简介

RPM(Red Hat Package Manager,后为RPM is Package Manager)是用于 Linux 发行版(distribution)的最常见的软件包管理器(另一重要的包管理器为deb)。后被LSB收录为Linux的标准包管理器,

 

RPM包命名方式:name-version-release.architecture.rpm(程序包名称-版本号-发行号-设用的平台.rpm)

如 :

Linux 初学者rpm管理工具之从头到尾全过程使用

二、RPM功用

有五种基本的操作功能:安装、卸载、升级、查询和验证。这五种基本功能的实现仅仅需要用rpm + 选项 + rpm包就能轻易地实现。

下面从获取程序包到安装、卸载、升级、查询和验证等日常操作流程来讲解rpm的基本应用。

(一)、安装包的获取

安装包的来源主要有以下4种途径:

(1)系统的发行光盘镜像或官方站点(或站点镜像服务器);

 PS:如果是用虚拟机,则需要将iso先挂载:mount -r /dev/cdrom /media/cdrom

 官方站点的国内镜像:

mirrors.sohu.com

mirrors.163.com

mirrors.aliyun.com

http://mirror.hust.edu.cn(教育网访问华中科技大学的镜像站点速度快)

(2)程序包的官方站点

(3)第三方组织:一般找信誉比较好的组织网站进行下载,如 epel

(4)搜索引擎:

http://rpmfind.net

http://rpm.pbone.net

http://pkgs.org


(二)、程序包的合法性验证

我们获取了一个程序包,不可能直接使用,一般都需要对其来源与内容的合法性进行验证,以免安装了被植入后门的程序对生产生活带来损失。

来源的合法性主要是通过我们可信的制作者的数字签名进行验证

内容的合法性主要是通过用同样的特征码提取算法提取程序包的特征码,并与原作者提供的相比较进行验证。

         验正其光盘中程序包的来源及完整性:

            a .  先导入公钥文件:rpm --import /path/to/RPM-GPG-KEY-FILE

                                  例如:# rpm --import RPM-GPG-KEY-CentOS-6

            b .  验证:rpm {-K|--checksig} PACKAGE_FILE

                       --nosignature: 不检查来源合法性

                       --nodigest: 不检查完整性

Linux 初学者rpm管理工具之从头到尾全过程使用

(三)安装前的查询:

查询某包是否已经安装,以及检查安装的所有包;还可以查看某个尚未安装的包的详细信息

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

                                           [select-options]:对尚未安装的程序包使用的查询选项

1、查询某包或某些包是否安装:

rpm -q PACKAGE_NAME...

[[email protected] pub]# rpm -q libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled

 2、查询已经安装的所有包:

rpm -qa

[[email protected] pub]# rpm -qa
iptables-ipv6-1.4.7-14.el6.x86_64
tcp_wrappers-7.6-57.el6.x86_64
evince-libs-2.28.2-14.el6_0.1.x86_64
…………(太多了,不详列)
dbus-c++-0.5.0-0.10.20090203git13281b3.1.el6.x86_64
[[email protected] pub]#

  3、查询尚未安装的包文件的相关信息

-p

#rpm -qpi PACKAGE_FILE

[[email protected] pub]# rpm -qpi libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
Name        : libvirt-daemon-driver-qemu   Relocations: (not relocatable)
Version     : 0.10.2.8                          Vendor: CentOS Devel
Release     : 8.el6.centos.alt              Build Date: Sat 27 Sep 201403:55:59 AM CST
Install Date: (notinstalled)               Build Host:bn5.alt.bsys.dev.centos.org
Group       : Development/Libraries         Source RPM:libvirt-0.10.2.8-8.el6.centos.alt.src.rpm
Size        : 839872                           License: LGPLv2+
Signature   : RSA/SHA1, Sat 27 Sep 2014 04:56:09 AM CST,Key ID 0946fca2c105b9de
Packager    : CentOS Devel BuildSystem <http://bugs.centos.org>
URL         : http://libvirt.org/
Summary     : Qemu driver plugin for the libvirtddaemon
Description :
The qemu driverplugin for the libvirtd daemon, providing
an implementation ofthe hypervisor driver APIs using
QEMU
[[email protected] pub]#


(四)安装

       命令模式:# rpm {-i|--install} [install-options] PACKAGE_FILE1...

               {-i|--install}:-i或者--install必选一个,表示是安装程序包

               [install-options]主要有一下几个,可选可不选:

                    -h :   hash,以#来表示安装进度,一个#表示2%的进度

                    -v或者--verbose:显示安装过程中的详细信息,可通过增加v的个数来表示显示的详细程度,如-vv、-vvv

                        --test:不执行真正的安装过程,而仅报告依赖关系及冲突信息等,常用于安装前测试所有程序包是否齐全

                  --nodeps:忽略依赖关系安装指定的程序包

                   --force:强制安装,对因依赖关系而无法安装的情况无效!!

                   --replacepkgs:用于原来安装有该程序时重新安装并覆盖原有的所有文件

                   --replacefiles:用于原来安装有该程序时重新安装并覆盖原有的有限几个文件

                  --relocate OLDPATH=NEWPATH:改变安装路径,只有那些允许修改安装路径的程序包才可使用此选项,不建议使用。

上面的选项可以单独使用,也可以组合使用,也可不使用。安装时常用的组合: -ivh, -ivvh

Linux 初学者rpm管理工具之从头到尾全过程使用

Linux 初学者rpm管理工具之从头到尾全过程使用

Linux 初学者rpm管理工具之从头到尾全过程使用

 

( 五)、安装后相关查询

事例中做了两个程序包的查询,第一个(libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm)未安装,第二个(bash-4.1.2-29.el6.x86_64)已安装


1、查询某包的简要说明信息:

rpm-qi PACKAGE_NAME

[[email protected] pub]# rpm -qi libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
[[email protected] pub]#which bash
/bin/bash
[[email protected] pub]# rpm -qf /bin/bash
bash-4.1.2-29.el6.x86_64
[[email protected] pub]# rpm -qi bash-4.1.2-29.el6.x86_64
Name        : bash                         Relocations: (notrelocatable)
Version     : 4.1.2                             Vendor: CentOS
Release     : 29.el6                        Build Date: Thu 16 Oct2014 09:58:35 PM CST
Install Date: Sat 28Mar 2015 04:06:19 PM CST      Build Host:c6b8.bsys.dev.centos.org
Group       : System Environment/Shells     Source RPM: bash-4.1.2-29.el6.src.rpm
Size        : 3140846                          License: GPLv3+
Signature   : RSA/SHA1, Sat 18 Oct 2014 04:03:01 AM CST,Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem <http://bugs.centos.org>
URL         : http://www.gnu.org/software/bash
Summary     : The GNU Bourne Again shell
Description :
The GNU Bourne Againshell (Bash) is a shell or command language
interpreter that iscompatible with the Bourne shell (sh). Bash
incorporates usefulfeatures from the Korn shell (ksh) and the C shell
(csh). Most shscripts can be run by bash without modification.
[[email protected] pub]#

2、查询某包安装生成的文件列表:

rpm-ql PACKAGE_NAME

rpm每装一个程序,各种安装信息(如名字,文件,capabilities,路径)都会记录在rpm数据库/var/lib/rpm中

[[email protected] pub]# rpm -ql libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
 
[[email protected] pub]# rpm -ql bash-4.1.2-29.el6.x86_64
/bin/bash
/bin/sh
/etc/skel/.bash_logout
/etc/skel/.bash_profile
……(过长,省略)
/usr/share/man/man1/unset.1.gz
/usr/share/man/man1/wait.1.gz
[[email protected] pub]#

3查询某包安装完成后生成的所有配置文件:

rpm-qc PACKAGE_NAME

[[email protected] pub]# rpm -qc libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
 
[[email protected] pub]# rpm -qc bash-4.1.2-29.el6.x86_64
/etc/skel/.bash_logout
/etc/skel/.bash_profile
/etc/skel/.bashrc
[[email protected] pub]#

4查询某包安装完成后生成的所有帮助文件:

rpm-qd PACKAGE_NAME

[[email protected] pub]# rpm -qd libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
[[email protected] pub]# rpm -qd bash-4.1.2-29.el6.x86_64
/usr/share/doc/bash-4.1.2/COPYING
/usr/share/info/bash.info.gz
……(过长,省略)
/usr/share/man/man1/unset.1.gz
/usr/share/man/man1/wait.1.gz
[[email protected] pub]#

5查看某包制作时随版本变化的changelog信息:

rpm-q --changelog PACKAGE_NAME

[[email protected] pub]# rpm -q --changelog libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
 
[[email protected] pub]# rpm -q --changelog bash-4.1.2-29.el6.x86_64
* Fri Sep 26 2014Michal Hlavinka <[email protected]> - 4.1.2-29
- CVE-2014-7169
  Resolves: #1146323
 
* Mon Sep 15 2014Ondrej Oprala <[email protected]> - 4.1.2-28
- Fix-up the patch
  Related: #1141646
……(过长,省略)
 
* Tue Jun 03 1997Erik Troan <[email protected]>
- built againstglibc
 
[[email protected] pub]#

6、查询某包提供的capabilities:

rpm-q --provides PACKAGE_NAME

[[email protected] pub]# rpm -q --provides libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
[[email protected] pub]# rpm -q --provides bash-4.1.2-29.el6.x86_64
config(bash) =4.1.2-29.el6
bash = 4.1.2-29.el6
bash(x86-64) =4.1.2-29.el6
[[email protected] pub]#

7、查询某包所依赖的capabilities:

rpm-q --requires PACKAGE_NAME

[[email protected] pub]# rpm -q --requires libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
 
[[email protected] pub]# rpm -q --requires bash-4.1.2-29.el6.x86_64
/bin/sh  
/bin/sh  
config(bash) =4.1.2-29.el6
libc.so.6()(64bit)  
libc.so.6(GLIBC_2.11)(64bit)  
libc.so.6(GLIBC_2.2.5)(64bit)  
libc.so.6(GLIBC_2.3)(64bit)  
libc.so.6(GLIBC_2.3.4)(64bit)  
libc.so.6(GLIBC_2.4)(64bit)  
libdl.so.2()(64bit)  
libdl.so.2(GLIBC_2.2.5)(64bit)  
libtinfo.so.5()(64bit)  
ncurses-libs  
rpmlib(BuiltinLuaScripts)<= 4.2.2-1
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
[[email protected] pub]#

8、查询某包安装或卸载时执行脚本:

rpm-q --scripts PACKAGE_NAME

[[email protected] pub]# rpm -q --scripts libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm
packagelibvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm is notinstalled
[[email protected] pub]# rpm -q --scripts bash-4.1.2-29.el6.x86_64
postinstallscriptlet (using <lua>):
bashfound = false;
shfound = false;
 
f =io.open("/etc/shells", "r");
if f == nil
then
……(过长,省略)
[[email protected] pub]#

9、查询某文件是由哪个包安装生成:

rpm-qf /PATH/TO/SOMEFILE

[[email protected] pub]# which /bin/bash(which可以查询到程序路径地址)
/bin/bash
[[email protected] pub]# rpm -qf bash
error: file/var/ftp/pub/bash: No such file or directory
[[email protected] pub]# rpm -qf /bin/bash  (一定要指明有效的路径地址,否则默认是当前工作目录)
bash-4.1.2-29.el6.x86_64
[[email protected] pub]#


(六)、升级

 rpm {-U|--upgrade} [install-options]PACKAGE_FILE ...

         -U:升级或安装(如果原来安装有则用此程序包升级,原来没有安装过则用此程序包安装)

 

 rpm {-F|--freshen} [install-options]PACKAGE_FILE ...

         -F:升级(如果没有安装老版本的程序,则不升级不安装)

 

 组合:-Uvh, -Fvh

 

 --test

 --nodeps

 --force

 --oldpackage:降级到旧版本;

 

 

[[email protected] pub]# rpm -U --test libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm error: Failed dependencies: libvirt-daemon = 0.10.2.8-8.el6.centos.alt is needed by libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64 libvirt-daemon-driver-network = 0.10.2.8-8.el6.centos.alt is needed by libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64[[email protected] pub]# rpm -U --test --nodeps libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm [[email protected] pub]# 没有任何提示说明能成功安装(test指明是测试而不是真的安装)
[[email protected] pub]# rpm -U --test  --force libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64.rpm error: Failed dependencies: libvirt-daemon = 0.10.2.8-8.el6.centos.alt is needed by libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64 libvirt-daemon-driver-network = 0.10.2.8-8.el6.centos.alt is needed by libvirt-daemon-driver-qemu-0.10.2.8-8.el6.centos.alt.x86_64[[email protected] pub]#(force选项对依赖性报错是强制安装不了的!)


注意:一定不要对内核执行升级;Linux允许多内核共存,所以,可以直接安装多个不同版本内核;

 

注意:如果程序包的配置文件安装后曾被修改,升级时,新版本的文件不会覆盖老版本的配置文件,而把新版本的配置文件重命名(加后缀.rpmnew)后保存;


( 七)日常工作之校验

查询包安装之后生成的文件是否发生了改变

 

命令模式:rpm{-V|--verify} [select-options] [verify-options]

 

常见用法:rpm -V PACKAGE_NAME

 结果说明:

       表示对应位置无改变

       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提供的功能改变

[[email protected] pub]# rpm -qc /bin/bashpackage /bin/bash is not installed[[email protected] pub]# rpm  -qc bash-4.1.2-29.el6.x86_64/etc/skel/.bash_logout/etc/skel/.bash_profile/etc/skel/.bashrc
[[email protected] pub]# vim /etc/skel/.bashrc 
[[email protected] pub]# rpm  -V  bash-4.1.2-29.el6.x86_64 (在空白行添加了3个单词)
prelink: /bin/bash: at least one of file's dependencies has changed since prelinking
S.?......    /bin/bash
S.5....T.  c /etc/skel/.bashrc (文件大小、内容、修改时间发生了改变)
[[email protected] pub]# vim /etc/skel/.bashrc (删除了刚才添加的3个单词)
[[email protected] pub]# rpm  -V  bash-4.1.2-29.el6.x86_64
prelink: /bin/bash: at least one of file's dependencies has changed since prelinking
S.?......    /bin/bash
.......T.  c /etc/skel/.bashrc[[email protected] pub]#


( 八)日常工作之数据库重建

 rpm {--initdb|--rebuilddb} [-v] [--dbpathDIRECTORY]

                                    --initdb: 初始化数据库,即数据库完全不存在时,可新建之; 

        --rebuilddb:无论当前数据存在与否,都会直接重建数据库;

        --dbpathDIRECTORY 指明重建数据库的路径。

[[email protected] pub]# rpm  --rebuilddb --dbpath /opt/
[[email protected] pub]# cd /opt/
[[email protected] opt]# ls
Packages  rh
[[email protected] opt]# file Packages 
Packages: Berkeley DB (Hash, version 9, native byte-order)
[[email protected] opt]# rm -rf Packages 
[[email protected] opt]# rpm  --rebuilddb --dbpath /opt/
[[email protected] opt]# ll
total 12
-rw-r--r--  1 root root 12288 Apr 10 13:38 Packages
drwxr-xr-x. 2 root root  4096 Nov 22  2013 rh
[[email protected] opt]# ll -h
total 12K
-rw-r--r--  1 root root  12K Apr 10 13:38 Packages
drwxr-xr-x. 2 root root 4.0K Nov 22  2013 rh
[[email protected] opt]# rpm  --initdb --dbpath /opt/
[[email protected] opt]# ll
total 852
-rw-r--r--  1 root root   24576 Apr 10 13:39 __db.001
-rw-r--r--  1 root root  229376 Apr 10 13:39 __db.002
-rw-r--r--  1 root root 1318912 Apr 10 13:39 __db.003
-rw-r--r--  1 root root  753664 Apr 10 13:39 __db.004
-rw-r--r--  1 root root   12288 Apr 10 13:38 Packages
drwxr-xr-x. 2 root root    4096 Nov 22  2013 rh

(九)卸载

移除已经安装的程序包

  rpm {-e|--erase} [--allmatches] [--nodeps][--test] PACKAGE_NAME ...

          简单用法:rpm -e PACKAGE_NAME...

                           PACKAGE_NAME...:表示可以一次移除多个程序

         --nodeps:忽略依赖关系;

         --test:测试卸载;dry-run模式;

         --allmatches:如果一个程序包同时安装多个版本,则此选项一次全部卸载之;

注意:如果程序包的配置文件安装后曾被修改,卸载时,此文件通常不会被删除,而是被重命名(加后缀.rpmsave)后留存;



Linux 初学者rpm管理工具之从头到尾全过程使用





三、获取rpm帮助的方法

1、直接用  #  man rpm 命令获取系统内命令自带的详细手册帮助

2、google或者百度 rpm,获取别人对此命令的文章博客等