比起 dnf 指令,更多人知道的是 yum 套件管理指令,其實 dnf 是 yum 未來取代版本,提供了如下功能:
yum 指令。因此,本小節將以 dnf 指令為主。
在 Enterprise Linux 8 開始正式使用 dnf 做為套件管理的程式,但為了相下相容,以目前的版本使用 yum 也是可以的,但為了面對未來的趨勢,學習 dnf 能夠讓自己瞭解系統套件的安裝狀態。
dnf 預設會從網路上取得可用套件,因此要使用 dnf 安裝套件之前必須讓主機可以連通網路。具體上 dnf 是會先讀取套件來源的設定檔,再從這些設定檔案的來源位置尋找可安裝套件。
這些設定檔被存放在 /etc/yum.repos.d/ 目錄中,是以 .repo 做為副檔名。
我們可以查看作業系統安裝好之後,有哪些套件設定檔:
student$ ls /etc/yum.repos.d/
Rocky-AppStream.repo         Rocky-NFV.repo
Rocky-BaseOS.repo            Rocky-Plus.repo
Rocky-Debuginfo.repo         Rocky-PowerTools.repo
Rocky-Devel.repo             Rocky-RT.repo
Rocky-Extras.repo            Rocky-ResilientStorage.repo
Rocky-HighAvailability.repo  Rocky-Sources.repo
Rocky-Media.repo
每一個 .repo 檔案都是一個安裝設定檔,而設定檔裡可能有 1 個或多個安裝來源的描述,但大抵上每個描述都是下列的架構:
[baseos]
name=Rocky Linux $releasever - BaseOS
mirrorlist=https://mirrors.rockylinux.org/mirrorlist?arch=$basearch&repo=BaseOS-$releasever
#baseurl=http://dl.rockylinux.org/$contentdir/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-rockyofficial
下表說明參數用意:
| 參數 | 解釋 | 
|---|---|
| [baseos] | Repository 的識別名稱。這是一個唯一的名稱,用於區分不同的 repository。 | 
| name | 供操作者辨識的說明。 | 
| mirrorlist | 指定安裝來源的列表 (list),dnf 會從中選擇合適的位置當作安裝來源。 | 
| #baseurl | 明確指定套件安裝來源位置,加入 #代表不使用這個設定 (註解)。 | 
| gpgcheck | 是否檢查 GPG 簽名。用來確認套件的封裝者是否正確。 | 
| enabled | 是否啟用這個 repository(1 表示是,0 表示否)。 | 
| gpgkey | GPG 公鑰的存放位置,用於驗證下載的套件是否和指定的公鑰相符,這會和 gpgcheck一同使用。 | 
這些設定指示 dnf 如何從這個 repository 下載和驗證套件。一般來說,我們會看到 mirrorlist 和 baseurl 兩者只有一個會被使用(即只有一個沒有被註解掉),而 gpgcheck 和 gpgkey 通常會一起使用,以確保套件的完整性和安全性。
dnf 提供很多副指令供管理員使用,其中常用的項目如下:
| 功能/指令 | 說明 | 
|---|---|
| install <package> | 安裝指定的套件。 | 
| update <package> | 更新指定的套件,如果不指定更新所有已安裝的套件。 | 
| remove <package> | 移除指定的套件,預設會一併刪除不再使用的相依套件。 | 
| list <package> | 列出所有可用的和已安裝的版本,也可以指定套件名稱。 | 
| history | 查看 dnf 操作的歷史紀錄。 | 
| clean all | 清除快取資料,包括套件安裝檔 (rpm) 和套件相關資訊。 | 
| provides | 查詢指令收錄在哪一個套件裡。 | 
以下示範常用的 dnf 操作:
查詢 httpd 套件
student$ sudo dnf list httpd
安裝 httpd 套件
安裝 httpd 套件的同時,也會把相依性一併安裝。
root# dnf install httpd
Last metadata expiration check: 0:00:16 ago on Tue 12 Sep 2023 10:07:15 PM CST.
Dependencies resolved.
===============================================================================
 Package               Arch       Version                  Repository     Size
===============================================================================
Installing:
 httpd                 x86_64     2.4.53-11.el9_2.5        appstream      47 k
