396 字
2 分钟
AlmaLinux9 YUM源配置

AlmaLinux 9 出厂的 /etc/yum.repos.d/almalinux*.repo 默认使用 mirrorlist=https://mirrors.almalinux.org/... 走海外 CDN,国内拉包经常超时。本文用一条 sed 命令把所有仓库切到阿里云镜像。

1. 关闭防火墙和 SELinux#

实验环境跑 dnf 经常要拉大量包,先排除安全策略干扰:

Terminal window
$ systemctl stop firewalld && systemctl disable firewalld
$ setenforce 0
$ sed -i 's/SELINUX=.*/SELINUX=disabled/' /etc/selinux/config

2. 切换到阿里云镜像#

一条 sed 同时完成两件事:注释掉原 mirrorlist、把 # baseurl 取消注释并换成阿里云域名:

Terminal window
$ sed -e 's|^mirrorlist=|#mirrorlist=|g' \
-e 's|^# baseurl=https://repo.almalinux.org|baseurl=https://mirrors.aliyun.com|g' \
-i.bak \
/etc/yum.repos.d/almalinux*.repo
$ dnf makecache

-i.bak 会生成 .repo.bak 备份文件,万一切错还能回滚。

3. 验证#

Terminal window
# 看仓库是否生效
$ dnf repolist
# 装个包试一下速度
$ dnf install -y vim

仓库列表里应该出现 baseurl 指向 mirrors.aliyun.com 的源,并且下载速度明显改善(国内带宽下通常几 MB/s)。

4. 常见问题#

现象原因 / 处理
dnf makecacheStatus code: 404阿里云对应路径不存在,确认系统主版本号是 9 而非 9.x 小版本路径错位
替换后仍走原地址没把 mirrorlist 注释掉,dnf 优先使用 mirrorlist
sed 命令报错shell 转义问题,命令必须一行写完或用反斜杠续行,不要拆段粘贴
想换回官方源mv /etc/yum.repos.d/<repo>.repo.bak /etc/yum.repos.d/<repo>.repo

5. 其他可选镜像#

如果阿里云路径变动,可以换成清华或华为:

Terminal window
# 清华
baseurl=https://mirrors.tuna.tsinghua.edu.cn/almalinux
# 华为云
baseurl=https://repo.huaweicloud.com/almalinux

只需把第二步 sed 中的 https://mirrors.aliyun.com 替换为目标域名即可。

6. 同系列文章#

  • RHEL 系:[[posts/rhel7-yum-repo/index.md]]
  • CentOS 7:[[posts/centos7-yum-repo/index.md]]
  • CentOS Stream 8:[[posts/centosstream8-yum-repo/index.md]]
  • Rocky Linux 9:[[posts/rockylinux9-yum-repo/index.md]]
  • Debian 12:[[posts/debian12-apt-repo/index.md]]
  • 离线场景制作本地仓库:[[posts/centos-local-yum-repo/index.md]]
AlmaLinux9 YUM源配置
https://blog.xeu.asia/posts/almalinux9-yum-repo/
作者
Xeu
发布于
2025-06-08
许可协议
CC BY-NC-SA 4.0