Rocky Linux 9 更换国内源

root
root
root
管理员
3
文章
0
粉丝
LinuxRocky Linux 9 更换国内源已关闭评论19阅读模式

Rocky Linux 9 默认核心仓库主要是 BaseOS、AppStream、Extras,其中 CRB 在 Rocky 9 存在但默认不启用;Rocky Wiki 也说明 Rocky 9 的 BaseOS/AppStream 默认启用,CRB、HighAvailability、ResilientStorage 默认不启用。


一、国内源

常见可选:

镜像站 Rocky 9 baseurl 前缀
阿里云 https://mirrors.aliyun.com/rockylinux
华为云 https://mirrors.huaweicloud.com/rockylinux
腾讯云 https://mirrors.tencent.com/rocky
中科大 USTC https://mirrors.ustc.edu.cn/rocky
上海交大 SJTUG https://mirror.sjtu.edu.cn/rocky
浙江大学 ZJU https://mirrors.zju.edu.cn/rocky

中科大镜像站明确收录 Rocky Linux 的 x86_64aarch64 架构,以及 8、9、10 版本,并给出了 Rocky 9/10 的换源方式。 阿里云也提供 Rocky Linux 镜像下载与软件包镜像。

二、安装 DNF 国内源

方式1:一键替换

# 默认替换到华为云
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|^#baseurl=http://dl.rockylinux.org/$contentdir|baseurl=https://mirrors.huaweicloud.com/rockylinux/|g' \
    -i.bak \
    /etc/yum.repos.d/rocky-extras.repo \
    /etc/yum.repos.d/rocky.repo

# 阿里云替换到华为云
sed -e 's|^mirrorlist=|#mirrorlist=|g' \
    -e 's|mirrors.aliyun.com|mirrors.huaweicloud.com|g' \
    -i.bak \
    /etc/yum.repos.d/rocky-extras.repo \
    /etc/yum.repos.d/rocky.repo

sudo dnf clean all
sudo dnf makecache
sudo dnf repolist

方式2:手动替换

sudo tee /etc/yum.repos.d/rocky.repo >/dev/null <<'EOF'
[baseos]
name=Rocky Linux $releasever - BaseOS - Aliyun
baseurl=https://mirrors.huaweicloud.com/rockylinux/$releasever/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
countme=0
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9

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

[extras]
name=Rocky Linux $releasever - Extras - Aliyun
baseurl=https://mirrors.huaweicloud.com/rockylinux/$releasever/extras/$basearch/os/
enabled=1
gpgcheck=1
countme=0
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9

[crb]
name=Rocky Linux $releasever - CRB - Aliyun
baseurl=https://mirrors.huaweicloud.com/rockylinux/$releasever/CRB/$basearch/os/
enabled=0
gpgcheck=1
countme=0
metadata_expire=6h
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-Rocky-9
EOF

中科大版只需要把 baseurl 前缀换成:

https://mirrors.ustc.edu.cn/rocky

也就是:

baseurl=https://mirrors.ustc.edu.cn/rocky/$releasever/BaseOS/$basearch/os/
baseurl=https://mirrors.ustc.edu.cn/rocky/$releasever/AppStream/$basearch/os/
baseurl=https://mirrors.ustc.edu.cn/rocky/$releasever/extras/$basearch/os/
baseurl=https://mirrors.ustc.edu.cn/rocky/$releasever/CRB/$basearch/os/

然后:

sudo dnf clean all
sudo dnf makecache
sudo dnf repolist

三、dnf配置

建议检查 /etc/dnf/dnf.conf

sudo cp -a /etc/dnf/dnf.conf /etc/dnf/dnf.conf.bak.$(date +%F-%H%M%S)

sudo tee /etc/dnf/dnf.conf >/dev/null <<'EOF'
[main]
gpgcheck=1
installonly_limit=3
clean_requirements_on_remove=True
best=True
skip_if_unavailable=False

max_parallel_downloads=10
fastestmirror=True

keepcache=0
timeout=30
retries=10
EOF

sudo dnf clean all
sudo dnf makecache

说明:

配置 建议 说明
gpgcheck=1 保留 必须保留,生产环境不要关包签名校验
installonly_limit=3 保留 保留 3 个内核,方便升级内核后回滚
clean_requirements_on_remove=True 保留 卸载包时自动清理不再需要的依赖
best=True 保留 依赖解析更严格,避免装到次优/异常版本
skip_if_unavailable=False 保留 源异常时直接失败,不静默跳过,生产更安全
max_parallel_downloads=10 推荐加 默认一般是 3,国内源带宽好时可明显加快下载
fastestmirror=True 可加 多镜像场景有帮助;如果单一阿里云/中科大 baseurl,收益不大
keepcache=0 推荐 不保留 rpm 缓存,节省磁盘
timeout=30 可保留/显式写入 30 秒超时比较稳妥
retries=10 可保留/显式写入 网络偶发抖动时自动重试

四、安装 EPEL 国内源