Installing dependencies:
 apr                   x86_64     1.7.0-11.el9             appstream     123 k
 apr-util              x86_64     1.6.1-20.el9_2.1         appstream      94 k
 apr-util-bdb          x86_64     1.6.1-20.el9_2.1         appstream      12 k
 httpd-core            x86_64     2.4.53-11.el9_2.5        appstream     1.4 M
 httpd-filesystem      noarch     2.4.53-11.el9_2.5        appstream      14 k
 httpd-tools           x86_64     2.4.53-11.el9_2.5        appstream      81 k
 mailcap               noarch     2.1.49-5.el9             baseos         32 k
 rocky-logos-httpd     noarch     90.14-1.el9              appstream      24 k
Installing weak dependencies:
 apr-util-openssl      x86_64     1.6.1-20.el9_2.1         appstream      14 k
 mod_http2             x86_64     1.15.19-4.el9_2.4        appstream     149 k
 mod_lua               x86_64     2.4.53-11.el9_2.5        appstream      61 k
Transaction Summary
===============================================================================
Install  12 Packages
Total download size: 2.0 M
Installed size: 6.0 M
Is this ok [y/N]: y (這邊輸入 "y")
Downloading Packages:
(1/12): mailcap-2.1.49-5.el9.noarch.rpm        297 kB/s |  32 kB     00:00    
(2/12): mod_lua-2.4.53-11.el9_2.5.x86_64.rpm   457 kB/s |  61 kB     00:00    
(3/12): httpd-tools-2.4.53-11.el9_2.5.x86_64.r 1.2 MB/s |  81 kB     00:00    
(4/12): rocky-logos-httpd-90.14-1.el9.noarch.r 136 kB/s |  24 kB     00:00    
(5/12): httpd-2.4.53-11.el9_2.5.x86_64.rpm     672 kB/s |  47 kB     00:00    
(6/12): httpd-filesystem-2.4.53-11.el9_2.5.noa 324 kB/s |  14 kB     00:00    
(7/12): apr-util-openssl-1.6.1-20.el9_2.1.x86_ 298 kB/s |  14 kB     00:00    
(8/12): apr-util-bdb-1.6.1-20.el9_2.1.x86_64.r 156 kB/s |  12 kB     00:00    
(9/12): apr-util-1.6.1-20.el9_2.1.x86_64.rpm   949 kB/s |  94 kB     00:00    
(10/12): mod_http2-1.15.19-4.el9_2.4.x86_64.rp 1.3 MB/s | 149 kB     00:00    
(11/12): apr-1.7.0-11.el9.x86_64.rpm           852 kB/s | 123 kB     00:00    
(12/12): httpd-core-2.4.53-11.el9_2.5.x86_64.r 7.2 MB/s | 1.4 MB     00:00    
-------------------------------------------------------------------------------
Total                                          1.2 MB/s | 2.0 MB     00:01     
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                       1/1 
  Installing       : apr-1.7.0-11.el9.x86_64                              1/12 
  Installing       : apr-util-bdb-1.6.1-20.el9_2.1.x86_64                 2/12 
  Installing       : apr-util-1.6.1-20.el9_2.1.x86_64                     3/12 
  Installing       : apr-util-openssl-1.6.1-20.el9_2.1.x86_64             4/12 
  Installing       : httpd-tools-2.4.53-11.el9_2.5.x86_64                 5/12 
  Running scriptlet: httpd-filesystem-2.4.53-11.el9_2.5.noarch            6/12 
