百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

学习笔记-更换国内镜像源 - CentOS

nanshan 2024-10-13 03:47 53 浏览 0 评论

学习笔记-更换国内镜像源 - CentOS

前言摘要

#    进提供 目前还在使用的 CentOS 版本及替代版本 RockyOS 的源信息
#    版本涵盖 
        CentOS 7
        RokcyOS 9
        CentOS 8
        CentOS Stream
        CentOS 6
#    镜像源 提供 四个版本,更多源可参考镜像源列表文档
        清华大学 镜像源
        北京大学 镜像源 (因为清华大学不提供RokcyOS源)
        阿里云 镜像源
        校园网联合 镜像源
#    为方便操作,全部采用各个镜像源官方脚本
#    文末,提供了 CentOS 7 及 RockyOS 9 的镜像源一键替换脚本

CentOS 7

CentOS 7 更换 清华大学 镜像源

#    CentOS 7 更换 清华大学 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
    -i.bak \
    /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 7 更换 阿里云 镜像源

#    CentOS 7 更换 阿里云 镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.BAK
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-7.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 7 更换 校园网联合 镜像源

#    CentOS 7 更换 校园网联合 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/centos|baseurl=https://mirrors.cernet.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

RockyOS 9

RockyOS 9 更换 北京大学 镜像源

#    RockyOS 9 更换 北京大学 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.pku.edu.cn/rocky|g' \
         -i.bak \
         /etc/yum.repos.d/rocky-extras.repo \
         /etc/yum.repos.d/rocky.repo
#    重建软件包缓存
dnf clean all && dnf makecache

RockyOS 9 更换 阿里云 镜像源

#    RockyOS 9 更换 阿里云 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.aliyun.com/rockylinux|g' \
    -i.bak \
    /etc/yum.repos.d/Rocky-*.repo
#    重建软件包缓存
dnf clean all && dnf makecache

RockyOS 9 更换 校园网联合 镜像源

#    RockyOS 9 更换 校园网联合 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.cernet.edu.cn/rocky|g' \
         -i.bak \
         /etc/yum.repos.d/rocky-extras.repo \
         /etc/yum.repos.d/rocky.repo
#    重建软件包缓存
dnf clean all && dnf makecache

CentOS 8

CentOS 8 更换 清华大学 镜像源

#    CentOS 8 更换 清华大学 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos|g' \
    -i.bak \
    /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 8 更换 阿里云 镜像源

#    CentOS 8 更换 阿里云 镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.BAK
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-8.5.2111.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 8 更换 校园网联合 镜像源

#    CentOS 8 更换 校园网联合 镜像源
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
         -e 's|^#baseurl=http://mirror.centos.org/$contentdir|baseurl=https://mirrors.cernet.edu.cn/centos|g' \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS Stream

CentOS Stream 更换 清华大学 镜像源

#    CentOS Stream 更换 清华大学 镜像源
#    创建 修改脚本
cat <<'EOF' > /tmp/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 $_;
    }
}
EOF

#    运行 修改脚本
perl /tmp/update_mirror.pl /etc/yum.repos.d/centos*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS Stream 更换 阿里云 镜像源

#    CentOS Stream 更换 阿里云 镜像源
#    创建 修改脚本
cat <<'EOF' > /tmp/update_mirror.pl
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $mirrors = 'https://mirrors.aliyun.com/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 $_;
    }
}
EOF

#    运行 修改脚本
perl /tmp/update_mirror.pl /etc/yum.repos.d/centos*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS Stream 更换 校园网联合 镜像源

#    CentOS Stream  更换 校园网联合 镜像源
#    创建 修改脚本
cat <<'EOF' > /tmp/update_mirror.pl
#!/usr/bin/perl

use strict;
use warnings;
use autodie;

my $mirrors = 'https://mirrors.cernet.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 $_;
    }
}
EOF

#    运行 修改脚本
perl /tmp/update_mirror.pl /etc/yum.repos.d/centos*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 6

