Linux下RPM命令参数使用详解
一、什么是rpm
RPM 是Red hat Package Manager 的缩写,本意是Red Hat软件包管理,由RedHat开发出来的一种软件包管理程序,后来被LSB(Linux规范标准)会正式吸纳为Linux的标准包格式,命名为RPM Package Manager,现在所说的RPM即使 RPM Package Manager的缩写。
二、为什么要使用RPM软件包管理器
RPM软件包管理器简化了用户在Linux系统上对软件进行安装、卸载、升级或更新的过程,只需要简短的命令就可以完成,从而省去了对对源代码进行编译、安装的复杂过程,从而大大提高了管理人员的工作效率
三、RPM命名格式
命名格式:
appname-VERSION-RELEASE.ARCH.rpm
VERSION:
magior:主版本号
minor:次版本号
release:发行号
RELEASE: 包自身的修订号,有时候会包含适用的OS信息:
eg: bash-4.3.2-2.centos6.x86_64.rpm中 2.centos6 就是RELEASE号
ARCH:适用平台
x86: i386, i486, i586, i686
x86_64: x86_64, amd64
powerpc: ppc
noarch: 跟平台无关;(perl,python,ruby等编译的程序)
RPM分包机制:
核心包,主包:命名与源项目名称一致;
eg: bash-4.3.2-2.centos6.x86_64.rpm
子包(支包):命令为源项目名称后附加支包中的文件提供的功能组成
eg: bash-devel-4.3.2-2.centos6.x86_64.rpm
RPM包获取途径
1、系统的发行光盘镜像或官方站点
2、程序包官方站点
3、第三方组织:epel
4、搜索RPM包的搜索引擎
http://rpmfind.net
http://rpm.pbone.net
http://pkgs.org
建议:生产过程中对rpm软件包安装之前需要验证程序包的来源合法性及包的完整性
四、如何使用RPM软件包管理器
rpm - RPM Package Manager 可以对软件进行安装、升级、卸载、查询、效验等功能,下面将对其进行逐一讲解:
1、安装
安装语法格式:
rpm {-i|--install} [install-options] PACKAGE_FILE1...
安装时常用选项:
-h: hash,以#来表示安装进度;每个# 号表示2%的安装进度
-v, --verbose:显示安装过程中的详细信息;
-vv: 能显示更加详细信息
-vvv:
--test:测试安装,不执行真正的安装过程,而仅报告依赖关系及冲突信息等;
--nodeps : 忽略依赖关系安装,【能安装成功,但未必能成功运行;】
--replacepkgs:覆盖安装--重新安装并覆盖原有的文件
--force: 强制安装
--olepackage:降级到旧版本
--relocate 指明安装位置
--replacefiles 指明安装时替换某个文件
--replacepkgs 指明安装时替换整个包
安装时常用组合: -ivh --ivvh
2、升级:
升级语法格式:
rpm {-U|--upgrade} [install-options] PACKAGE_FILE ...
-U: 升级或安装
rpm {-F|--freshen} [install-options] PACKAGE_FILE ...
-F:升级 【只进行升级】
升级常用选项
-v, --verbose:显示升级过程中的详细信息;
-vv: 能显示更加详细信息
-vvv:
--test:测试升级安装,不执行真正的升级安装过程,而仅报告依赖关系及冲突信息等;
--nodeps : 忽略依赖关系进行升级,
--force: 强制升级
--olepackage:降级到旧版本
升级时常用组合: -Uvh --Uvvh
注意1:一定不要对内核执行升级;Linux允许多内核共存,所以,可以直接安装多个不同版本内核;
注意2:如果程序包的配置文件安装后曾被修改,升级时,新版本的文件不会覆盖老版本的配置文件,而把新版本的配置文件重命名(加后缀.rpmnew)后保存;
例 使用-i选项安装zsh [root@1inux Packages]# rpm -i zsh-4.3.10-7.el6.x86_64.rpm warning: zsh-4.3.10-7.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY [root@1inux Packages]# 使用-v 显示详细安装过程 [root@1inux Packages]# rpm -e zsh //卸载zsh [root@1inux Packages]# rpm -ivh zsh-4.3.10-7.el6.x86_64.rpm //使用-v详细显示安装过程 warning: zsh-4.3.10-7.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] 1:zsh ########################################### [100%] 使用--test测试安装zsh [root@1inux Packages]# rpm -e zsh //卸载zsh [root@1inux Packages]# rpm -ivh --test zsh-4.3.10-7.el6.x86_64.rpm //测试安装zsh warning: zsh-4.3.10-7.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] [root@1inux Packages]# rpm -q zsh //查询是否安装 package zsh is not installed [root@1inux Packages]#
例: --nodeps的使用 [root@1inux Packages]# rpm -q php //查询是否安装了php程序 package php is not installed //结果显示没有安装 [root@1inux Packages]# rpm -ivh php-5.3.3-38.el6.x86_64.rpm //进行PHP安装 warning: php-5.3.3-38.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY error: Failed dependencies: //提示无法安装,需要安装依赖包php-cli及php-common php-cli(x86-64) = 5.3.3-38.el6 is needed by php-5.3.3-38.el6.x86_64 php-common(x86-64) = 5.3.3-38.el6 is needed by php-5.3.3-38.el6.x86_64 [root@1inux Packages]# rpm -ivh --nodeps php-5.3.3-38.el6.x86_64.rpm //忽略依赖关系安装 warning: php-5.3.3-38.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] 1:php ########################################### [100%] [root@1inux Packages]# rpm -q php //查询是否成功安装了php程序 php-5.3.3-38.el6.x86_64 [root@1inux Packages]# //注意:忽略依赖关系进行安装后即使能成功安装程序,也有可能程序无法正常使用
例:--replacepkgs 覆盖安装的使用 [root@1inux Packages]# rpm -ivh zsh-4.3.10-7.el6.x86_64.rpm //安装zsh warning: zsh-4.3.10-7.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] package zsh-4.3.10-7.el6.x86_64 is already installed //显示已经安装过了,无法进行安装 [root@1inux Packages]# rpm -ivh --replacepkgs zsh-4.3.10-7.el6.x86_64.rpm //进行覆盖安装 warning: zsh-4.3.10-7.el6.x86_64.rpm: Header V3 RSA/SHA1 Signature, key ID c105b9de: NOKEY Preparing... ########################################### [100%] 1:zsh ########################################### [100%] [root@1inux Packages]#
3、卸载:移除已经安装的程序包
卸载语法格式:
rpm {-e|--erase} [--allmatches] [--nodeps] [--test] PACKAGE_NAME ...
简单用法:rpm -e PACKAGE_NAME...
卸载时常用选项:
--nodeps:忽略依赖关系;
--test: 测试卸载;dry-run模式;
--allmatches: 如果一个程序包同时安装多个版本,则此选项一次全部卸载之;
注意:如果程序包的配置文件安装后曾被修改,卸载时,此文件通常不会被删除,而是被重命名(加后缀.rpmsave)后留存;
eg:卸载zsh [root@1inux Packages]# rpm -q zsh //查询是否安装zsh zsh-4.3.10-7.el6.x86_64 [root@1inux Packages]# rpm -e zsh //卸载zsh warning: /etc/zshrc saved as /etc/zshrc.rpmsave //由于之前我对/etc/zshrc文件做了更改所以卸载时被重命名保存下来 [root@1inux Packages]# rpm -q zsh //验证是否卸载成功 package zsh is not installed [root@1inux Packages]# [root@1inux Packages]# ls /etc/zsh* /etc/zshrc.rpmsave [root@1inux Packages]#
4、查询:
查询某包是否已经安装,以及检查系统安装的所有包;还可以查看某包的详细信息;
RPM查询语法格式:
rpm {-q|--query} [select-options] [query-options]
[select-options]:
⑴、查询某包或某些包是否安装:
# rpm -q PACKAGE_NAME...
[root@1inux Packages]# rpm -q zsh zsh-4.3.10-7.el6.x86_64
⑵、查询已经安装的所有包:
# rpm -qa
root@1inux Packages]# rpm -qa |more 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 dejavu-fonts-common-2.30-2.el6.noarch kernel-firmware-2.6.32-504.el6.noarch time-1.7-37.1.el6.x86_64 PackageKit-yum-0.5.8-23.el6.x86_64 cjkuni-fonts-common-0.2.20080216.1-36.el6.noarch totem-2.28.6-4.el6.x86_64 mesa-private-llvm-3.4-3.el6.x86_64 pycairo-1.8.6-2.1.el6.x86_64 dmz-cursor-themes-0.4-4.el6.noarch ...
⑶、查询某文件是由哪个包安装生成:
# rpm -qf /PATH/TO/SOMEFILE
[root@1inux Packages]# rpm -qf /etc/zshrc //查询 /etc/zshrc文件是由哪个程序安装产生的 zsh-4.3.10-7.el6.x86_64 [root@1inux Packages]#
⑷、查询尚未安装的包文件的相关信息
-p
# rpm -qpi PACKAGE_FILE
[root@1inux Packages]# rpm -qpi zip-3.0-1.el6.x86_64.rpm warning: zip-3.0-1.el6.x86_64.rpm: Header V3 RSA/SHA256 Signature, key ID c105b9de: NOKEY Name : zip Relocations: (not relocatable) Version : 3.0 Vendor: CentOS Release : 1.el6 Build Date: Thu 11 Nov 2010 11:26:40 PM CST Install Date: (not installed) Build Host: c6b3.bsys.dev.centos.org Group : Applications/Archiving Source RPM: zip-3.0-1.el6.src.rpm Size : 823612 License: BSD Signature : RSA/8, Sun 03 Jul 2011 01:07:05 PM CST, Key ID 0946fca2c105b9de Packager : CentOS BuildSystem <http://bugs.centos.org> 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.
[query-options]:
⑴、查询某包的简要说明信息:
# rpm -qi PACKAGE_NAME
[root@1inux Packages]# rpm -qi zsh //查询zsh包的简要信息 Name : zsh Relocations: (not relocatable) Version : 4.3.10 Vendor: CentOS Release : 7.el6 Build Date: Mon 25 Nov 2013 01:40:59 AM CST Install Date: Sun 12 Apr 2015 03:44:34 PM CST Build Host: c6b9.bsys.dev.centos.org Group : System Environment/Shells Source RPM: zsh-4.3.10-7.el6.src.rpm Size : 5009102 License: BSD Signature : RSA/SHA1, Mon 25 Nov 2013 03:33:46 AM CST, 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.
⑵、查询某包安装生成的文件列表:
# rpm -ql PACKAGE_NAME
[root@1inux Packages]# rpm -ql zsh //查询zsh安装后所生成的所有文件列表 /bin/zsh /etc/skel/.zshrc /etc/zlogin /etc/zlogout /etc/zprofile /etc/zshenv /etc/zshrc /usr/lib64/zsh /usr/lib64/zsh/4.3.10 /usr/lib64/zsh/4.3.10/zsh /usr/lib64/zsh/4.3.10/zsh/attr.so /usr/lib64/zsh/4.3.10/zsh/cap.so /usr/lib64/zsh/4.3.10/zsh/clone.so /usr/lib64/zsh/4.3.10/zsh/compctl.so /usr/lib64/zsh/4.3.10/zsh/complete.so /usr/lib64/zsh/4.3.10/zsh/complist.so /usr/lib64/zsh/4.3.10/zsh/computil.so .....
⑶、查询某包安装完成后生成的所有配置文件:
# rpm -qc PACKAGE_NAME
[root@1inux Packages]# rpm -qc zsh //查询zsh包安装后所生产的所有配置文件 /etc/skel/.zshrc /etc/zlogin /etc/zlogout /etc/zprofile /etc/zshenv /etc/zshrc [root@1inux Packages]#
⑷、查询某包安装完成后生成的所有帮助文件:
# rpm -qd PACKAGE_NAME
[root@1inux Packages]# rpm -qd zsh //查询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 /usr/share/doc/zsh-4.3.10/LICENCE /usr/share/doc/zsh-4.3.10/MACHINES /usr/share/doc/zsh-4.3.10/NEWS /usr/share/doc/zsh-4.3.10/README /usr/share/doc/zsh-4.3.10/completion-style-guide /usr/share/doc/zsh-4.3.10/zsh-development-guide /usr/share/doc/zsh-4.3.10/zshprompt.pl /usr/share/info/zsh.info-1.gz /usr/share/info/zsh.info-2.gz /usr/share/info/zsh.info-3.gz ...
⑸、查看某包制作时随版本变化的changelog信息:
# rpm -q --changelog PACKAGE_NAME
[root@1inux Packages]# rpm -q --changelog zsh //查询zsh随版本变化的log信息 * Mon Aug 05 2013 James Antill <[email protected]> - 4.3.10-7 - Change {NAME:OFFSET:LENGTH} substitution feature to ignore KSH_ARRAYS option. - Fixup tests. - Resolves: rhbz#820530 * Tue Jun 25 2013 James Antill <[email protected]> - 4.3.10-6 - Add {NAME:OFFSET:LENGTH} substitution feature. - Resolves: rhbz#820530 * Fri Mar 02 2012 James Antill <[email protected]> - 4.3.10-5 - Change invocation as "zsh foo" to search the path by default. - Add new option PATH_SCRIPT for old behaviour. - Resolves: rhbz#612685 - Solve defect in emulation of ksh with math substituion. - Resolves: rhbz#657300 ...
⑹、查询某包提供的capabilities:
# rpm -q --provides PACKAGE_NAME
[root@1inux Packages]# rpm -q --provides zsh attr.so()(64bit) cap.so()(64bit) clone.so()(64bit) compctl.so()(64bit) complete.so()(64bit) complist.so()(64bit) computil.so()(64bit) config(zsh) = 4.3.10-7.el6 curses.so()(64bit) datetime.so()(64bit) deltochar.so()(64bit) ....
⑺、查询某包所依赖的capabilities:
# rpm -q --requires PACKAGE_NAME
[root@1inux Packages]# rpm -q --requires zsh /bin/sh /bin/sh /bin/sh /bin/sh /bin/zsh /sbin/install-info /sbin/install-info config(zsh) = 4.3.10-7.el6 coreutils grep grep 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) ....
⑻、查询某包安装或卸载时执行脚本:
# rpm -q --scripts PACKAGE_NAME
脚本包含四类内容:
preinstall: 安装过程开始之前执行的脚本;
postinstall: 安装过程完成之后执行的脚本;
preuninstall: 卸载开始之前执行的脚本 ;
postuninstall: 卸载过程完成之后执行的脚本;
[root@1inux Packages]# rpm -q --scripts zsh postinstall scriptlet (using /bin/sh): //安装过程完成之后执行的脚本; if [ ! -f /etc/shells ] ; then echo "/bin/zsh" > /etc/shells else grep -q "^/bin/zsh$" /etc/shells || echo "/bin/zsh" >> /etc/shells fi if [ -f /usr/share/info/zsh.info.gz ]; then # This is needed so that --excludedocs works. /sbin/install-info /usr/share/info/zsh.info.gz /usr/share/info/dir --entry="* zsh: (zsh). An enhanced bourne shell." fi : preuninstall scriptlet (using /bin/sh): //卸载开始之前执行的脚本 ; if [ "$1" = 0 ] ; then if [ -f /usr/share/info/zsh.info.gz ]; then # This is needed so that --excludedocs works. /sbin/install-info --delete /usr/share/info/zsh.info.gz /usr/share/info/dir --entry="* zsh: (zsh). An enhanced bourne shell." fi fi : postuninstall scriptlet (using /bin/sh): //卸载过程完成之后执行的脚本; if [ "$1" = 0 ] ; then if [ -f /etc/shells ] ; then TmpFile=`/bin/mktemp /tmp/.zshrpmXXXXXX` grep -v ‘^/bin/zsh$‘ /etc/shells > $TmpFile cp -f $TmpFile /etc/shells rm -f $TmpFile fi fi [root@1inux Packages]#
5、校验:
查询包安装之后生成的文件是否发生了改变
效验语法格式
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
eg: [root@1inux Packages]# vim /etc/zshrc //编辑/etc/zshrc文件 # # /etc/zshrc is sourced in interactive shells. It # should contain commands to set up aliases, functions, # options, key bindings, etc. # # This is a test //此处我们增加一行 [root@1inux Packages]# rpm -V zsh //查询安装后生成的文件是否发生了变化 S.5....T. c /etc/zshrc //显示第五行及Time发生变化 [root@1inux Packages]#
6、RPM管理器数据库:
重建数据库:
rpm {--initdb|--rebuilddb} [-v] [--dbpath DIRECTORY]
--initdb: 初始化数据库,即数据库完全不存时,可新建之;
--rebuilddb: 无论当前数据存在与否,都会直接重建此库;
本文出自 “无常” 博客,请务必保留此出处http://1inux.blog.51cto.com/10037358/1631569
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。