您的位置:首页 > 运维架构 > Linux

linux下rpm包和命令使用简介

2015-07-10 17:14 501 查看
一、rpm包简介
RPM[1] 是RPM Package Manager(RPM软件包管理器)的缩写,这一文件格式名称虽然打上了RedHat的标志,但是其原始设计理念是开放式的,现在包括OpenLinux、S.u.S.E.以及Turbo Linux等Linux的分发版本都有采用,可以算是公认的行业标准了。

二、rpm包的安装
首先本例实在CentOS中进行的,若是别的发行版不在本篇文件的介绍范围内,同时在找rpm安装包时最好是发行商提供或者是从官方下载,最后还可以从国内比较常用的镜像网站中下载,请记住一定不要安装来路不明的包,不然若安装后果自负,下面说下常用的几个镜像网站有:
CentOS:网易镜像:http://mirrors.163.com 搜狐镜像: http://mirrors.sohu.com

rpm包的安装:
[root@hpf-linux ~]# rpm -ivh zsh-4.3.10-7.el6.i686.rpm
Preparing...                ########################################### [100%]
1:zsh                    ########################################### [100%]
通常在使用rpm命令进行安装rpm包时要提前将rpm包给下载到本地,或者使用http的链接
[root@hpf-linux~]# rpm -ivh
Retrieving http://mirrors.sohu.com/centos/6.6/updates/i386/Packages/unzip-6.0-2.el6_6.i686.rpm Preparing...                ########################################### [100%]
1:unzip                  ########################################### [100%]
若包已经安装后还想从新安装就可以使用--replacepkgs 选项,而原来的配置文件不会被覆盖,新安装的配置文件将会重命名以.rpmnew为后缀的文件。
[root@hpf-linux ~]# rpm -ivh zsh-4.3.10-7.el6.i686.rpm
Preparing...                ########################################### [100%]
package zsh-4.3.10-7.el6.i686 is already installed
[root@hpf-linux ~]# rpm -ivh --replacepkgs  zsh-4.3.10-7.el6.i686.rpm
Preparing...                ########################################### [100%]
1:zsh                       ########################################### [100%]
在安装时出现了安装rpm包常见的依赖关系可以使用--nodeps选项(忽略依赖关系)安装。但是这样虽然安装了包但是还是不能达到自己想要的结果,所以就有了程序包管理器的前段工具(yum)的出现,这样就解决了依赖的问题,由于本篇是介绍rpm安装所以就不介绍yum的使用。

三、rpm包的升级
常用选项及意义:
-Uvh 后面接的套件即使没有安装过,则系统将予以直接安装; 若后面接的套件有安装过旧版,则系统自动更新至新版;
-Fvh 如果后面接的套件并未安装到您的 Linux 系统上,则该套件不会被安装;亦即只有安装至您 Linux 系统内的套件会被『升级』!

[root@hpf-linux ~]# rpm -e zsh
[root@hpf-linux ~]# rpm -q zsh-4.3.10-7.el6.i686.rpm         //查询zsh包是否安装
package zsh-4.3.10-7.el6.i686.rpm is not installed
[root@hpf-linux ~]# rpm -Fvh zsh-4.3.10-7.el6.i686.rpm      //由于系统没有安装zsh包,所以使用-Uvh升级包就无法升级
[root@hpf-linux ~]# rpm -q zsh-4.3.10-7.el6.i686.rpm
package zsh-4.3.10-7.el6.i686.rpm is not installed
[root@hpf-linux ~]# rpm -Uvh zsh-4.3.10-7.el6.i686.rpm
Preparing...                ########################################### [100%]
1:zsh                       ########################################### [100%]


四、rpm包的卸载
前面已经使用过卸载的选项-e,若在卸载某个包时出现被别的包依赖则可以有下面几种情况:
1、把依赖者一同卸载 ;
2、忽略依赖关系:--nodeps ; //一般请不要这么做可能会出现不必要的麻烦
3、不在卸载。

五、rpm包的查询
1、查询所以已安装的包:
[root@hpf-linux ~]# rpm -qa
nagios-plugins-dummy-1.4.16-10.el6.i686
libgxim-0.3.3-3.1.el6.i686
cjkuni-fonts-common-0.2.20080216.1-36.el6.noarch
nagios-plugins-ping-1.4.16-10.el6.i686
.......................
[root@hpf-linux ~]# rpm -qa | grep "zsh"    //查询zsh包是否在计算机上安装zsh-4.3.10-7.el6.i686


