CentOS Stream 9 切换 yum 源到清华 TUNA 镜像

在 CentOS Stream 9 上,把默认的 metalink 注释掉,追加清华 TUNA 镜像的 baseurl,可以显著提升 dnf 速度(特别是在国内网络环境)。

一、源切换脚本

update_mirror.pl 是一个 perl 一行命令脚本,遍历给定的 .repo 文件,把 metalink= 注释掉,并按 name 段解析 repoarch,自动拼接清华 TUNA 的 baseurl

完整脚本

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $mirrors = 'https://mirrors.tuna.tsinghua.edu.cn/centos-stream';

if (@ARGV < 1) {
die "Usage: $0 <filename1> <filename2> ...\n";
}

while (my $filename = shift @ARGV) {
my $backup_filename = $filename . '.bak';
rename $filename, $backup_filename;

open my $input, "<", $backup_filename;
open my $output, ">", $filename;

while (<$input>) {
s/^metalink/# metalink/;

if (m/^name/) {
my (undef, $repo, $arch) = split /-/;
$repo =~ s/^\s+|\s+$//g;
($arch = defined $arch ? lc($arch) : '') =~ s/^\s+|\s+$//g;

if ($repo =~ /^Extras/) {
$_ .= "baseurl=${mirrors}/SIGs/\$releasever-stream/extras" . ($arch eq 'source' ? "/${arch}/" : "/\$basearch/") . "extras-common\n";
} else {
$_ .= "baseurl=${mirrors}/\$releasever-stream/$repo" . ($arch eq 'source' ? "/" : "/\$basearch/") . ($arch ne '' ? "${arch}/tree/" : "os") . "\n";
}
}

print $output $_;
}
}

二、使用方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
# 给脚本加执行权限
chmod +x update_mirror.pl

# 切换 CentOS Stream 基础源
sudo ./update_mirror.pl \
/etc/yum.repos.d/centos.repo

# 切换 EPEL
sudo ./update_mirror.pl \
/etc/yum.repos.d/epel.repo \
/etc/yum.repos.d/epel-next.repo

# 清理缓存
sudo dnf clean all
sudo dnf makecache

三、脚本工作原理

对每个 .repo 文件:

  1. 备份<file>.bak
  2. metalink= 注释成 # metalink(保留原配置可回滚)
  3. 解析每个 name=<repo>-<arch> 段:
    • repo 段名(如 BaseOSAppStreamExtras
    • arch 架构(如 x86_64aarch64source
  4. 追加清华 TUNA baseurl
    • Extras/SIGs/$releasever-stream/extras/.../extras-common
    • 其他走 /$releasever-stream/$repo/.../$arch/tree/

四、效果示例

切换前 /etc/yum.repos.d/CentOS-Stream-BaseOS.repo

1
2
3
[baseos]
name=CentOS Stream $releasever - BaseOS
metalink=https://mirrors.centos.org/mirrorlist?arch=$basearch&repo=centos-stream-$releasever-baseos&...

切换后:

1
2
3
4
[baseos]
name=CentOS Stream $releasever - BaseOS
# metalink=https://mirrors.centos.org/mirrorlist?arch=$basearch&repo=centos-stream-$releasever-baseos&...
baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-stream/$releasever-stream/BaseOS/$basearch/os/tree/

五、验证

1
2
3
4
5
# 查看 baseurl 是否生效
dnf repolist -v | grep -E "Repo-id|baseurl"

# 测速:安装一个小包
time sudo dnf install -y htop

六、回滚

1
2
3
4
# 用备份还原
sudo cp /etc/yum.repos.d/centos.repo.bak /etc/yum.repos.d/centos.repo
sudo dnf clean all
sudo dnf makecache

七、其它镜像

如需切换到其他镜像,修改脚本顶部的 $mirrors 变量即可:

镜像 URL
清华 TUNA https://mirrors.tuna.tsinghua.edu.cn/centos-stream
中科大 USTC https://mirrors.ustc.edu.cn/centos-stream
阿里云 https://mirrors.aliyun.com/centos-stream
华为云 https://mirrors.huaweicloud.com/centos-stream

总结

  • 脚本核心动作:注释 metalink + 追加 baseurl
  • 适用于 BaseOS / AppStream / Extras / PowerTools / CRB 等所有标准源
  • 不适用于第三方仓库(如 docker-ce.reponvidia-container-toolkit.repo),它们有自己的镜像机制
  • 备份即回滚,放心执行

CentOS Stream 9 切换 yum 源到清华 TUNA 镜像
https://zhazhajust.github.io/2026/06/10/centos-stream-mirror/
作者
JayZz
发布于
2026年6月10日
许可协议