Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux Yum 套件管理從入門到精通:安裝、配置、倉庫管理與自動化升級

編輯:Linux 教學

Yum(Yellowdog Updater Modified)是 RPM 系 Linux 的前端套件管理器,自動解決依賴關係、支援多倉庫、提供版本鎖定與交易歷史。本文以 CentOS 4.4 為例,詳述 Yum 安裝、主/倉庫配置、GPG 校驗、常用操作與生產環境最佳實務。

一、 Yum 安裝與依賴解決

CentOS 光碟含 Yum 及其依賴套件:

rpm -ivh python-elementtree-*.rpm
rpm -ivh sqlite-*.rpm
rpm -ivh python-sqlite-*.rpm
rpm -ivh python-urlgrabber-*.rpm
rpm -ivh centos-yumconf-*.rpm
rpm -ivh yum-*.rpm

若缺少依賴,按錯誤提示逐個安裝即可。

二、 配置檔結構解析

  • 主配置:/etc/yum.conf(快取目錄、debug 等級、proxy 等全域設定)
  • 倉庫配置目錄:/etc/yum.repos.d/(每個 .repo 檔定義一組倉庫)

三、 核心倉庫配置 CentOS-Base.repo 逐行詳解

[base]
name=CentOS-4 - Base
baseurl=https://mirror.centos.org/centos/4/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-4

[updates]
name=CentOS-4 - Updates
baseurl=https://mirror.centos.org/centos/4/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-4

[addons]
name=CentOS-4 - Addons
baseurl=https://mirror.centos.org/centos/4/addons/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-4

[extras]
name=CentOS-4 - Extras
baseurl=https://mirror.centos.org/centos/4/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-4

關鍵欄位:
name:倉庫識別名
baseurl:實際 RPM 存放 URL,$basearch 自動展開為 i386/x86_64
gpgcheck=1:啟用 GPG 簽名驗證,防止供應鏈攻擊
gpgkey:公鑰路徑(本地檔案或 HTTP URL)
enabled=1:預設啟用,設 0 可暫時停用

四、 國內鏡像源加速(推薦)

編輯 CentOS-Base.repo,將 baseurl 指向阿里雲、清華、網易等鏡像:

baseurl=https://mirrors.aliyun.com/centos/4/os/$basearch/
# 或
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos/4/os/$basearch/

執行 yum clean all && yum makecache 重建快取。

五、 常用操作速查表

任務指令
搜尋套件yum search keyword
列出可更新套件yum check-update
安裝套件yum install package
更新單一套件yum update package
全系統升級yum update
移除套件yum remove package
查詢套件資訊yum info package
列出已安裝套件yum list installed
查詢檔案屬於哪個套件yum provides /path/to/file
清理快取yum clean all
建立快取yum makecache
查看交易歷史yum history
回滾交易yum history undo ID

六、 進階技巧

  • 版本鎖定:yum install yum-plugin-versionlock → yum versionlock add kernel* 防止核心意外升級。
  • 僅下載不安裝:yum install --downloadonly --downloaddir=/tmp package
  • 本地 ISO 掛載為倉庫:
    mount -o loop CentOS-7-x86_64-DVD.iso /mnt/cdrom
    建立 local.repo 指向 file:///mnt/cdrom。
  • EPEL/Remi 等第三方倉庫:yum install epel-release 即可啟用海量額外套件。

七、 結語

Yum 將 RPM 的依賴地獄轉化為自動化流程,配合倉庫優先級、GPG 驗證、版本鎖定、歷史回滾,構建了企業級軟體生命週期管理基石。善用國內鏡像、第三方倉庫與插件生態,讓套件管理更高效、更安全。

Copyright © Windows教程網 All Rights Reserved