2、查询某单个包是否安装也可以这样使用:
[root@hpf-linux ~]# rpm -q zsh
zsh-4.3.10-7.el6.i686


3、查询包的描述信息:
[root@hpf-linux ~]# rpm -qi zsh-4.3.10-7.el6.i686
Name        : zsh                          Relocations: (not relocatable)    //表示不支持用户自定义安装路径
Version     : 4.3.10                            Vendor: CentOS
Release     : 7.el6                         Build Date: 2013年11月25日 星期一 01时37分44秒
Install Date: 2015年07月10日 星期五 00时44分02秒      Build Host: c6b9.bsys.dev.centos.org
Group       : System Environment/Shells     Source RPM: zsh-4.3.10-7.el6.src.rpm
Size        : 4777026                          License: BSD
Signature   : RSA/SHA1, 2013年11月25日 星期一 03时30分40秒, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem < http://bugs.centos.org> URL         : http://zsh.sunsite.dk/ Summary     : A powerful interactive shell
Description :
The zsh shell is a command interpreter usable as an interactive login
shell and as a shell script command processor.  Zsh resembles the ksh
shell (the Korn shell), but includes many enhancements.  Zsh supports
command line editing, built-in spelling correction, programmable
command completion, shell functions (with autoloading), a history
mechanism, and more.


4、查询安装包生成的列表的位置:
[root@hpf-linux ~]# rpm -ql zsh
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
...........


5、查询某文件是那个包安装生成的:
[root@hpf-linux ~]# rpm -qf /etc/passwd
setup-2.8.14-20.el6_4.1.noarch


6、查询包安装后生成的帮助文档:
[root@hpf-linux ~]# rpm -qd zsh
/usr/share/doc/zsh-4.3.10/BUGS
/usr/share/doc/zsh-4.3.10/CONTRIBUTORS
/usr/share/doc/zsh-4.3.10/FAQ
/usr/share/doc/zsh-4.3.10/FEATURES
..........................


7、查询安装后生成的配置文件:
[root@hpf-linux ~]# rpm -qc zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
/etc/zshenv
/etc/zshrc


8、查询包相关的脚本:
[root@localhost ~]# rpm -q --scripts httpd
preinstall scriptlet (using /bin/sh):              //preinstall 安装前脚本
# Add the "apache" user
getent group apache >/dev/null || groupadd -g 48 -r apache
getent passwd apache >/dev/null || \
useradd -r -u 48 -g apache -s /sbin/nologin \
-d /var/
www -c
"Apache" apache
exit 0
postinstall scriptlet (using /bin/sh):          //postinstall 安装后脚本
# Register the httpd service
/sbin/chkconfig --add httpd
/sbin/chkconfig --add htcacheclean
preuninstall scriptlet (using /bin/sh):       //preuninstall  卸载前脚本
if [ $1 = 0 ]; then
/sbin/service httpd stop > /dev/null 2>&1
/sbin/chkconfig --del httpd
/sbin/service htcacheclean stop > /dev/null 2>&1
/sbin/chkconfig --del htcacheclean
fi
posttrans scriptlet (using /bin/sh):           //
postuninstall 卸载后脚本
test -f /etc/sysconfig/httpd-disable-posttrans || \
/sbin/service httpd condrestart >/dev/null 2>&1 || :


9、查询尚未安装的rpm包文件的相关信息
查询安装后会生成的文件列表: rpm -qpl 包名
[root@hpf-linux ~]# rpm -q wiresharkpackage
wireshark is not installed
[root@hpf-linux ~]# rpm -qpl wireshark-1.8.10-8.el6_6.i686.rpm
/etc/pam.d/wireshark
/etc/security/console.apps/wireshark
/usr/lib/libwireshark.so.2
/usr/lib/libwireshark.so.2.0.10
/usr/lib/libwiretap.so.2
............................


查询未安装包的简单描述信息:
[root@hpf-linux ~]# rpm -qpi wireshark-1.8.10-8.el6_6.i686.rpm
Name        : wireshark                    Relocations: (not relocatable)
Version     : 1.8.10                            Vendor: CentOS
Release     : 8.el6_6                       Build Date: 2014年10月22日 星期三 07时49分43秒
Install Date: (not installed)               Build Host: c6b9.bsys.dev.centos.org
Group       : Applications/Internet         Source RPM: wireshark-1.8.10-8.el6_6.src.rpm
Size        : 50542085                         License: GPL+
Signature   : RSA/SHA1, 2014年10月22日 星期三 08时01分53秒, Key ID 0946fca2c105b9de
Packager    : CentOS BuildSystem < http://bugs.centos.org> URL         : http://www.wireshark.org/ Summary     : Network traffic analyzer
Description :
Wireshark is a network traffic analyzer for Unix-ish operating systems.
This package lays base for libpcap, a packet capture and filtering
library, contains command-line utilities, contains plugins and
documentation for wireshark. A graphical user interface is packaged
separately to GTK+ package.


