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

CentOS 系統配置 Yum 源完整教程(以 163/網易源為例)

編輯:關於 Linux

Yum 是 RPM 包管理器的前端工具,配置優質的軟件倉庫源能大幅提升包下載速度與安裝成功率。本文詳述在 CentOS 6/7 上替換官方源為國內鏡像源(網易 163、阿里云、清華)的標準流程,含備份、下載 repo 文件、清理緩存、驗證可用性。

Yum 源配置文件示例

方法一:一鍵替換為網易 163 源(推薦新手)

# 1. 備份原有 repo 文件
mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

# 2. 下載網易 163 CentOS-Base.repo
# CentOS 7
wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.163.com/.help/CentOS7-Base-163.repo
# CentOS 6
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.163.com/.help/CentOS6-Base-163.repo
# CentOS 8 / Stream
# wget -O /etc/yum.repos.d/CentOS-Base.repo https://mirrors.163.com/.help/CentOS8-Base-163.repo

# 3. 清理緩存並重建
yum clean all
yum makecache

# 4. 測試
yum list | head -20
yum install -y vim wget net-tools

方法二:手動編輯 repo 文件(理解結構)

編輯 /etc/yum.repos.d/CentOS-Base.repo,典型結構如下:

[base]
name=CentOS-$releasever - Base - mirrors.163.com
baseurl=https://mirrors.163.com/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates - mirrors.163.com
baseurl=https://mirrors.163.com/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=https://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras - mirrors.163.com
baseurl=https://mirrors.163.com/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=https://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus - mirrors.163.com
baseurl=https://mirrors.163.com/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7

關鍵變量:

  • $releasever:自動匹配 CentOS 大版本(6、7、8)。
  • $basearch:自動匹配架構(x86_64、aarch64 等)。
  • gpgcheck=1:驗證 RPM 簽名,保證安全性。
  • enabled=0:默認不啟用(如 centosplus),需時加 --enablerepo=centosplus。

常用國內優質鏡像源地址

鏡像站Base URL 示例 (CentOS 7)特點
網易 163https://mirrors.163.com/centos/老牌、穩定、全版本
阿里云https://mirrors.aliyun.com/centos/雲服務器內網極快、同步及時
清華大學https://mirrors.tuna.tsinghua.edu.cn/centos/HTTPS 支持、學術網優化
中科大https://mirrors.ustc.edu.cn/centos/教育網/科研網首選
華為云https://mirrors.huaweicloud.com/centos/華為云內網加速

替換方法:將 repo 文件中 baseurl 域名部分批量替換,如 sed -i 's|mirror.centos.org|mirrors.aliyun.com|g' /etc/yum.repos.d/*.repo。

EPEL 源安裝(擴展包必備)

# CentOS 7
yum install -y epel-release
# 或手動下載 rpm
# rpm -ivh https://mirrors.aliyun.com/epel/epel-release-latest-7.noarch.rpm

# 替換 EPEL 為阿里云
sed -i 's|^metalink=|#metalink=|g' /etc/yum.repos.d/epel.repo
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub/epel|baseurl=https://mirrors.aliyun.com/epel|g' /etc/yum.repos.d/epel.repo
yum clean all && yum makecache

安裝 fastestmirror 插件(自動選最快源)

yum install -y yum-plugin-fastestmirror
# 配置 /etc/yum/pluginconf.d/fastestmirror.conf
# enabled=1
# verbose=1
# socket_timeout=3
# hostfilepath=timedhosts.txt
# maxhostfileage=10
# prefer=mirrors.aliyun.com,mirrors.163.com,mirrors.tuna.tsinghua.edu.cn

常見問題排查

現象原因解決
Failed to download metadata源地址不可達、DNS 解析失敗檢查網絡、換源、配置 DNS(echo nameserver 223.5.5.5 > /etc/resolv.conf)
GPG key verification failedGPG 金鑰缺失或不匹配rpm --import https://mirrors.163.com/centos/RPM-GPG-KEY-CentOS-7
版本不匹配(如 CentOS 8 用 7 的 repo)$releasever 解析錯誤手動指定 baseurl=.../7/... 或升級 centos-release 包
內網環境無法訪問公網源無互聯網出口搭建本地 Yum 源(reposync + createrepo + Nginx)或配置代理

CentOS 8 / Stream / Rocky Linux / AlmaLinux 特別說明

CentOS 8 官方源已遷移至 vault.centos.org,建議直接遷移至 Rocky Linux 或 AlmaLinux 源:

# 以 Rocky Linux 8 為例
sed -i 's|mirrorlist=|#mirrorlist=|g' /etc/yum.repos.d/Rocky-*.repo
sed -i 's|^#baseurl=https://dl.rockylinux.org|baseurl=https://mirrors.aliyun.com/rockylinux|g' /etc/yum.repos.d/Rocky-*.repo
dnf clean all && dnf makecache

合理配置 Yum 源是 Linux 系統維護的基礎功課,選對鏡像站、啟用 EPEL、裝上 fastestmirror,能讓包管理事半功倍。

Copyright © Windows教程網 All Rights Reserved