useradd warning: apache's uid 48 outside of the SYS_UID_MIN 201 and SYS_UID_MAX 999 range.
  Installing       : httpd-filesystem-2.4.53-11.el9_2.5.noarch            6/12 
  Installing       : rocky-logos-httpd-90.14-1.el9.noarch                 7/12 
  Installing       : mailcap-2.1.49-5.el9.noarch                          8/12 
  Installing       : httpd-core-2.4.53-11.el9_2.5.x86_64                  9/12 
  Installing       : mod_lua-2.4.53-11.el9_2.5.x86_64                    10/12 
  Installing       : mod_http2-1.15.19-4.el9_2.4.x86_64                  11/12 
  Installing       : httpd-2.4.53-11.el9_2.5.x86_64                      12/12 
  Running scriptlet: httpd-2.4.53-11.el9_2.5.x86_64                      12/12 
  Verifying        : mailcap-2.1.49-5.el9.noarch                          1/12 
  Verifying        : rocky-logos-httpd-90.14-1.el9.noarch                 2/12 
  Verifying        : mod_lua-2.4.53-11.el9_2.5.x86_64                     3/12 
  Verifying        : httpd-tools-2.4.53-11.el9_2.5.x86_64                 4/12 
  Verifying        : httpd-2.4.53-11.el9_2.5.x86_64                       5/12 
  Verifying        : httpd-filesystem-2.4.53-11.el9_2.5.noarch            6/12 
  Verifying        : apr-util-openssl-1.6.1-20.el9_2.1.x86_64             7/12 
  Verifying        : apr-util-bdb-1.6.1-20.el9_2.1.x86_64                 8/12 
  Verifying        : apr-util-1.6.1-20.el9_2.1.x86_64                     9/12 
  Verifying        : mod_http2-1.15.19-4.el9_2.4.x86_64                  10/12 
  Verifying        : apr-1.7.0-11.el9.x86_64                             11/12 
  Verifying        : httpd-core-2.4.53-11.el9_2.5.x86_64                 12/12 
Installed:
  apr-1.7.0-11.el9.x86_64                                                      
  apr-util-1.6.1-20.el9_2.1.x86_64                                             
  apr-util-bdb-1.6.1-20.el9_2.1.x86_64                                         
  apr-util-openssl-1.6.1-20.el9_2.1.x86_64                                     
  httpd-2.4.53-11.el9_2.5.x86_64                                               
  httpd-core-2.4.53-11.el9_2.5.x86_64                                          
  httpd-filesystem-2.4.53-11.el9_2.5.noarch                                    
  httpd-tools-2.4.53-11.el9_2.5.x86_64                                         
  mailcap-2.1.49-5.el9.noarch                                                  
  mod_http2-1.15.19-4.el9_2.4.x86_64                                           
  mod_lua-2.4.53-11.el9_2.5.x86_64                                             
  rocky-logos-httpd-90.14-1.el9.noarch                                         
Complete!
移除 httpd 套件
student$ sudo dnf remove httpd
Dependencies resolved.
===============================================================================
 Package               Arch       Version                 Repository      Size
===============================================================================
Removing:
 httpd                 x86_64     2.4.53-11.el9_2.5       @appstream      59 k
Removing unused dependencies:
 apr                   x86_64     1.7.0-11.el9            @appstream     289 k
 apr-util              x86_64     1.6.1-20.el9_2.1        @appstream     211 k
 apr-util-bdb          x86_64     1.6.1-20.el9_2.1        @appstream      15 k
 apr-util-openssl      x86_64     1.6.1-20.el9_2.1        @appstream      23 k
 httpd-core            x86_64     2.4.53-11.el9_2.5       @appstream     4.6 M
 httpd-filesystem      noarch     2.4.53-11.el9_2.5       @appstream     400  
 httpd-tools           x86_64     2.4.53-11.el9_2.5       @appstream     198 k
 mailcap               noarch     2.1.49-5.el9            @baseos         78 k
 mod_http2             x86_64     1.15.19-4.el9_2.4       @appstream     385 k
 mod_lua               x86_64     2.4.53-11.el9_2.5       @appstream     142 k
 rocky-logos-httpd     noarch     90.14-1.el9             @appstream      24 k