五、rpm包的校验
1、检查包安装后生成的文件是否被修改过:
改变了zsh包所生成的文件/etc/skel/.zshrc ,而导致其大小、MD5校验码以及文件时间戳的改变。
[root@hpf-linux ~]# rpm -ql zsh       //查询zsh包生成的列表
/bin/zsh
/etc/skel/.zshrc
/etc/zlogin
/etc/zlogout
/etc/zprofile
................
[root@hpf-linux ~]# rpm -V zsh    //查看包是否发生改变,不输出结果就代表未改变
[root@hpf-linux ~]# echo "##########" >> /etc/skel/.zshrc       //改变文件
[root@hpf-linux ~]# tail -2 /etc/skel/.zshrc
#colors
##########
[root@hpf-linux ~]# rpm -V zsh
S.5....T.  c /etc/skel/.zshrc
通过输出结果发现zsh安装包发生改变,其改变的项目的意义为:
S file Size differs 大小发生改变
M Mode differs (includes permissions and file type) 头文件权限发生改变
5 digest (formerly MD5 sum) differs MD5码发生改变
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 能力改变了

通过改回文件的原先内容在使用校验时发现虽然其前两项变回来了,但是其时间戳已发生改变而无法改回。
[root@hpf-linux ~]# vim /etc/skel/.zshrc
[root@hpf-linux ~]# tail -2 /etc/skel/.zshrc
# autoload -U colors
#colors
[root@hpf-linux ~]# rpm -V zsh
.......T.  c /etc/skel/.zshrc


2、校验来源合法性和软件完整性:
校验包完整通过单向加密机制(MD5|sha1)来校验;
校验来源合法性通过公钥加密机制(RSA)校验;

通过-K选项和--checksig两个选项都能校验:
[root@hpf-linux ~]# rpm -K wireshark-1.8.10-8.el6_6.i686.rpm
wireshark-1.8.10-8.el6_6.i686.rpm: rsa sha1 (md5) pgp md5 OK
[root@hpf-linux ~]# rpm -K zsh-4.3.10-7.el6.i686.rpm
zsh-4.3.10-7.el6.i686.rpm: rsa sha1 (md5) pgp md5 OK
[root@hpf-linux ~]# rpm --checksig wireshark-1.8.10-8.el6_6.i686.rpm
wireshark-1.8.10-8.el6_6.i686.rpm: rsa sha1 (md5) pgp md5 OK
[root@hpf-linux ~]# rpm --checksig zsh-4.3.10-7.el6.i686.rpm
zsh-4.3.10-7.el6.i686.rpm: rsa sha1 (md5) pgp md5 OK


3、导入密钥:
可以使用--import选项导入rpm包的密钥,通常密钥是由包提供者来给与,本例是导入iso镜像中的包密钥。
[root@hpf-linux ~]# ls /media/
CentOS_BuildTag  isolinux                  RPM-GPG-KEY-CentOS-6           TRANS.TBL
EULA             Packages                  RPM-GPG-KEY-CentOS-Debug-6
GPL              RELEASE-NOTES-en-US.html  RPM-GPG-KEY-CentOS-Security-6
images           repodata                  RPM-GPG-KEY-CentOS-Testing-6
[root@hpf-linux ~]# rpm --import /media/RPM-GPG-KEY-CentOS-6


4、rpm包的数据库的初始化及重建:
数据库所在位置:
[root@hpf-linux ~]# ls /var/lib/rpm/
Basenames     __db.003     Group         Packages        Requirename     Triggername
Conflictname  __db.004     Installtid    Providename     Requireversion
__db.001      Dirnames     Name          Provideversion  Sha1header
__db.002      Filedigests  Obsoletename  Pubkeys         Sigmd5
数据库初始化:
命令选项 rpm --initdb
如果事先不存在一个数据库,则新建之
数据库重建:
命令选项 rpm --rebuilddb

直接新建数据库,会覆盖原有库

通过上面的简要介绍我们已经基本掌握rpm包及命令的使用,今后在使用时首先要考虑的是包来源合法及安全,剩下的通过本篇文章的简要描述来使用rpm命令帮助我们完成日常工作所需。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  linux rpm