Linux 中的 RPM 命令與示例

RPM(Redhat Package Manager)是一個命令行包管理實用程序,用於安裝、卸載、更新、查詢和驗證軟件包。

在本教程中,我將解釋一些最常見的 Linux 系統 rpm 命令 admin. 一些基於 RPM 的 Linux 發行版是 Redhat、Centos、 Fedora,和 OpenSuse。 Yum 和 DNF 是 RPM 管理的前端,它們具有更多功能並且可以克服其局限性。

如何獲取 RPM 包

有多種方法可以獲取 RPM 包,如下所示。

  1. 軟件 CD/DVD
  2. CentOS 鏡像
  3. RedHat(需要賬號)
  4. 任何開放的存儲庫網站

RPM 命令的基本模式

  • 安裝 : 用於安裝任何 RPM 包。
  • 消除 :它用於擦除、刪除或卸載任何 RPM 包。
  • 升級 : 用於更新現有的 RPM 包。
  • 詢問 : 用於查詢任何 RPM 包。
  • 核實 : 用於驗證RPM包。

RPM 包示例

GeoIP 應用 GeoIP-1.5.0-11.el7.x86_64.rpm 是一個用於國家/城市/組織到 IP 地址或主機名映射的 RPM 包庫。 GeoIP 是包名,版本是 1.5.0-11.el7,釋放是 7 和建築 .x86_64.

安裝

1.如何在不安裝的情況下知道RPM包的信息

在我們下載了一個包之後,我們想在安裝之前知道一個包的信息。 我們可以用 -qip選項(查詢信息包)列出包的信息。

$ sudo rpm -qip GeoIP-1.5.0-11.el7.x86_64.rpm

output
Name : GeoIP
Version : 1.5.0
Release : 11.el7
Architecture: x86_64
Install Date: (not installed)
Group : Development/Libraries
Size : 2905020
License : LGPLv2+ and GPLv2+ and CC-BY-SA
Signature : RSA/SHA256, Sun 20 Nov 2016 05:49:19 PM UTC, Key ID 24c6a8a7f4a80eb5
Source RPM : GeoIP-1.5.0-11.el7.src.rpm
Build Date : Sat 05 Nov 2016 08:29:17 PM UTC
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem
Vendor : CentOS
URL : https://www.maxmind.com/app/c
Summary : Library for country/city/organization to IP address or hostname mapping
Description :
GeoIP is a C library that enables the user to find the country that any IP
address or hostname originates from. It uses a file based database that is
accurate as of June 2007 and can optionally be updated on a weekly
basis by installing the GeoIP-update package. This database simply contains IP
blocks as keys, and countries as values. This database should be more complete
and accurate than using reverse DNS lookups.

This package includes GeoLite data created by MaxMind, available from
https://www.maxmind.com/

2.如何安裝RPM包

我們可以用 -ivh 安裝特定包的參數,如下所示。

$ sudo rpm -ivh GeoIP-1.5.0-11.el7.x86_64.rpm

Output
Preparing... ################################# [100%]
package GeoIP-1.5.0-11.el7.x86_64 is already installed

3. 如何檢查已安裝的 RPM 包

我們可以用 -q 帶有包名的選項,它將顯示是否安裝了 rpm 包。

$ sudo rpm -q GeoIP

output
GeoIP-1.5.0-11.el7.x86_64

4. 如何列出特定安裝的 RPM 包的所有文件

我們可以列出已安裝的 rpm 包的所有文件,通過使用 -ql rpm 命令的選項。

$ sudo rpm -ql GeoIP