CentOS 6 更换 清华大学 镜像源

#    CentOS 6 更换 清华大学 镜像源
sed -e "s|^mirrorlist=|#mirrorlist=|g" \
    -e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/6.10|g" \
    -e "s|^#baseurl=http://mirror.centos.org/\$contentdir/\$releasever|baseurl=https://mirrors.tuna.tsinghua.edu.cn/centos-vault/6.10|g" \
    -i.bak \
    /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 6 更换 阿里云 镜像源

#    CentOS 6 更换 阿里云 镜像源
mv /etc/yum.repos.d/CentOS-Base.repo /etc/yum.repos.d/CentOS-Base.repo.BAK
curl -o /etc/yum.repos.d/CentOS-Base.repo https://mirrors.aliyun.com/repo/Centos-vault-6.10.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 6 更换 校园网联合 镜像源

#    CentOS 6 更换 校园网联合 镜像源
#    其中 $minorver 表示版本号,需要自行替换
sed -e "s|^mirrorlist=|#mirrorlist=|g" \
         -e "s|^#baseurl=http://mirror.centos.org/centos/\$releasever|baseurl=https://mirrors.cernet.edu.cn/centos-vault/$minorver|g" \
         -i.bak \
         /etc/yum.repos.d/CentOS-*.repo
#    重建软件包缓存
yum clean all && yum makecache

CentOS 7 一键替换脚本

#    全文复制 直接运行
#    覆盖 CentOS 7 Repo 文件
cat <<'EOF' >  /etc/yum.repos.d/CentOS-Base.repo
#   CentOS-Base.repo
#   CentOS 7 更换 校园网联合 镜像源 专用 Repo 文件
#   author: SRover Lee

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
#    重建软件包缓存
yum clean all && yum makecache#    全文复制 直接运行
#    覆盖 CentOS 7 Repo 文件
cat <<'EOF' >  /etc/yum.repos.d/CentOS-Base.repo
#   CentOS-Base.repo
#   CentOS 7 更换 校园网联合 镜像源 专用 Repo 文件
#   author: SRover Lee

[base]
name=CentOS-$releasever - Base
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/os/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[updates]
name=CentOS-$releasever - Updates
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/updates/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/extras/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

[centosplus]
name=CentOS-$releasever - Plus
baseurl=https://mirrors.cernet.edu.cn/centos/$releasever/centosplus/$basearch/
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF
#    重建软件包缓存
yum clean all && yum makecache

Rocky OS 9 一键替换脚本

#    全文复制 直接运行
#    覆盖 RockyOS 9 Repo 文件
cat <<'EOF' >  /etc/yum.repos.d/CentOS-Base.repo
# rocky.repo
#   CentOS-Base.repo
#   CentOS 9 更换 校园网联合 镜像源 专用 Repo 文件
#   author: SRover Lee

[baseos]
name=Rocky Linux $releasever - BaseOS
baseurl=https://mirrors.cernet.edu.cn/rocky/$releasever/BaseOS/$basearch/os/
gpgcheck=1
enabled=1
countme=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9

[appstream]
name=Rocky Linux $releasever - AppStream
baseurl=https://mirrors.cernet.edu.cn/rocky/$releasever/AppStream/$basearch/os/
gpgcheck=1
enabled=1
countme=1
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9

相关推荐

ubuntu24.04下kubernetes1.30环境搭建

设置root用户密码#在Ubuntu系统中,默认情况下root用户是被禁用的(没有设置密码)#而是通过sudo命令让普通用户临时获取管理员权限,#如果需要启用或修改root密...

Canonical 在 Ubuntu 24.10 发布之前对 Snap 进行了更多改进

作为Ubuntu桌面临时工程总监,OliverSmith介绍了Ubuntu24.10的最新进展。在Ubuntu24.10功能冻结之前,GNOME47测试版已经登陆Ubuntu...

