1. 程式人生 > >Linux程序包管理命令

Linux程序包管理命令

rpm yum

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

Linux程序包管理命令