Linux程序包管理命令

rpm命令的常见选项

rpm命令是RPM软件包的管理工具。rpm最初是Red Hat Linux发行版专用于管理Linux各项套件的开源程序。它可以用于给程序进行安装,升级,卸载,查询和校验等。

语法格式:rpm [OPTIONS] [PACKAGE_FILE]


1. 安装选项

命令格式:rpm {-i | --install} [install-options] PACKAGE_FILE ...

常见选项:

  • -v:显示详细信息;
  • -h:安装时显示进度条,单个‘#’表示2%进度;
  • --test:测试安装,此操作只用于模拟安装,用于检查包的依赖关系集冲突信息等;
  • --nodeps:安装时忽略依赖关系;相当于强制安装,不建议此操作;
  • --replacepkgs:重新安装
  • --nosignature:安装时不检查包签名的信息,也就是不检查包的来源合法性;
  • --nodigest:不检查包的完整性信息;
  • --noscripts:安装时不执行包的脚本片段;

    注意:rpm可以自带脚本执行;
    有四类:
    preinstall:安装过程开始前运行的 脚本,%pre,--nopre
    postinstall:安装过程完成后运行的脚本,%post,--nopost
    preuninstall:卸载过程真正开始执行前运行的脚本,%preun,--preun
    postuninstall:卸载过程完成后运行的脚本,%postun,--nopostun

示例

[root@elf Packages]# rpm -ivh apr-1.4.8-3.el7.x86_64.rpm 
准备中...               ################################# [100%]  ‘安装显示进度条’
    软件包 apr-1.4.8-3.el7_4.1.x86_64 (比 apr-1.4.8-3.el7.x86_64 还要新) 已经安装
    file /usr/lib64/libapr-1.so.0.4.8 from install of apr-1.4.8-3.el7.x86_64 conflicts with file from package apr-1.4.8-3.el7_4.1.x86_64
[root@elf Packages]# 

2.升级选项

命令格式:
rpm {-U | --upgrade} [install-options] PACKAGE_FILE ...
rpm {-F | --freshen} [install-options] PACKAGE_FILE ...
注意:-U和-F的区别在于U表示升级或安装,在升级过程中,如果没有检测到系统上的旧程序,U会直接进行安装需要升级的程序。而F则表示单纯的升级,若系统上没有需要升级的旧程序,F也并不会为之安装。

常见选项

  • -U:对指定程序进行升级或安装;
  • -F:对指定程序进行升级;
  • --oldpackage:对指定程序进行降级操作;
  • --force:对指定程序强制升级;

    注意:
    (1).尽量不要对内核进行升级操作,Linux支持多内核版本并存,因此,建议直接安装新本的内核。
    (2).如果某原程序包的配置文件安装后曾被修改过,升级时,新版本的程序包所提供的同一配置文件不会覆盖原有版本程序包的配置文件,新版本程序包配置文件会重命名提供(FILENAME.rpmnew);

示例

[root@elf Packages]# rpm -Uvh httpd-tools-2.4.6-45.el7.centos.x86_64.rpm 
准备中...   ################################# [100%] ‘v和h分别表示显示详细信息与进度条’
    软件包 httpd-tools-2.4.6-67.el7.centos.6.x86_64 (比 httpd-tools-2.4.6-45.el7.centos.x86_64 还要新) 已经安装

3.卸载选项

命令格式:
rpm {-e | --erase} [--allmatches] [--nodeps] [--noscripts] [--test]

常见选项

  • -e:直接卸载指定程序包;
  • --allmatches:卸载所有匹配到的指定名称的程序包的各版本;
  • --test:测试卸载,模拟卸载操作;

示例

[root@elf Packages]# rpm -evh wireshark-devel-1.10.14-10.el7.i686 --nodeps
准备中...                          ################################# [100%]
正在清理/删除...
   1:wireshark-devel-1.10.14-10.el7   ################################# [100%]

4.查询选项

命令格式:
rpm {-1 | --query } [select-options] [query-options]

常见选项
[select-options]

  • -a, --all:查询所有已经安装过的包;
  • -f, FILE:查询指定的文件由哪个程序包安装生成;
  • -p, --package:可对指定的未安装程序包执行查询操作;
  • --whatprovides CAPABILITY:查询指定的CAPABILITY由哪个程序包提供;
  • --whatprovides CAPABILITY:查询指定的CAPABILITY被哪个包所依赖;

[query-options]

  • --changelog:查询rpm包的changlog;
  • -l, --list:程序安装生成的所有文件列表;
  • -i, --info:程序包的详细信息,包含版本号,包大小,所属的包组等;
  • -c,--configfiles:查询指定程序包所提供的配置文件;
  • -d,--docfiles:查询指定程序包所提供的帮助文档;
  • -R,--requires:查询指定的程序包的依赖关系;
  • --provides:列出指定的程序包所提供的所有CAPABILITY;
  • --scripts:查看程序包自带的脚本片段;

示例

[root@elf ~]# rpm -qa     '查询系统上所有已安装的程序包'
dracut-network-033-463.el7.x86_64
filesystem-3.2-21.el7.x86_64
ebtables-2.0.10-15.el7.x86_64
bind-license-9.9.4-37.el7.noarch
libsoup-2.48.1-6.el7.x86_64
nss-softokn-freebl-3.16.2.3-14.4.el7.x86_64
NetworkManager-1.4.0-12.el7.x86_64
ncurses-libs-5.9-13.20130511.el7.x86_64
libestr-0.1.9-2.el7.x86_64
......
[root@elf ~]# rpm -qi plymouth   '查询指定程序包的详细信息’
Name        : plymouth
Version     : 0.8.9
Release     : 0.26.20140113.el7.centos
Architecture: x86_64
Install Date: 2018年03月03日 星期六 07时43分28秒
Group       : System Environment/Base
Size        : 232811
License     : GPLv2+
Signature   : RSA/SHA256, 2016年11月21日 星期一 04时06分35秒, Key ID 24c6a8a7f4a80eb5
Source RPM  : plymouth-0.8.9-0.26.20140113.el7.centos.src.rpm
Build Date  : 2016年11月15日 星期二 02时35分13秒
Build Host  : c1bm.rdu2.centos.org
Relocations : (not relocatable)
Packager    : CentOS BuildSystem <http://bugs.centos.org>
Vendor      : CentOS
URL         : http://www.freedesktop.org/wiki/Software/Plymouth
Summary     : Graphical Boot Animation and Logger
Description :
Plymouth provides an attractive graphical boot animation in
place of the text messages that normally get shown.  Text
messages are instead redirected to a log file for viewing
after boot.
[root@elf ~]# rpm -qc plymouth  ‘查询指定程序包的配置文件’
/etc/plymouth/plymouthd.conf
[root@elf ~]# rpm -ql plymouth   ‘查询指定程序包的帮助文档’
/etc/plymouth
/etc/plymouth/plymouthd.conf
/usr/bin/plymouth
/usr/lib/systemd/system/halt.target.wants
/usr/lib/systemd/system/halt.target.wants/plymouth-halt.service
/usr/lib/systemd/system/initrd-switch-root.target.wants
/usr/lib/systemd/system/initrd-switch-root.target.wants/plymouth-start.service
......
[root@elf ~]# rpm -qf /var/spool/plymouth  ‘查询指定的文件由哪个程序包所提供’
plymouth-0.8.9-0.26.20140113.el7.centos.x86_64
[root@elf Packages]# rpm -qpl zziplib-utils-0.13.62-5.el7.x86_64.rpm   ‘查询未安装程序包所提供的所有文件列表’
/usr/bin/unzip-mem
/usr/bin/unzzip
/usr/bin/unzzipcat
/usr/bin/unzzipcat-mem
/usr/bin/unzzipcat-seeko
/usr/bin/unzzipdir
/usr/bin/unzzipdir-mem
/usr/bin/zzcat
/usr/bin/zzdir
/usr/bin/zzxorcat
/usr/bin/zzxorcopy
/usr/bin/zzxordir

5.校验选项

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

[select-options]

  • -p:Verify against package file
  • -f:校验所属的软件包
  • -a:校验所有的软件包
  • -g:校验所有属于组的软件包