Ubuntu Touch OTA-5手机系统发布:细化电源配置等

IT之家8月2日消息,UBports基金会于7月30日发布UbuntuTouch20.04OTA-5版本更新,距离上次OTA-4更新发布相隔6个月时间。Ubuntu...

Ubuntu更契合英特尔酷睿Ultra,综合性能比Win11高15%

IT之家12月23日消息,英特尔本月推出酷睿UltraMeteorLake处理器,那么Win11和Ubuntu发行版两者时间,谁能更好地发挥其性能呢?国外科技媒体phoron...

针对英特尔酷睿CPU优化,Canonical发布Ubuntu实时内核

IT之家7月27日消息,Canonical今天宣布针对支持时序协调运算(TCC)和时间敏感网络(IEEETSN)的英特尔酷睿处理器,推出优化版实时Ubuntu内核。Canonical...

在Ubuntu/Debian上设置永久DNS域名服务器

在Linux上设置自定义DNS服务器可以提高性能和安全性,甚至可以通过DNS阻止一些使用地理屏蔽的网站。有几种方法可以做到这一点,包括在许多Linux发行版中包括的NetworkManagerGUI...

宣布延期:Ubuntu 24.04 LTS第一个版本发布推迟两周

Ubuntu开发团队原计划于8月19日星期四发布Ubuntu24.04.1LTS。然而,由于发现几个重大升级错误,发布被推迟。Ubuntu24.04.1LTS的新发布日期现定为...

Ubuntu系统已经十岁了 10月新推14.10版

|责编:李鑫比较非主流的Ubuntu系统刚刚推出了14.10版,同时大家也可能不知道其实它已经十岁了!它的第一次公布时间为2004年的10月呢。在这次十周年更新中,Ubuntu为用...

wsl2在休眠后的时间偏差问题的修复

笔记本电脑在日常使用中,常常会有进入休眠状态的情况。休眠对于wsl2而言,却造成了时间偏差的问题,休眠期间wsl2的时间停止了。这个问题的根治,需要等微软。本文提供的是一种简单的修复办法。ntp是网络...

基于Ubuntu22.04源码安装配置RabbitVCS过程记录

基于Ubuntu22.04源码安装配置RabbitVCS过程记录安装开始时间开始时间:2025年7月18日17:09(北京时间)系统:Ubuntu22.04用户:itgather时区:A...

GNOME 46桌面环境发布,Ubuntu 24.04 LTS和Fedora 40率先预装

IT之家3月21日消息,GNOME团队今天发布公告,正式推出代号为“Kathmandu”的GNOME46桌面环境,并已经开放下载。Fedora40发行版将于4月发布,率先预装...

如果大家同意的话 Ubuntu可能很快就会有一个新的垃圾桶图标

Ubuntu贡献者目前正在构思一个新的垃圾桶图标,该图标最早可能在10月份Ubuntu25.10发布时出现在Dock栏中。关于Ubuntu垃圾桶图标外观的讨论在2019年持续进...

Ubuntu 25.10 通过更安全地获取时间来提供进一步的安全性提升

Canonical宣布将从Ubuntu25.10开始使用一款名为chrony的软件,以实现更安全的时间管理。最终用户无需过于担心这一变化,但它将增强系统安全性,尤其是在加密操作和证书验证方...

Linux 修改系统时间的两种方式

一:更新系统时间的方式1、手动修改通过相关工具来手动修改系统的时间。2、自动同步使用NTP自动同步系统时间。二:手动修改系统时间1、date工具作用:显示和设置系统时间选项:-d<字符串&g...

Ubuntu计划下版本为RISC-V设置RVA23基线,大量硬件无法升级

IT之家7月14日消息,主要Linux发行版之一的Ubuntu计划在其接下来的一个大版本25.10中将对RISC-V处理器的准入门槛设置从此前的RVA20配置文件更新至最新...

取消回复欢迎 发表评论: