Centos 9stream国内yum源

来源:Linux 教程 / 时间:2026-10-04

Centos 9stream国内yum源 使用方法
CentOS Stream 9 默认启用了包管理工具 dnf
是 yum 包管理工具的替代品
dnf 与 yum 大部分的命令都是通用的
dnf 也使用 /etc/yum.repos.d/ 进行镜像配置

CentOS Stream 9 中源被整合入两个文件
/etc/yum.repos.d/centos.repo
和
/etc/yum.repos.d/centos-addons.repo

由于文件中不包含 baseurl 字段,需要手动插入
通过文本替换修改源的方法较为复杂,也可以选择直接复制最后的替换结果覆盖源文件

文本替换
将这段代码保存为一个文件,例如 update_mirror.pl

#!/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 $_;
    }
}



然后,在命令行中使用以下命令来执行

perl ./update_mirror.pl /etc/yum.repos.d/centos*.repo

注意将 ./update_mirror.pl
替换为脚本实际保存的路径
其中的 * 通配符,如果只需要替换一些文件中的源,请自行增删

请确保已经安装了 Perl 解释器
并将 perl 命令添加到系统的 PATH 环境变量中。这样才能在命令行中运行 Perl 脚本

对于大部分 CentOS Stream 9 镜像
应该已经包含了 Perl 解释器
如果你的镜像没有包含,你可以使用以下命令简单的安装

# 使用 dnf
dnf install perl

# 使用 yum
yum install perl

最后,更新软件包缓存

# 使用 dnf
dnf clean all && dnf makecache

# 使用 yum
yum clean all && yum makecache

注意,如果需要启用其中一些 repo
需要将其中的 enabled=0 改为 enabled=1。

注:截至 2023-05-16
并未在官方源与镜像源中发现 nfv-source
建议不要开启 nfv-source

标签: Centos

上一篇: Git From-source-in-centos

下一篇: Centos llama.cpp

客服