Transaction Summary
===============================================================================
Remove  12 Packages
Freed space: 6.0 M
Is this ok [y/N]: (這邊輸入 "y")
Running transaction check
Transaction check succeeded.
Running transaction test
Transaction test succeeded.
Running transaction
  Preparing        :                                                       1/1 
  Running scriptlet: httpd-2.4.53-11.el9_2.5.x86_64                       1/12 
  Erasing          : httpd-2.4.53-11.el9_2.5.x86_64                       1/12 
  Running scriptlet: httpd-2.4.53-11.el9_2.5.x86_64                       1/12 
  Erasing          : mod_lua-2.4.53-11.el9_2.5.x86_64                     2/12 
  Erasing          : mod_http2-1.15.19-4.el9_2.4.x86_64                   3/12 
  Erasing          : rocky-logos-httpd-90.14-1.el9.noarch                 4/12 
  Erasing          : httpd-core-2.4.53-11.el9_2.5.x86_64                  5/12 
  Erasing          : httpd-tools-2.4.53-11.el9_2.5.x86_64                 6/12 
  Erasing          : mailcap-2.1.49-5.el9.noarch                          7/12 
  Erasing          : httpd-filesystem-2.4.53-11.el9_2.5.noarch            8/12 
  Erasing          : apr-util-1.6.1-20.el9_2.1.x86_64                     9/12 
  Erasing          : apr-util-bdb-1.6.1-20.el9_2.1.x86_64                10/12 
  Erasing          : apr-1.7.0-11.el9.x86_64                             11/12 
  Erasing          : apr-util-openssl-1.6.1-20.el9_2.1.x86_64            12/12 
  Running scriptlet: apr-util-openssl-1.6.1-20.el9_2.1.x86_64            12/12 
  Verifying        : apr-1.7.0-11.el9.x86_64                              1/12 
  Verifying        : apr-util-1.6.1-20.el9_2.1.x86_64                     2/12 
  Verifying        : apr-util-bdb-1.6.1-20.el9_2.1.x86_64                 3/12 
  Verifying        : apr-util-openssl-1.6.1-20.el9_2.1.x86_64             4/12 
  Verifying        : httpd-2.4.53-11.el9_2.5.x86_64                       5/12 
  Verifying        : httpd-core-2.4.53-11.el9_2.5.x86_64                  6/12 
  Verifying        : httpd-filesystem-2.4.53-11.el9_2.5.noarch            7/12 
  Verifying        : httpd-tools-2.4.53-11.el9_2.5.x86_64                 8/12 
  Verifying        : mailcap-2.1.49-5.el9.noarch                          9/12 
  Verifying        : mod_http2-1.15.19-4.el9_2.4.x86_64                  10/12 
  Verifying        : mod_lua-2.4.53-11.el9_2.5.x86_64                    11/12 
  Verifying        : rocky-logos-httpd-90.14-1.el9.noarch                12/12 
Removed:
  apr-1.7.0-11.el9.x86_64                                                      
  apr-util-1.6.1-20.el9_2.1.x86_64                                             
  apr-util-bdb-1.6.1-20.el9_2.1.x86_64                                         
  apr-util-openssl-1.6.1-20.el9_2.1.x86_64                                     
  httpd-2.4.53-11.el9_2.5.x86_64                                               
  httpd-core-2.4.53-11.el9_2.5.x86_64                                          
  httpd-filesystem-2.4.53-11.el9_2.5.noarch                                    
  httpd-tools-2.4.53-11.el9_2.5.x86_64                                         
  mailcap-2.1.49-5.el9.noarch                                                  
  mod_http2-1.15.19-4.el9_2.4.x86_64                                           
  mod_lua-2.4.53-11.el9_2.5.x86_64                                             
  rocky-logos-httpd-90.14-1.el9.noarch                                         
Complete!
dnf 在移除時,也會一併把沒有在使用的相依套件一併刪除。
查看套件安裝歷程。
student$ sudo history
dnf history
ID     | Command line             | Date and time    | Action(s)      | Altered
-------------------------------------------------------------------------------
     4 | remove httpd             | 2023-09-12 22:09 | Removed        |   12   
     3 | install httpd            | 2023-09-12 22:08 | Install        |   12 EE
     2 | install -y mariadb-serve | 2023-05-12 00:43 | I, U           |   89 E<
     1 |                          | 2023-02-16 16:28 | Install        |  336 >E
查詢 awk 指令在哪個套件裡。
若想查出 awk 指令被收錄到哪個套件,則可以使用 provides 達成:
student$ sudo dnf provides '*bin/awk'
gawk-5.1.0-6.el9.x86_64 : The GNU version of the AWK text processing utility
Repo        : @System
Matched from:
Provide    : /bin/awk