dnf 指令:套件安裝、更新、移除與 yum 的差異
dnf 指令是在 RHEL 系列 Linux 上搜尋、安裝、更新、移除套件的套件管理指令。在 AlmaLinux 9 中,即使輸入 yum 也會執行同一個程式。我們在 AlmaLinux 9 伺服器上實際執行了 search、info、provides、history,並從輸出確認了用來找套件的 search、info、provides 各自查看哪裡,以及 history 中留下的作業紀錄。
安裝、移除、更新這類會變更系統的指令,我們並未執行。這些動作是透過 DNF 官方文件和伺服器上留存的作業紀錄確認的。
1️⃣ dnf 指令與 yum 的關係
很容易以為輸入 yum 會執行舊的 yum,但在 AlmaLinux 9 中,yum 和 dnf 都是指向同一個執行檔 dnf-3 的連結。
ls -l /usr/bin/yum /usr/bin/dnflrwxrwxrwx 1 root root 5 May 21 04:48 /usr/bin/dnf -> dnf-3
lrwxrwxrwx 1 root root 5 May 21 04:48 /usr/bin/yum -> dnf-3yum 和 dnf 的差別只在輸入的名稱,無論輸入哪一個,子指令和選項都相同。分別用 yum 和 dnf 執行 search htop 的輸出也完全一樣。
根據 DNF 官方文件 [Changes in DNF CLI compared to YUM],update 與 upgrade 是相同的動作,文件建議使用 upgrade。
在沒有 dnf 的舊版發行版上,請照常使用 yum 指令。本文中的 install、search、history 等子指令名稱在 yum 中也相同。
2️⃣ dnf 指令查詢套件:search、info、provides
不知道要安裝的程式確切的套件名稱時,在 search 後面加上一個詞來搜尋。search 會在套件名稱和摘要(Summary)中尋找該詞,並把名稱完全相同的套件排在最上面。
dnf search htopLast metadata expiration check: 0:21:53 ago on Wed Oct 28 15:24:31 2026.
========================== Name Exactly Matched: htop ==========================
htop.x86_64 : Interactive process viewerhtop 只有一個名稱完全相同的套件,因此在 [Name Exactly Matched] 下方一行就結束了。
dnf search wgetLast metadata expiration check: 0:22:35 ago on Wed Oct 28 15:24:31 2026.
========================== Name Exactly Matched: wget ==========================
wget.x86_64 : A utility for retrieving files using the HTTP or FTP protocols
========================= Name & Summary Matched: wget =========================
perl-HTTP-Tinyish-Wget.noarch : HTTP::Tinyish wget backend
wget2-devel.x86_64 : Libraries and header files needed for using wget2 libraries
wget2-libs.x86_64 : Runtime libraries for GNU Wget2
============================== Name Matched: wget ==============================
wget2.x86_64 : An advanced file and recursive website downloader像 wget 這種出現在多個套件中的詞,結果會分成三組:名稱完全相同的、名稱和摘要都包含的、只有名稱包含的。
search 不會查看說明(Description)文字,所以不適合用只出現在說明中的詞來搜尋。這時加上 [--all] 選項,就會把搜尋範圍擴大到說明和 URL。
知道套件名稱後,用 info 確認版本、大小以及來自哪個儲存庫。
dnf info htopLast metadata expiration check: 0:22:37 ago on Wed Oct 28 15:24:31 2026.
Available Packages
Name : htop
Version : 3.3.0
Release : 1.el9
Architecture : x86_64
Size : 198 k
Source : htop-3.3.0-1.el9.src.rpm
Repository : epel
Summary : Interactive process viewer
URL : http://hisham.hm/htop/
License : GPLv2+
Description : htop is an interactive text-mode process viewer for Linux, similar to
: top(1).尚未安裝的 htop 在 info htop 中顯示於 [Available Packages] 下方,版本 3.3.0,儲存庫 epel。htop 是只存在於 epel 儲存庫的套件,因此排除 epel 後再執行 search,就什麼都找不到。
dnf info wgetLast metadata expiration check: 0:22:38 ago on Wed Oct 28 15:24:31 2026.
Installed Packages
Name : wget
Version : 1.21.1
Release : 11.el9_8
Architecture : x86_64
Size : 3.1 M
Source : wget-1.21.1-11.el9_8.src.rpm
Repository : @System
From repo : appstream
Summary : A utility for retrieving files using the HTTP or FTP protocols
URL : http://www.gnu.org/software/wget/
License : GPLv3+
Description : GNU Wget is a file retrieval utility which can use either the HTTP or
: FTP protocols. Wget features include the ability to work in the
: background while you are logged out, recursive retrieval of
: directories, file name wildcard matching, remote file timestamp
: storage and comparison, use of Rest with FTP servers and Range with
: HTTP servers to retrieve files over slow or unstable connections,
: support for Proxy servers, and configurability.已安裝的 wget 顯示於 [Installed Packages] 下方。Repository 為 @System,安裝時下載來源的儲存庫則在 [From repo] 這一行記為 appstream。
反過來,只知道檔案名稱、不知道它在哪個套件中時,就在 provides 後面加上檔案路徑。
dnf provides /usr/bin/treeLast metadata expiration check: 0:22:40 ago on Wed Oct 28 15:24:31 2026.
tree-1.8.0-10.el9.x86_64 : File system tree viewer
Repo : @System
Matched from:
Filename : /usr/bin/tree
tree-1.8.0-10.el9.x86_64 : File system tree viewer
Repo : baseos
Matched from:
Filename : /usr/bin/tree以 /usr/bin/tree 搜尋的結果分成兩組:已安裝 tree 套件的一方(@System)和儲存庫的一方(baseos)。[Matched from] 下方的 Filename 就是實際比對到的路徑。如果不寫路徑只寫檔案名稱,dnf 會在前面加上 /usr/bin/ 和 /usr/sbin/ 再找一次,仍然找不到就會以下面的錯誤結束。
dnf provides nosuchfileLast metadata expiration check: 0:22:41 ago on Wed Oct 28 15:24:31 2026.
Error: No matches found. If searching for a file, try specifying the full path or using a wildcard prefix ("*/") at the beginning.錯誤訊息會提示寫出完整路徑,或在前面加上萬用字元 [*/]。三個指令各接收什麼輸入、到哪裡尋找,可以在下方的圖解中依序比較。
3️⃣ 套件安裝、移除、更新:install、remove、upgrade
找到的套件用 install 安裝。dnf 不只安裝指定的套件,還會一次安裝該套件所需的相依套件。
dnf install htop根據官方文件,dnf 會先輸出要安裝的套件清單(交易摘要),再詢問是否繼續;加上 [-y] 就會不詢問直接進行。
安裝時一併需要的相依套件,可以先用 repoquery [--requires] 確認。
dnf repoquery --requires htopLast metadata expiration check: 0:22:59 ago on Wed Oct 28 15:24:31 2026.
libc.so.6(GLIBC_2.34)(64bit)
libcap.so.2()(64bit)
libhwloc.so.15()(64bit)
libm.so.6()(64bit)
libm.so.6(GLIBC_2.2.5)(64bit)
libm.so.6(GLIBC_2.29)(64bit)
libncursesw.so.6()(64bit)
libnl-3.so.200()(64bit)
libnl-3.so.200(libnl_3)(64bit)
libnl-genl-3.so.200()(64bit)
libnl-genl-3.so.200(libnl_3)(64bit)
libtinfo.so.6()(64bit)
rtld(GNU_HASH)只加 [--requires] 時,只會列出 htop 需要的函式庫名稱。這些函式庫在哪個套件中,可用下一個指令確認。
dnf repoquery --requires --resolve htopLast metadata expiration check: 0:23:01 ago on Wed Oct 28 15:24:31 2026.
glibc-0:2.34-283.el9_8.i686
glibc-0:2.34-283.el9_8.x86_64
hwloc-libs-0:2.4.1-6.el9_7.x86_64
libcap-0:2.48-10.el9_8.1.x86_64
libnl3-0:3.11.0-1.el9.x86_64
ncurses-libs-0:6.2-12.20210508.el9.x86_64再加上 [--resolve],就會改為列出提供這些函式庫的套件名稱。其中 hwloc-libs 在測試的伺服器上沒有,所以安裝 htop 時會一併安裝。
remove 會一起移除指定的套件和相依於該套件的套件,在預設設定下,也會一併移除不再需要的相依套件。
dnf remove htop即使只移除 htop,像 hwloc-libs 這種因 htop 而安裝、其他套件都不需要的相依套件,也會一併被移除。
以前作為相依套件安裝、但現在沒有任何套件需要的套件,由 autoremove 移除。要移除的清單請先用 list [--autoremove] 確認。
dnf list --autoremoveLast metadata expiration check: 0:23:09 ago on Wed Oct 28 15:24:31 2026.
Autoremove Packages
cmake-rpm-macros.noarch 3.31.8-3.el9 @appstream
grub2-tools-efi.x86_64 1:2.06-126.el9_8.alma.1 @baseos
grub2-tools-extra.x86_64 1:2.06-126.el9_8.alma.1 @baseos
libatomic.x86_64 11.5.0-14.el9.alma.1 @baseos
libvisual.x86_64 1:0.4.0-34.el9 @appstream
libwayland-server.x86_64 1.21.0-1.el9 @appstreamdnf autoremove在測試的伺服器上,清單中出現 6 個套件,autoremove 會一次移除這 6 個。
已安裝的套件是否有新版本,用 check-update 確認。加上套件名稱時,只以該套件為對象。
dnf check-update libavif libdav1dLast metadata expiration check: 0:52:20 ago on Wed Oct 28 15:24:31 2026.
libavif.x86_64 1.1.1-3.el9 epel
libdav1d.x86_64 1.5.4-1.el9 epel上面指定 libavif 和 libdav1d 的指令,因兩者都有新版本而以結束代碼 100 結束。check-update 有更新時以 100 結束,沒有時為 0,錯誤時為 1,因此可以在指令碼中作為分支條件。
在 list 後面加上套件名稱,會同時顯示已安裝的版本和儲存庫中的新版本。用 updateinfo list 則可確認該更新屬於哪一則公告(advisory)。
dnf list libavifLast metadata expiration check: 0:52:21 ago on Wed Oct 28 15:24:31 2026.
Installed Packages
libavif.x86_64 0.11.1-5.el9 @epel
Available Packages
libavif.x86_64 1.1.1-3.el9 epellibavif 已安裝的 0.11.1-5.el9 顯示於 [Installed Packages] 下方,儲存庫的 1.1.1-3.el9 則分開顯示於 [Available Packages] 下方。
dnf updateinfo list libavifLast metadata expiration check: 0:52:24 ago on Wed Oct 28 15:24:31 2026.
FEDORA-EPEL-2026-d611eab94a bugfix libavif-1.1.1-3.el9.x86_64那個新版本是對應 bugfix 公告 FEDORA-EPEL-2026-d611eab94a 的更新。安全性更新會在同一位置像 [Moderate/Sec.] 這樣附上等級和 Sec.。
更新用 upgrade 執行。upgrade 只在相依關係相符的範圍內安裝新版本,所以 check-update 中看到的版本不一定會原樣安裝。
dnf upgrade libavif加上套件名稱時,只會把該套件和所需的相依套件升級到新版本。
dnf upgrade省略名稱時,對象是所有已安裝的套件。輸出交易摘要後詢問是否繼續的流程與 install 相同,執行過的交易會以編號留在 history 中。
4️⃣ dnf 指令儲存庫清單:repolist
search 和 install 尋找套件的地方是儲存庫(repository)。repolist 會輸出目前已啟用儲存庫的 id 和名稱。
dnf repolistrepo id repo name
appstream AlmaLinux 9 - AppStream
baseos AlmaLinux 9 - BaseOS
epel Extra Packages for Enterprise Linux 9 - x86_64
epel-cisco-openh264 Extra Packages for Enterprise Linux 9 openh264 (From Cisco) - x86_64
extras AlmaLinux 9 - Extras
nginx-stable nginx stable repo
remi-modular Remi's Modular repository for Enterprise Linux 9 - x86_64
remi-safe Safe Remi's RPM repository for Enterprise Linux 9 - x86_64很容易以為清單中沒有的儲存庫就是伺服器上沒有,但 repolist 的預設輸出只會列出已啟用的儲存庫。加上 [--all] 會連同停用的儲存庫一起輸出,並附上 status 欄。在測試的伺服器上,AlmaLinux 9 的三個 CRB 儲存庫全都是 disabled。
dnf repolist --all | grep -i crbcrb AlmaLinux 9 - CRB disabled
crb-debuginfo AlmaLinux 9 - CRB - Debug disabled
crb-source AlmaLinux 9 - CRB - Source disabled單一儲存庫的套件數量、鏡像位址、中繼資料過期時間和設定檔位置,用 repoinfo 確認。
dnf repoinfo baseosLast metadata expiration check: 0:22:55 ago on Wed Oct 28 15:24:31 2026.
Repo-id : baseos
Repo-name : AlmaLinux 9 - BaseOS
Repo-status : enabled
Repo-revision : 1793090609
Repo-updated : Tue Oct 27 17:43:29 2026
Repo-pkgs : 3458
Repo-available-pkgs: 3458
Repo-size : 21 G
Repo-mirrors : https://mirrors.almalinux.org/mirrorlist/9/baseos
Repo-baseurl : http://aws.repo.almalinux.org/almalinux/9.8/BaseOS/x86_64/os/ (9 more)
Repo-expire : 86400 second(s) (last: Wed Oct 28 15:24:26 2026)
Repo-filename : /etc/yum.repos.d/almalinux-baseos.repo
Total packages: 3458repoinfo baseos 顯示套件有 3,458 個,中繼資料設定為每 86,400 秒(一天)重新檢查一次。[Repo-filename] 這一行的 .repo 檔案就是這個儲存庫的設定檔。啟用和停用儲存庫的 [--set-enabled] 與 [--set-disabled] 選項,要搭配 dnf-plugins-core 的 config-manager 指令使用。
5️⃣ dnf 指令作業紀錄與復原:history
經過多次安裝和移除後,很難追查何時改了什麼,但 dnf 會為每筆交易(transaction)編號並記錄在 history 中。加上套件名稱時,只會顯示變更過該套件的交易。
dnf history list tmuxID | Command line | Date and time | Action(s) | Altered
-------------------------------------------------------------------------------
51 | install -y tmux | 2026-10-15 20:40 | Install | 1以 tmux 篩選後的輸出,只剩下安裝 tmux 的第 51 筆交易這一行。編號範圍用兩個點表示,例如 [50..51]。
dnf history list 50..51ID | Command line | Date and time | Action(s) | Altered
-------------------------------------------------------------------------------
51 | install -y tmux | 2026-10-15 20:40 | Install | 1
50 | -y -q install mlocate | 2026-10-07 19:39 | Install | 1 EE[Altered] 欄的數字是變更的套件數。第 50 筆那一行的末尾附有 [EE],表示交易已完成,但有警告或錯誤輸出。
單筆交易的指令列、變更的套件和結果代碼,用 history info 確認。
dnf history info 51Transaction ID : 51
Begin time : Thu Oct 15 20:40:51 2026
Begin rpmdb : d3db9c92a9d0f6ef9a7a1404552bb76e9d56ba7602b1fce73fa5a62170d33c5c
End time : Thu Oct 15 20:40:51 2026 (0 seconds)
End rpmdb : a862c33ee6a2f2d0479e4dc2631907dee47591c5b6795551b755c6cd090ac1df
User : root <root>
Return-Code : Success
Releasever : 9
Command Line : install -y tmux
Persistence : Persist
Comment :
Packages Altered:
Install tmux-3.2a-5.el9.x86_64 @baseos打開第 51 筆後,[Command Line] 中原封不動地留著執行過的指令 install -y tmux。[Packages Altered] 記錄了已安裝的 tmux 和儲存庫 @baseos,[Return-Code] 則記錄 Success。省略編號時會顯示最近一筆交易。
裝錯時,在 history undo 後面加上交易編號,就會執行與該交易相反的動作。rollback 則會把套件狀態恢復到指定交易剛完成之後。
dnf history undo 51第 51 筆交易是安裝 tmux,因此 undo 51 就成為移除 tmux 的反向交易。
dnf history rollback 50rollback 50 會把 50 之後執行的交易直到最後一筆全部復原,因此結果可能與只移除 tmux 的 undo 51 不同。
根據官方文件,若有無法復原的套件,undo 和 rollback 會不做任何變更就結束。即使直接用 rpm 變更的內容不在紀錄中,也不會因錯誤而停止。
🔲 dnf 指令模擬器
在下方的模擬器中,可以直接輸入 search、info、provides、repolist、history 指令查看輸出。用 yum 輸入也會得到相同結果。
🔢 FAQ & 推薦內容

ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。

