yum 指令:用法與選項、yum-utils 與儲存庫確認
yum 指令是在 RHEL 系列 Linux 上安裝、更新與移除套件的套件管理指令。在 AlmaLinux 9 伺服器上實際執行查詢、更新檢查指令與 yum-utils 工具後發現,yum 是以與 dnf 相同的程式執行,輸出也與 dnf 相同。
下方輸出中的儲存庫清單、套件 release 編號與安裝時間都是範例值。
1️⃣ yum 指令基本用法:yum 與 dnf
在 AlmaLinux 9 輸入 yum 會執行 dnf。這是因為 /usr/bin/yum 是指向 dnf-3 的連結。
ls -l /usr/bin/yum /usr/bin/dnflrwxrwxrwx 1 root root 5 Oct 5 10:12 /usr/bin/dnf -> dnf-3
lrwxrwxrwx 1 root root 5 Oct 5 10:12 /usr/bin/yum -> dnf-3兩個檔案都是指向 dnf-3 的連結。所以執行 yum --version 也會顯示 dnf 的版本。
yum --version4.14.0
Installed: dnf-0:4.14.0-21.el9_8.alma.1.noarch at Mon Oct 5 10:12:30 2026
Built : AlmaLinux Packaging Team <packager@almalinux.org> at Fri May 1 09:30:00 2026
Installed: rpm-0:4.16.1.3-23.el9.x86_64 at Mon Oct 5 10:12:08 2026
Built : AlmaLinux Packaging Team <packager@almalinux.org> at Thu Mar 12 08:15:20 2026第一行的 4.14.0 是 dnf 版本,下面接著是 dnf 與 rpm 套件的安裝時間。之所以連 rpm 版本也一起顯示,是因為 yum 從儲存庫取得的套件最後是用 rpm安裝的。
指令的基本格式依序是 yum、選項、指令、套件名稱。--help 的第一行就是這個格式,下面接著是指令清單。
yum --help | head -n 12usage: yum [options] COMMAND
List of Main Commands:
alias List or create command aliases
autoremove remove all unneeded packages that were originally installed as dependencies
check check for problems in the packagedb
check-update check for available package upgrades
clean remove cached data
deplist [deprecated, use repoquery --deplist] List package's dependencies and what packages provide them
distro-sync synchronize installed packages to the latest available versions
downgrade Downgrade a package常用的指令整理在表格中。
| 指令 | 作用 | |
|---|---|---|
| list installed | 已安裝套件清單(加上套件名稱則只看該套件) | |
| list updates | 儲存庫中有新版本的套件清單 | |
| check-update | 確認是否有更新(有則結束代碼為 100) | |
| info | 單一套件的版本、大小、儲存庫與說明 | |
| search | 從名稱與摘要搜尋套件 | |
| install | 安裝套件(含相依套件) | |
| remove | 移除套件(與 erase 相同) | |
| update | 將已安裝套件更新為新版本(與 upgrade 相同) | |
| autoremove | 移除當作相依套件安裝、現在已不需要的套件 | |
| makecache | 預先下載儲存庫中繼資料並建立快取 | |
| clean all | 刪除全部已下載的快取 | |
| repolist | 已啟用的儲存庫清單 | |
| deplist | 相依性與提供套件(建議使用 repoquery --deplist) | |
| history | 安裝與移除的交易紀錄 |
2️⃣ yum 指令選項:-y、-C、exclude、enablerepo
選項放在 yum 與指令之間。-y 會對問題一律回答是,因此用在腳本中;在正式伺服器上手動執行 update 時,比較適合不加 -y,先看交易表再自己回答。
| 選項 | 複製 | 說明 |
|---|---|---|
| -C, --cacheonly | 不連線到儲存庫,只使用已下載的快取。 | |
| -q, --quiet | 隱藏中繼資料行等提示,只輸出結果。 | |
| --showduplicates | 同時輸出同一套件的已安裝版本與儲存庫版本。 | |
| --exclude=PKG, -x PKG | 在這次指令中排除該套件(可用 kernel* 之類的模式)。 | |
| --enablerepo=REPO | 只在這次指令中啟用已停用的儲存庫。 | |
| --version | 輸出 yum(dnf)與 rpm 的版本。 | |
| -h, --help | 輸出說明。 |
| 選項 | 複製 | 說明 |
|---|---|---|
| -y, --assumeyes | 對所有問題回答是。 | |
| -v, --verbose | 詳細輸出執行內容。 | |
| --disablerepo=REPO | 只在這次指令中停用已啟用的儲存庫。 | |
| --skip-broken | 略過相依性損壞的套件,繼續處理其餘套件。 |
-C 不連線到儲存庫,只用已下載的快取輸出結果。第一行留下快取最後更新的時間,第二行起才是結果。
yum -C list treeLast metadata expiration check: 0:35:03 ago on Thu Oct 29 05:32:35 2026.
Installed Packages
tree.x86_64 1.8.0-12.el9 @baseos加上 -q 後第一行會消失,只顯示結果。
yum -C -q list installed treeInstalled Packages
tree.x86_64 1.8.0-12.el9 @baseos--showduplicates 會把已安裝的版本與儲存庫中的版本並列輸出。
yum -C --showduplicates list libavifLast metadata expiration check: 0:38:15 ago on Thu Oct 29 05:32:35 2026.
Installed Packages
libavif.x86_64 0.11.1-7.el9 @epel
Available Packages
libavif.x86_64 1.1.1-2.el9 epelInstalled Packages 的 0.11.1 與 Available Packages 的 1.1.1 是同一個套件的兩個版本。儲存庫的版本較新,所以 libavif 是要更新的套件。
--exclude 會在這次指令中排除該套件。
yum -C --exclude=libavif check-update libavif libdav1dLast metadata expiration check: 0:35:10 ago on Thu Oct 29 05:32:35 2026.
libdav1d.x86_64 1.5.4-2.el9 epel指定了兩個套件,但 libavif 被排除,只剩下 libdav1d。加在 update 上就會排除該套件、更新其餘套件。
3️⃣ yum 套件管理與更新順序
就算知道選項,也要先看清楚裝了什麼、出了什麼新版本,才能決定要更新哪些套件。所以先從確認已安裝套件的指令看起。
yum -C list installed treeInstalled Packages
tree.x86_64 1.8.0-12.el9 @baseos最後一欄的 @baseos 是安裝時取得套件的儲存庫。用 rpm -q 查詢,連版本都會顯示在一行裡。
rpm -q tree htop libaviftree-1.8.0-12.el9.x86_64
package htop is not installed
libavif-0.11.1-7.el9.x86_64查詢未安裝的套件時,輸出只有一行 not installed 就結束,結束代碼為 1。
儲存庫中出了新版本的套件,用 list updates 確認。
yum -C list updates libavif libdav1dLast metadata expiration check: 0:35:07 ago on Thu Oct 29 05:32:35 2026.
Available Upgrades
libavif.x86_64 1.1.1-2.el9 epel
libdav1d.x86_64 1.5.4-2.el9 epelAvailable Upgrades 下方就是有新版本的套件。不加套件名稱時,會以所有已安裝的套件為對象。
check-update 也會用結束代碼告知同樣的清單。
yum -C check-update libavif libdav1d; echo $?Last metadata expiration check: 0:35:08 ago on Thu Oct 29 05:32:35 2026.
libavif.x86_64 1.1.1-2.el9 epel
libdav1d.x86_64 1.5.4-2.el9 epel
100最後一行的 100 是結束代碼。有更新時為 100,沒有時為 0,因此腳本以這個值來判斷。
更新依 makecache、update、autoremove 的順序進行。
yum makecachemakecache 會預先下載儲存庫的中繼資料。update 會自己下載,所以可以省略。
yum updateupdate 會把所有已安裝套件升到新版本,也一併處理相依性。
yum update libavif加上套件名稱就只升級該套件。
yum autoremoveautoremove 會刪除當作相依套件一起安裝、現在已不再需要的套件。在 update 之後執行。
yum update --exclude='kernel*'若要排除核心進行更新,就在 --exclude 加上 kernel 模式。核心更新後要重新開機才會以新核心運作,是否需要重新開機可用 yum-utils 的 needs-restarting 確認。
4️⃣ yum-utils 工具:yumdownloader、repoquery、needs-restarting
AlmaLinux 9 的 yum-utils 是由 dnf-plugins-core 提供的相容層。指令名稱沿用舊 YUM 時代,實際執行的是 dnf 外掛。
yum -C info yum-utilsLast metadata expiration check: 0:38:12 ago on Thu Oct 29 05:32:35 2026.
Installed Packages
Name : yum-utils
Version : 4.3.0
Release : 14.el9
Architecture : noarch
Size : 23 k
Source : dnf-plugins-core-4.3.0-14.el9.src.rpm
Repository : @System
From repo : baseos
Summary : Yum-utils CLI compatibility layer
URL : https://github.com/rpm-software-management/dnf-plugins-core
License : GPLv2+
Description : As a Yum-utils CLI compatibility layer, supplies in CLI shims for
: debuginfo-install, repograph, package-cleanup, repoclosure, repomanage,
: repoquery, reposync, repotrack, repodiff, builddep, config-manager, debug,
: download and yum-groups-manager that use new implementations using DNF.Source 是 dnf-plugins-core,Summary 是 Yum-utils CLI compatibility layer。若尚未安裝,用 [yum install yum-utils] 安裝。
這個套件安裝的 17 個指令可用 rpm -ql 列出。
rpm -ql yum-utils | grep bin//usr/bin/debuginfo-install
/usr/bin/find-repos-of-install
/usr/bin/needs-restarting
/usr/bin/package-cleanup
/usr/bin/repo-graph
/usr/bin/repoclosure
/usr/bin/repodiff
/usr/bin/repomanage
/usr/bin/repoquery
/usr/bin/reposync
/usr/bin/repotrack
/usr/bin/yum-builddep
/usr/bin/yum-config-manager
/usr/bin/yum-debug-dump
/usr/bin/yum-debug-restore
/usr/bin/yum-groups-manager
/usr/bin/yumdownloaderls -l /usr/bin/yumdownloader /usr/bin/repoquery /usr/bin/needs-restarting /usr/bin/yum-config-managerlrwxrwxrwx 1 root root 22 Mar 3 2026 /usr/bin/needs-restarting -> /usr/libexec/dnf-utils
lrwxrwxrwx 1 root root 22 Mar 3 2026 /usr/bin/repoquery -> /usr/libexec/dnf-utils
lrwxrwxrwx 1 root root 22 Mar 3 2026 /usr/bin/yum-config-manager -> /usr/libexec/dnf-utils
lrwxrwxrwx 1 root root 22 Mar 3 2026 /usr/bin/yumdownloader -> /usr/libexec/dnf-utils全部都是指向同一個檔案 /usr/libexec/dnf-utils 的連結。常用工具與用途整理成樹狀圖。
yumdownloader 只下載套件檔而不安裝。加上 --urls 則只輸出要下載的網址。
yumdownloader --urls treeLast metadata expiration check: 0:35:16 ago on Thu Oct 29 05:32:35 2026.
https://repo.almalinux.org/almalinux/9.8/BaseOS/x86_64/os/Packages/tree-1.8.0-12.el9.x86_64.rpm儲存庫網址與檔名會顯示在一行。可以用這個網址預先下載檔案,再搬到無法連網的伺服器上安裝。
repoquery 會查詢套件安裝的檔案與相依性。
repoquery -l treeLast metadata expiration check: 0:35:17 ago on Thu Oct 29 05:32:35 2026.
/usr/bin/tree
/usr/lib/.build-id
/usr/lib/.build-id/e0
/usr/lib/.build-id/e0/e2ec3d46003583b56979e08d437395bd0d2a07
/usr/share/doc/tree
/usr/share/doc/tree/README
/usr/share/licenses/tree
/usr/share/licenses/tree/LICENSE
/usr/share/man/man1/tree.1.gz-l 是該套件的檔案清單。裡面有一個執行檔,以及文件、授權與說明手冊。
repoquery --requires treeLast metadata expiration check: 0:35:19 ago on Thu Oct 29 05:32:35 2026.
libc.so.6(GLIBC_2.34)(64bit)
rtld(GNU_HASH)--requires 是需要的函式庫。yum deplist 也會輸出相同內容並附上提供套件,但說明中標示為 deprecated,因此改用 repoquery --deplist。
yum -C repoquery --deplist treeLast metadata expiration check: 0:38:17 ago on Thu Oct 29 05:32:35 2026.
package: tree-1.8.0-12.el9.x86_64
dependency: libc.so.6(GLIBC_2.34)(64bit)
provider: glibc-2.34-170.el9_8.x86_64
dependency: rtld(GNU_HASH)
provider: glibc-2.34-170.el9_8.i686
provider: glibc-2.34-170.el9_8.x86_64dependency 行是需要的東西,provider 行是提供它的套件。
更新後是否需要重新開機,用 needs-restarting -r 確認。
needs-restarting -rNo core libraries or services have been updated since boot-up.
Reboot should not be necessary.核心或重要函式庫更新後,提示會變成需要重新開機,並以結束代碼 1 結束。
package-cleanup 不加選項執行時,只會輸出請你選擇要做哪種檢查的提示。
package-cleanuppackage-cleanup has to be executed with one of the options: --dupes, --leaves, --orphans, --problems or --cleandupes5️⃣ 確認 yum 儲存庫:repolist 與 enablerepo 選項
就算工具齊全,套件不在儲存庫裡的話 yum 也什麼都找不到。所以先看哪些 yum 儲存庫是啟用的。
yum 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 - Extrasrepolist 只輸出設定檔中 enabled=1 的儲存庫。baseos 與 appstream 是發行版的預設儲存庫,epel 是另外新增的儲存庫。
yum repolist --all | head -n 12repo id repo name status
appstream AlmaLinux 9 - AppStream enabled
appstream-debuginfo AlmaLinux 9 - AppStream - Debug disabled
appstream-source AlmaLinux 9 - AppStream - Source disabled
baseos AlmaLinux 9 - BaseOS enabled
baseos-debuginfo AlmaLinux 9 - BaseOS - Debug disabled
baseos-source AlmaLinux 9 - BaseOS - Source disabled
crb AlmaLinux 9 - CRB disabled
crb-debuginfo AlmaLinux 9 - CRB - Debug disabled
crb-source AlmaLinux 9 - CRB - Source disabled
epel Extra Packages for Enterprise Linux 9 - x enabled
epel-cisco-openh264 Extra Packages for Enterprise Linux 9 ope enabled--all 會連同 status 欄一起輸出已停用的儲存庫。像 crb 這樣 disabled 的儲存庫,其中的套件無法直接安裝。
若只想在這次指令中啟用已停用的儲存庫,就加上 --enablerepo。
yum --enablerepo=crb repolistrepo id repo name
appstream AlmaLinux 9 - AppStream
baseos AlmaLinux 9 - BaseOS
crb AlmaLinux 9 - CRB
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 - Extrascrb 已進入清單。設定檔沒有變,所以下一個指令又會被排除。若要讓儲存庫一直保持啟用,請用 yum-config-manager修改設定檔。儲存庫啟用與停用的樣子,可以在下方圖表中按指令按鈕查看。
新增像 EPEL 這樣的新儲存庫,只要安裝一個儲存庫套件就完成了。
yum install epel-release安裝後會在 /etc/yum.repos.d/ 底下產生 epel.repo 檔案,repolist 中也會出現 epel。
🔲 yum 指令模擬器
在下方的模擬器中,可以親自輸入 yum list、check-update、repolist 與 yum-utils 指令並確認輸出。儲存庫清單與本文使用相同的範例值,安裝、移除與更新指令只會顯示提示。
🔢 FAQ & 推薦內容
ℹ️ 聯盟行銷說明
本站內容包含聯盟行銷連結。訪客透過這些連結購買商品或服務時,本站會從賣家獲得佣金。在這個過程中,購買者支付的金額(遇到活動折扣時金額會降低↓)並不會因此提高。刊登的價格、折扣與庫存資訊以撰寫當時為準,可能與實際情況不同,購買前請向賣家做最後確認。商品的挑選與評價依本站自訂的標準撰寫,是否支付佣金不會影響介紹順序或評價內容。