[verify-options]

  • --noscripts 不运行校验脚本
  • --nodeps 不校验依赖性
  • --nofiles 不校验文件属性

常见参数

  • 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

    rpm程序包的合法性验证

    通常我们在得到一个rpm包的时候,时常会去考虑该程序包是否会对我们的计算机造成影响,是否含有hacker植入的病毒呢?为了保证我们到手的程序包是安全的,这时我们就有必要对程序包进行合法性验证了,恰好我们的linux上就有这种验证的机制。

验证两步骤:

  1. 获取并导入程序包制作者的密钥 (只针对于CentOS)
    rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
  2. 验证
    rpm -K PACKAGE_FILE

    rpm包数据库重建

    还记得我们的rpm包的查询命令吗?对,就是我们的‘rpm -q’命令啦。有这样一个问题,我们存储rpm包名称的地方在哪儿呢,rpm包查询到的详细信息又是从哪儿来呢?很简单,我们Linux上有一个rpm包的数据库,每当我们系统上减少或增加一个程序包时,数据库就会更新条目并记录 ,这样看来,rpm包查询时是不是就很方便了呢?

rpm管理器数据库路径:/var/lib/rpm/

注意:Centos6和centos7获取rpm管理器数据库相关命令手册的命令不同
CentOS 6:man rpm
CentOS 7:man rpmdb

命令格式:
rpm {--initdb | --rebuilddb} [--dbpath DIRECTORY] [--root DIRECTORY]

--initdb:初始化数据库,当前无任何数据库可初始化创建一个新的;当前有时不执行任何操作;
--rebuilddb:重新构建,通过读取当前系统上所有已经安装过的程序包进行重新创建;


yum命令的常见选项

yum命令可用于添加,删除,更新rpm包,它最大的优势在于可自动解决包的依赖问题,可自行处理包的依赖关系。

1.yum仓库配置

配置文件

  • /etc/yum.conf:为所有仓库提供公共配置
  • /etc/yum.repos.d/d*.repo:为仓库的指向提供配置

配置yum仓库

[base]       #仓库ID,ID必须保证唯一性;
name        #仓库名称,可以随意命名;
mirrorlist   #此处地址下包含了仓库地址列表,包含一个或多个镜像站点,和baseurl选择其中一个就行;
baseurl      #仓库地址。网络上的地址写网络地址,本地地址则写本地地址;
gpgcheck  #说明是否需要gpg签名,1表示需要,0表示不需要;
gpgkey      #数字签名的文件路径
enable       #表示该仓库是否开启,1表示开启,0表示关闭;
cost           #路径的开销,开销越高,优先级就越低;默认为100;

2.yum命令用法

语法格式:
yum [options] [command] [package ...]

命令列表

* help   命令的帮助信息,用法:yum help command;
* clean   清除缓存数据,如yum clean all;
* makecache   生成元数据缓存数据,yum makecache;
* deplist   列出包的依赖关系;
* erase   卸载包;
* fs   为当前系统创建快照,或者列出或删除当前已有快照。快照非常具有实用性,在给程序包进行升级或打补丁前做个快照,就可以安安心心地做快照了;
* groups   对包组进行操作;
* history   对yum事务信息进行查看;
* info   输出包或包组的详细信息;
* install   安装指定程序包;
* list   列出包名,可配合与grep命令一起使用;
* provides   搜索给定的内容有谁提供,可用来搜索来源于哪个包;
* reinstall   重新安装程序包;
* repolist   列出可用的仓库列表;
* search   给定字符串搜索相关包,并给出相关包较为详细的信息;
* update   更新指定包;

Options:
-R [minutes], --randomwait=[minutes]:最多等待时间;
-q, --quiet:安静模式;
-v, --verbose:详细模式;
-y,--assumeyes:对安装过程中出现的所有问题都回答为yes;
--assumeno:对安装过程中出现的所有问题都回答为no;
--enablerepo=[repo]:启用一个多个仓库;
--disablerepo=[repo]:禁用一个或多个仓库;
-x [package], --exclude=[package]:通配要排除的包;
--nogpgcheck:禁用gpgcheck;
--color=COLOR:高亮显示,选其指定颜色;
--downloadnoly:仅下载包,不安装或升级。默认下载在yum的缓存目录中,默认为/var/cache/yum/$basearch/$releasever
--downloaddir=DLDIR:指定下载目录;