output
/etc/GeoIP.conf
/etc/GeoIP.conf.default
/usr/bin/geoiplookup
/usr/bin/geoiplookup6
/usr/bin/geoipupdate
/usr/lib64/libGeoIP.so.1
/usr/lib64/libGeoIP.so.1.5.0
/usr/lib64/libGeoIPUpdate.so.0
/usr/lib64/libGeoIPUpdate.so.0.0.0
/usr/share/GeoIP
/usr/share/GeoIP/GeoIP-initial.dat
/usr/share/GeoIP/GeoIP.dat
/usr/share/GeoIP/GeoIPASNum.dat
/usr/share/GeoIP/GeoIPASNumv6.dat
/usr/share/GeoIP/GeoIPCity.dat
/usr/share/GeoIP/GeoIPCityv6.dat
/usr/share/GeoIP/GeoIPCountry.dat
/usr/share/GeoIP/GeoIPCountryv6.dat
/usr/share/GeoIP/GeoIPv6-initial.dat
...

5.如何列出最近安裝的RPM包

我們可以用 -qa 選項與 --last, 它將列出所有最近安裝的 rpm 包。

$ sudo rpm -qa --last

output
GeoIP-1.5.0-11.el7.x86_64 Sat 01 Sep 2018 11:34:09 AM UTC
wget-1.14-15.el7_4.1.x86_64 Sun 26 Aug 2018 03:21:02 PM UTC
iwl7265-firmware-22.0.7.0-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:18 PM UTC
libgomp-4.8.5-28.el7_5.1.x86_64 Thu 16 Aug 2018 02:10:15 PM UTC
iwl2030-firmware-18.168.6.1-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:15 PM UTC
iptables-1.4.21-24.1.el7_5.x86_64 Thu 16 Aug 2018 02:10:15 PM UTC
yum-plugin-fastestmirror-1.1.31-46.el7_5.noarch Thu 16 Aug 2018 02:10:14 PM UTC
iwl6000-firmware-9.221.4.1-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:14 PM UTC
iwl4965-firmware-228.61.2.24-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:14 PM UTC
iwl105-firmware-18.168.6.1-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:14 PM UTC
iwl100-firmware-39.31.5.1-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:13 PM UTC
iwl1000-firmware-39.31.5.1-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:13 PM UTC
ca-certificates-2018.2.22-70.0.el7_5.noarch Thu 16 Aug 2018 02:10:13 PM UTC
iwl6000g2b-firmware-17.168.5.2-62.2.el7_5.noarch Thu 16 Aug 2018 02:10:12 PM UTC
...

6.如何安裝無依賴的RPM包

我們可以用 -ivh 參數與 --nodeps 代表沒有依賴項檢查,安裝一個沒有依賴項的特定包,如下所示。

$ sudo rpm -ivh --nodeps GeoIP-1.5.0-11.el7.x86_64.rpm

Output
Preparing... ################################# [100%]
package GeoIP-1.5.0-11.el7.x86_64 is already installed

7.如何更換安裝的RPM包

我們可以用 -ivh --replacepkgs 參數來替換安裝的特定包,如下所示。

$ sudo rpm -ivh --replacepkgs GeoIP-1.5.0-11.el7.x86_64.rpm

Output
Preparing... ################################# [100%]
Updating / installing...
1:GeoIP-1.5.0-11.el7 ################################# [100%]

消除

8.如何卸載RPM包

我們可以用 -e 卸載沒有依賴項安裝的特定包的參數(不檢查依賴項),如下所示。

$ sudo rpm -e --nodeps GeoIP

現在在下面輸入命令以確保未安裝 GeoIP。

$ sudo rpm -q GeoIP

Output
package GeoIP is not installed

升級

9.如何升級已安裝的RPM包

我們可以用 -Uvh 參數來升級安裝的特定軟件包,如下所示。

$ sudo rpm -Uvh GeoIP-1.5.0-11.el7.x86_64.rpm

Output
Preparing... ################################# [100%]
package GeoIP-1.5.0-11.el7.x86_64 is already installed

詢問

10.如何查詢所有已安裝的包

我們可以用 -a 參數與 q 查詢服務器上所有已安裝的軟件包,如下所示。

$ sudo rpm -qa

