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 源修改為國內鏡像加速下載教程

編輯:Linux 教學

前言

CentOS 官方鏡像在國內下載往往極慢。將 YUM 倉庫源替換為阿里雲、清華、中科大、上海交大等國內高速鏡像,能將套件下載速度提升數十倍。本文提供 CentOS 7/8/Stream 通用的單行命令替換法與手動配置法。

一、備份原始配置

cd /etc/yum.repos.d/
mkdir backup && mv *.repo backup/
# 或單獨備份
mv CentOS-Base.repo CentOS-Base.repo.bak

二、一鍵替換腳本 (推薦,最快)

CentOS 7

# 阿里雲
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo

# 清華大學
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.tuna.tsinghua.edu.cn/help/centos/centos7-base.repo

CentOS 8 / Stream 8

# 阿里雲 (含 BaseOS、AppStream、Extras)
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-8.repo
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-8.repo

CentOS Stream 9

curl -o /etc/yum.repos.d/CentOS-Stream-BaseOS.repo https://mirrors.aliyun.com/repo/CentOS-Stream-9-BaseOS.repo
curl -o /etc/yum.repos.d/CentOS-Stream-AppStream.repo https://mirrors.aliyun.com/repo/CentOS-Stream-9-AppStream.repo

三、手動編寫配置 (以 CentOS 7 + 上海交大源為例)

cat > /etc/yum.repos.d/CentOS-Base.repo <<'EOF'
[base]
name=CentOS-7-Base
baseurl=https://ftp.sjtu.edu.cn/centos/7/os/$basearch/
gpgcheck=1
gpgkey=https://ftp.sjtu.edu.cn/centos/RPM-GPG-KEY-CentOS-7

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

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

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

四、導入 GPG 金鑰並刷新快取

# 導入官方金鑰 (鏡像站通常同步提供)
rpm --import https://ftp.sjtu.edu.cn/centos/RPM-GPG-KEY-CentOS-7

# 清理舊快取並重建
yum clean all
yum makecache

# 測試速度
yum update -y

五、EPEL 額外倉庫同步替換

# 安裝 epel-release 後替換
yum install -y epel-release
mv /etc/yum.repos.d/epel.repo /etc/yum.repos.d/epel.repo.bak

# 阿里雲 EPEL 7
curl -o /etc/yum.repos.d/epel.repo https://mirrors.aliyun.com/repo/epel-7.repo

# 清華 EPEL 7
curl -o /etc/yum.repos.d/epel.repo https://mirrors.tuna.tsinghua.edu.cn/help/epel/epel-7.repo

yum clean all && yum makecache

六、常用國內鏡像站對照表

七、DNF (CentOS 8/Stream) 專用優化

# /etc/dnf/dnf.conf 加入
fastestmirror=True
max_parallel_downloads=10
defaultyes=True
keepcache=True

結語

替換 YUM 源是 CentOS 初始化的第一步。建議根據網路運營商 (電信/聯通/移動/教育網) 選擇最近鏡像站,並配置 `fastestmirror` 插件自動選擇最優節點。定期 `yum clean all` 避免快取損壞導致異常。

Copyright © Windows教程網 All Rights Reserved
鏡像站CentOS 7 Base URL特點
阿里雲mirrors.aliyun.com/centos/速度極快、同步及時、支援 HTTPS
清華大學mirrors.tuna.tsinghua.edu.cn/centos/學術網優先、文檔完善
中科大mirrors.ustc.edu.cn/centos/教育網/聯通極快
上海交大ftp.sjtu.edu.cn/centos/電信/聯通穩定
網易mirrors.163.com/centos/老牌穩定
騰訊雲mirrors.cloud.tencent.com/centos/騰訊雲內網免流量
華為雲mirrors.huaweicloud.com/centos/華為雲內網免流量