示例

[root@elf ~]# yum repolist all   '列出所有仓库'
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
源标识                                     源名称                                     状态
base/7/x86_64                              CentOS-7 - Base                            启用:  9,911
centosplus/7/x86_64                        CentOS-7 - Plus                            禁用
epel/x86_64                                epel                                       启用: 12,503
extras/7/x86_64                            CentOS-7 - Extras                          启用:    258
local                                      local                                      启用:  9,363
updates/7/x86_64                           CentOS-7 - Updates                         启用:    143
repolist: 32,178
[root@elf ~]# yum install httpd   '安装程序包,并自动解决包的依赖关系'
已加载插件:fastestmirror
base                                                                       | 3.6 kB  00:00:00     
epel                                                                       | 4.7 kB  00:00:00     
extras                                                                     | 3.4 kB  00:00:00     
local                                                                      | 3.6 kB  00:00:00     
updates                                                                    | 3.4 kB  00:00:00     
(1/4): epel/x86_64/updateinfo                                              | 779 kB  00:00:03     
(2/4): base/7/x86_64/primary_db                                            | 5.9 MB  00:00:10     
(3/4): epel/x86_64/primary_db                                              | 6.0 MB  00:00:11     
(4/4): base/7/x86_64/group_gz                                              | 166 kB  00:00:16     
Determining fastest mirrors
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-80.el7.centos 将被 安装
--> 正在处理依赖关系 httpd-tools = 2.4.6-80.el7.centos,它被软件包 httpd-2.4.6-80.el7.centos.x86_64 需要
--> 正在检查事务
---> 软件包 httpd-tools.x86_64.0.2.4.6-67.el7.centos.6 将被 升级
---> 软件包 httpd-tools.x86_64.0.2.4.6-80.el7.centos 将被 更新
--> 解决依赖关系完成

依赖关系解决

==================================================================================================
 Package                架构              版本                              源               大小
==================================================================================================
正在安装:
 httpd                  x86_64            2.4.6-80.el7.centos               base            2.7 M
为依赖而更新:
 httpd-tools            x86_64            2.4.6-80.el7.centos               base             89 k

事务概要
==================================================================================================
安装  1 软件包
升级           ( 1 依赖软件包)

总下载量:2.8 M
Is this ok [y/d/N]: y
00:00:55 
[root@elf ~]# yum update vim   '更新程序包'
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 vim-enhanced.x86_64.2.7.4.160-2.el7 将被 升级
---> 软件包 vim-enhanced.x86_64.2.7.4.160-4.el7 将被 更新
--> 正在处理依赖关系 vim-common = 2:7.4.160-4.el7,它被软件包 2:vim-enhanced-7.4.160-4.el7.x86_64 需要
--> 正在检查事务
---> 软件包 vim-common.x86_64.2.7.4.160-2.el7 将被 升级
......
[root@elf ~]# yum update vim
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 vim-enhanced.x86_64.2.7.4.160-2.el7 将被 升级
---> 软件包 vim-enhanced.x86_64.2.7.4.160-4.el7 将被 更新
--> 正在处理依赖关系 vim-common = 2:7.4.160-4.el7,它被软件包 2:vim-enhanced-7.4.160-4.el7.x86_64 需要
--> 正在检查事务
---> 软件包 vim-common.x86_64.2.7.4.160-2.el7 将被 升级
[root@elf ~]# yum update vim
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
正在解决依赖关系
--> 正在检查事务
---> 软件包 vim-enhanced.x86_64.2.7.4.160-2.el7 将被 升级
---> 软件包 vim-enhanced.x86_64.2.7.4.160-4.el7 将被 更新
--> 正在处理依赖关系 vim-common = 2:7.4.160-4.el7,它被软件包 2:vim-enhanced-7.4.160-4.el7.x86_64 需要
--> 正在检查事务
---> 软件包 vim-common.x86_64.2.7.4.160-2.el7 将被 升级
[root@elf ~]# yum remove httpd   '卸载程序包’
已加载插件:fastestmirror
正在解决依赖关系
--> 正在检查事务
---> 软件包 httpd.x86_64.0.2.4.6-80.el7.centos 将被 删除
--> 解决依赖关系完成