EPEL 常用,但生产环境要克制。建议只在确实需要软件包时启用。

安装:

sudo dnf install -y epel-release

# 换成华为云 EPEL 源:
sudo sed -e 's!^metalink=!#metalink=!g' \
  -e 's!^#baseurl=!baseurl=!g' \
  -e 's!https\?://download\.fedoraproject\.org/pub/epel!https://mirrors.huaweicloud.com/epel!g' \
  -e 's!https\?://download\.example/pub/epel!https://mirrors.huaweicloud.com/epel!g' \
  -i /etc/yum.repos.d/epel{,-testing}.repo


# 阿里云 替换成 华为云
sudo sed -e 's!^metalink=!#metalink=!g' \
  -e 's!^#baseurl=!baseurl=!g' \
  -e 's!https://mirrors.aliyun.com/epel!https://mirrors.huaweicloud.com/epel!g' \
  -i /etc/yum.repos.d/epel{,-testing}.repo
  
sudo dnf clean all
sudo dnf makecache

华为云的 EPEL 帮助文档也是以 Rocky Linux 9 为例,先安装 epel-release,再替换 metalink/baseurl 到 TUNA EPEL 镜像。

生产建议,不要启用 epel-testing

dnf config-manager --set-disabled epel-testing

五、安装 ELRepo 国内源

ELRepo 用途主要是:较新内核、硬件驱动、kmod 包。ELRepo 官方说明它是面向 Enterprise Linux 的硬件支持类仓库;官方安装 Rocky/RHEL 9 对应的 release 包方式是安装 elrepo-release-9.el9.elrepo.noarch.rpm

安装官方 ELRepo release:

sudo rpm --import https://www.elrepo.org/RPM-GPG-KEY-elrepo.org
sudo dnf install -y https://www.elrepo.org/elrepo-release-9.el9.elrepo.noarch.rpm

换成清华 ELRepo 源:

sudo cp /etc/yum.repos.d/elrepo.repo /etc/yum.repos.d/elrepo.repo.bak.$(date +%F-%H%M%S)

sudo sed -e 's|^mirrorlist=|#mirrorlist=|g' \
  -e 's|http://elrepo.org/linux|https://mirrors.tuna.tsinghua.edu.cn/elrepo|g' \
  -i /etc/yum.repos.d/elrepo.repo

sudo dnf clean all
sudo dnf makecache

清华 TUNA 的 ELRepo 帮助文档也建议先按官网安装 ELRepo,然后备份 /etc/yum.repos.d/elrepo.repo,注释 mirrorlist,并把 http://elrepo.org/linux 替换为 TUNA 的 ELRepo 镜像。


六、ELRepo 的生产策略

安装 ELRepo 后,我建议这样处理:

sudo dnf repolist all | grep -i elrepo

生产环境建议:

sudo dnf config-manager --set-disabled elrepo-extras 2>/dev/null || true
sudo dnf config-manager --set-disabled elrepo-testing 2>/dev/null || true
sudo dnf config-manager --set-disabled elrepo-kernel 2>/dev/null || true

需要安装驱动或内核时,临时启用:

sudo dnf --enablerepo=elrepo-kernel list available 'kernel-lt*' 'kernel-ml*'

安装长期支持内核:

sudo dnf --enablerepo=elrepo-kernel install -y kernel-lt kernel-lt-devel

安装 mainline 内核:

sudo dnf --enablerepo=elrepo-kernel install -y kernel-ml kernel-ml-devel

但我不建议生产默认用 kernel-ml。ELRepo 自己也说明,kernel-lt 这类内核主要用于硬件测试或临时解决官方 RHEL 内核无法支持的硬件问题,并且是 “As-Is” 提供,不保证支持,也可能带来安全、性能或数据风险。


七、CRB 是否启用

默认不启用。只有遇到类似下面情况再启用:

Error:
 Problem: package xxx from epel requires yyy, but none of the providers can be installed

启用 CRB:

sudo dnf config-manager --set-enabled crb
sudo dnf makecache

不需要后可以关掉:

sudo dnf config-manager --set-disabled crb

建议:

场景 CRB
普通 Web / Docker / 数据库服务器 默认关闭
安装 EPEL 软件依赖失败 临时开启
编译类服务器 / 开发工具链较多 可以开启
极简生产环境 关闭

八、源配置后检查命令

dnf repolist
dnf repolist all
dnf makecache --refresh
dnf check
dnf repoquery --duplicates
dnf repoquery --unsatisfied

检查具体包来自哪个源:

dnf info nginx
dnf repoquery --info kernel

查看 repo 文件是否还有国外 mirrorlist:

grep -R '^mirrorlist=' /etc/yum.repos.d/
grep -R '^metalink=' /etc/yum.repos.d/
grep -R '^baseurl=' /etc/yum.repos.d/

 
root
  • 本文由 root 发表于2026年7月5日 12:56:35
  • 转载请务必保留本文链接:https://www.wtcat.com/34.html