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 源優化實戰:替換官方源為 USTC/阿里云/清華鏡像、EPEL/Remi 擴展源與離線源構建

編輯:Linux 教學

CentOS 官方鏡像在國內訪問極慢且不穩定。本文提供生產環境驗證的 Yum 源替換全流程:備份原源、選擇優質鏡像站 (USTC/阿里云/清華)、配置 Base/EPEL/Remi 擴展源、GPG 密鑰導入、元數據緩存優化、離線本地源構建與多版本鎖定策略。

核心替換流程 (CentOS 7/8/Stream 通用)

1. 備份原配置


mkdir -p /etc/yum.repos.d/backup
mv /etc/yum.repos.d/*.repo /etc/yum.repos.d/backup/

2. 創建優化後的 CentOS-Base.repo

推薦:USTC (中科大) 鏡像,學術網骨幹直連,速度最穩定


cat > /etc/yum.repos.d/CentOS-Base.repo <<'EOF'
# CentOS-Base.repo - USTC Mirror
[base]
name=CentOS-$releasever - Base - mirrors.ustc.edu.cn
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=https://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-$releasever

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

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

[centosplus]
name=CentOS-$releasever - Plus - mirrors.ustc.edu.cn
baseurl=https://mirrors.ustc.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
enabled=0
gpgkey=https://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-$releasever
EOF

備選:阿里云鏡像 (ECS 內網極快)


# 將 baseurl 替換為:
# https://mirrors.aliyun.com/centos/$releasever/os/$basearch/
# gpgkey 替換為:
# https://mirrors.aliyun.com/centos/RPM-GPG-KEY-CentOS-$releasever

3. 安裝並配置 EPEL 擴展源 (企業級必裝)


# 安裝 epel-release 包 (自動配置鏡像)
yum install -y epel-release

# 優化 epel.repo 指向 USTC
sed -i 's|^metalink=|#metalink=|' /etc/yum.repos.d/epel.repo
sed -i 's|^#baseurl=https://download.fedoraproject.org/pub/epel|baseurl=https://mirrors.ustc.edu.cn/epel|' /etc/yum.repos.d/epel.repo
# CentOS 8/Stream 需額外調整 $releasever 為 8 或 9

4. 配置 Remi 源 (PHP/MySQL 新版本)


# 安裝 remi-release
yum install -y https://rpms.remirepo.net/enterprise/remi-release-7.rpm  # CentOS 7
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-8.rpm  # CentOS 8/Stream 8
# yum install -y https://rpms.remirepo.net/enterprise/remi-release-9.rpm  # CentOS Stream 9

# 啟用 PHP 8.2 模組流
yum module reset php -y
yum module enable php:remi-8.2 -y

5. 導入 GPG 密鑰並清理重建緩存


# 導入 CentOS 官方密鑰 (防止 gpgcheck 失敗)
rpm --import https://mirrors.ustc.edu.cn/centos/RPM-GPG-KEY-CentOS-7  # 或 8, 9

# 清理並重建緩存
yum clean all
yum makecache fast
# 或
dnf clean all && dnf makecache  # CentOS 8/Stream

版本鎖定與穩定性保障


# 安裝版本鎖定插件
yum install -y yum-plugin-versionlock

# 鎖定關鍵包版本 (防止意外升級破壞相容性)
yum versionlock kernel-3.10.0-1160.el7
yum versionlock docker-ce-20.10.24
yum versionlock mysql-community-server-8.0.36
yum versionlock nginx-1.24.0
yum versionlock '*openjdk*1.8.0*'

# 查看/刪除鎖定
yum versionlock list
yum versionlock delete kernel-3.10.0-1160.el7

離線本地源構建 (內網隔離環境)

1. 在有外網機器下載所有 RPM


# 創建下載目錄
mkdir -p /repo/centos/7/os/x86_64
mkdir -p /repo/centos/7/updates/x86_64
mkdir -p /repo/epel/7/x86_64

# 使用 reposync 同步 (需 createrepo_c)
yum install -y yum-utils createrepo_c
reposync --repoid=base --repoid=updates --repoid=extras --repoid=epel \
    --download-path=/repo --downloadcomps --download-metadata --newest-only

# 生成元數據
createrepo -g comps.xml /repo/centos/7/os/x86_64
createrepo /repo/centos/7/updates/x86_64
createrepo /repo/epel/7/x86_64

2. 打包傳輸至內網服務器


tar czf repo.tar.gz /repo
scp repo.tar.gz user@internal-server:/tmp/
# 內網服務器解壓
tar xzf /tmp/repo.tar.gz -C /

3. 內網服務器配置本地 file:// 源


cat > /etc/yum.repos.d/local.repo <<'EOF'
[local-base]
name=Local CentOS Base
baseurl=file:///repo/centos/7/os/x86_64
gpgcheck=0
enabled=1

[local-updates]
name=Local CentOS Updates
baseurl=file:///repo/centos/7/updates/x86_64
gpgcheck=0
enabled=1

[local-epel]
name=Local EPEL
baseurl=file:///repo/epel/7/x86_64
gpgcheck=0
enabled=1
EOF
yum clean all && yum makecache

4. 內網 HTTP/HTTPS 源 (多機器共享)


# 安裝 nginx 提供目錄索引
yum install -y nginx
cat > /etc/nginx/conf.d/repo.conf <<'EOF'
server {
    listen 80;
    server_name repo.internal;
    root /repo;
    autoindex on;
    autoindex_exact_size off;
    autoindex_localtime on;
}
EOF
systemctl enable --now nginx
# 客戶端配置 baseurl=https://repo.internal/centos/7/os/x86_64/

常見問題排查

現象原因解決
GPG check FAILED密鑰缺失/不匹配rpm --import URL 或 gpgcheck=0 (臨時)
repomd.xml 404鏡像路徑變更/$releasever 解析錯誤手動指定 baseurl=.../7/... 或 8/...
Metadata expired緩存過期yum clean metadata && yum makecache
依賴衝突多源混用版本不一致yum distro-sync、versionlock、優先級 priority=N
速度仍慢未走內網/選錯鏡像檢查 curl -I baseurl、阿里云 ECS 用 mirrors.cloud.aliyuncs.com

CentOS 8/Stream 9 遷移提示

  • CentOS 8 2021 年底 EOL,建議遷移至 Rocky Linux 8/9、AlmaLinux 8/9、CentOS Stream 9。
  • 新發行版默認使用 dnf、module 機制、appstream 源,配置文件在 /etc/yum.repos.d/ 兼容。
  • 鏡像站路徑調整為 /rocky/, /almalinux/, /centos-stream/。
Copyright © Windows教程網 All Rights Reserved