依赖关系解决

==================================================================================================
 Package            架构                版本                             源                  大小
==================================================================================================
正在删除:
 httpd              x86_64              2.4.6-80.el7.centos              @base              9.4 M

事务概要
==================================================================================================
移除  1 软件包

安装大小:9.4 M
是否继续?[y/N]:y
Downloading packages:
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
  正在删除    : httpd-2.4.6-80.el7.centos.x86_64                                              1/1 
  验证中      : httpd-2.4.6-80.el7.centos.x86_64                                              1/1 
删除:
  httpd.x86_64 0:2.4.6-80.el7.centos                                                              
完毕!
[root@elf ~]# yum info zziplib   '查看指定程序包的详细信息'
已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: mirrors.aliyun.com
 * extras: centos.ustc.edu.cn
 * updates: mirrors.aliyun.com
可安装的软件包
名称    :zziplib
架构    :i686
版本    :0.13.62
发布    :5.el7
大小    :82 k
源    :base/7/x86_64
简介    : Lightweight library to easily extract data from zip files
网址    :http://zziplib.sourceforge.net/
协议    : LGPLv2+ or MPLv1.1
描述    : The zziplib library is intentionally lightweight, it offers the ability to
         : easily extract data from files archived in a single zip file. Applications
         : can bundle files into a single zip archive and access them. The implementation
         : is based only on the (free) subset of compression with the zlib algorithm
         : which is actually used by the zip/unzip tools.

yum命令操作包组

命令格式
yum [ group( list | info | summary | install | upgrade | remove | mark ) ] [GROUP]

示例

[root@elf ~]# yum groupinfo Haskell   '查看指定包组的信息‘
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn

组:Haskell
 组编号:haskell
 描述:对用 Haskell 编程语言开发程序的支持。
 默认的软件包:
   +darcs
   +ghc
   +ghc-rpm-macros
   +haskell-platform
   +hscolour
 可能的软件包:
   +ghc-gtk-devel
[root@elf ~]# yum groupinstall haskell   ’安装指定程序包组‘
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
......
[root@elf ~]# yum groupremove haskell   '移除指定的程序包组’
已加载插件:fastestmirror
没有安装组信息文件
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: centos.ustc.edu.cn
No environment named haskell exists
Maybe run: yum groups mark remove (see man yum)
......
3.案例

使用光盘用作本地仓库

[root@elf Packages]# mount -r -t iso9660 /dev/sr0 /mnt/cdrom   '挂载光盘‘
[root@elf ~]# cd /etc/yum.repos.d/
[root@elf yum.repos.d]# vim cd.repo   '配置yum仓库'
[CDROM]
name=cd
baseurl=file:///mnt/cdrom
gpgcheck=0
enabled=1
[root@elf yum.repos.d]# yum clean all   '清楚缓存'
已加载插件:fastestmirror
正在清理软件源: CDROM base epel extras local updates
Cleaning up everything
Cleaning up list of fastest mirrors
[root@elf yum.repos.d]# yum makecache   '再建立缓存'
[root@elf yum.repos.d]# yum repolist all   '查看创建的仓库是否生成'
[已加载插件:fastestmirror
Loading mirror speeds from cached hostfile
 * base: centos.ustc.edu.cn
 * extras: centos.ustc.edu.cn
 * updates: mirrors.163.com
源标识                                         源名称                                        状态
CDROM                                          cd                                            启用:  9,363
base/7/x86_64                                  CentOS-7 - Base                               启用:  9,911
centosplus/7/x86_64                            CentOS-7 - Plus                               禁用
epel/x86_64                                    epel                                          启用: 12,505
extras/7/x86_64                                CentOS-7 - Extras                             启用:    291
local                                          local                                         启用:  9,363
updates/7/x86_64                               CentOS-7 - Updates                            启用:    539
repolist: 41,972

猜你喜欢

转载自blog.51cto.com/12598964/2118923