Output
python-firewall-0.4.4.4-14.el7.noarch
ncurses-base-5.9-14.20130511.el7_4.noarch
plymouth-0.8.9-0.31.20140113.el7.centos.x86_64
kbd-misc-1.15.5-13.el7.noarch
vim-common-7.4.160-4.el7.x86_64
bash-4.2.46-30.el7.x86_64
dmidecode-3.0-5.el7.x86_64
filesystem-3.2-25.el7.x86_64
kbd-1.15.5-13.el7.x86_64
vim-enhanced-7.4.160-4.el7.x86_64
firewalld-0.4.4.4-14.el7.noarch
....

11.如何查詢特定包

我們可以用 grep 用於查找特定軟件包是否安裝的命令,如下所示。

$ sudo rpm -qa | grep GeoIP

Output
GeoIP-1.5.0-11.el7.x86_64

12.如何查詢一個文件屬於哪個RPM包

在 example 數字 9 我們輸入命令列出已安裝的 RPM 包的所有文件,現在我們想知道 /usr/lib64/libGeoIP.so.1.5.0 文件屬於哪個 RPM 包。

$ sudo rpm -qf /usr/lib64/libGeoIP.so.1.5.0

output
GeoIP-1.5.0-11.el7.x86_64

核實

13.如何獲取特定包的信息

我們可以用 -i 參數與 q 獲取特定包的信息,如下所示。

$ sudo rpm -qi GeoIP

Output
Name : GeoIP
Version : 1.5.0
Release : 11.el7
Architecture: x86_64
Install Date: Thu 16 Aug 2018 02:04:09 PM UTC
Group : Development/Libraries
Size : 2905020
License : LGPLv2+ and GPLv2+ and CC-BY-SA
Signature : RSA/SHA256, Sun 20 Nov 2016 05:49:19 PM UTC, Key ID 24c6a8a7f4a80eb5
Source RPM : GeoIP-1.5.0-11.el7.src.rpm
Build Date : Sat 05 Nov 2016 08:29:17 PM UTC
Build Host : worker1.bsys.centos.org
Relocations : (not relocatable)
Packager : CentOS BuildSystem
Vendor : CentOS
URL : https://www.maxmind.com/app/c
Summary : Library for country/city/organization to IP address or hostname mapping
Description :
GeoIP is a C library that enables the user to find the country that any IP
address or hostname originates from. It uses a file based database that is
accurate as of June 2007 and can optionally be updated on a weekly
basis by installing the GeoIP-update package. This database simply contains IP
blocks as keys, and countries as values. This database should be more complete
and accurate than using reverse DNS lookups.

This package includes GeoLite data created by MaxMind, available from https://www.maxmind.com/

14.如何驗證一個RPM包

我們可以通過將軟件包的已安裝文件信息與 rpm 數據庫進行比較來驗證軟件包,通過使用 -Vp 選項(驗證包)。

$ sudo rpm -Vp GeoIP-1.5.0-11.el7.x86_64.rpm

15.如何驗證所有RPM包

我們可以驗證所有已安裝的 rpm 包,通過使用 -Va 選項(驗證所有)。

$ sudo rpm -Va

output
S.5....T. c /etc/sysconfig/authconfig
S.5....T. c /etc/yum.repos.d/CentOS-Base.repo
.M....... c /etc/machine-id
.M....... g /etc/udev/hwdb.bin
.M....... g /var/lib/systemd/random-seed
.M....... c /etc/shadow
S.5....T. c /etc/ssh/sshd_config
.M....... c /etc/audit/rules.d/audit.rules
S.5....T. c /etc/NetworkManager/NetworkManager.conf
....L.... c /etc/pam.d/fingerprint-auth
....L.... c /etc/pam.d/password-auth
....L.... c /etc/pam.d/postlogin
...

另請閱讀:

  • 如何在不安裝 RPM 發行版的情況下下載軟件包
  • 管理基於 RPM 的 Linux 發行版的 21 個 DNF 命令
  • 如何使用 Linux RPM 命令查詢包

一個 rpm 包由程序文件、配置文件和包所需的已編譯軟件程序和庫(依賴項)的列表組成。 此 rpm 實用程序僅適用於以“.rpm”格式構建的軟件包。