586 字
3 分钟
Red Hat 7.4/8.1 YUM 仓库配置

RHEL(Red Hat Enterprise Linux)和 CentOS 不同,未购买 Red Hat 订阅时 yum 会因为 RHN(Red Hat Network)插件限制无法使用,甚至 yum 包本身也是订阅版。最近因故折腾起 RHEL,便有了这篇笔记。

涉及版本:

  • Red Hat Enterprise Linux release 8.1 (Ootpa)
  • Red Hat Enterprise Linux Server release 7.4 (Maipo)

重要:以下方案是把 RHEL 改造为使用 CentOS 同主版本源,本质上是用 CentOS 包替换 RHEL 包,生产环境不建议——会丢失 RHEL 支持服务并可能违反订阅协议。这里仅作实验或学习用途。

1. RHEL 8 处理方案#

1.1 卸载原版 yum#

RHEL 8 的 yum 实际是 dnf 的兼容层,但仍受订阅控制。先卸载:

Terminal window
[root@rhel8 ~]# rpm -qa | grep yum
yum-4.2.7-6.el8.noarch
[root@rhel8 ~]# rpm -e yum-* --nodeps

1.2 配置 CentOS 8 源并安装新版 yum#

Terminal window
[root@rhel8 ~]# curl -o /etc/yum.repos.d/Centos8.repo \
https://mirrors.aliyun.com/repo/Centos-8.repo
[root@rhel8 ~]# rpm -ivh --nodeps \
https://mirrors.aliyun.com/centos/8/BaseOS/x86_64/os/Packages/yum-4.7.0-4.el8.noarch.rpm
[root@rhel8 ~]# yum clean all && yum makecache

CentOS 8 已 EOL,aliyun.com/repo 路径可能失效,必要时改用 https://mirrors.aliyun.com/centos-vault/8.5.2111/ 之类的归档路径。

2. RHEL 7 处理方案#

2.1 清理原版 yum 全家桶#

RHEL 7 的 yum 由多个子包组成,全部卸载:

Terminal window
[root@rhel7 ~]# rpm -qa | grep yum
yum-metadata-parser-1.1.4-10.el7.x86_64
yum-3.4.3-154.el7.noarch
yum-rhn-plugin-2.0.1-9.el7.noarch
[root@rhel7 ~]# rpm -qa | grep yum | xargs rpm -e --nodeps

yum-rhn-plugin 就是阻止未订阅机器使用 yum 的元凶。

2.2 准备 CentOS 7 仓库文件#

Terminal window
[root@rhel7 ~]# mkdir /etc/yum.repos.d/
[root@rhel7 ~]# curl -o /etc/yum.repos.d/Centos7.repo \
https://mirrors.aliyun.com/repo/Centos-7.repo
# 替换变量占位符(RHEL 没有这两个变量,需要写死)
[root@rhel7 ~]# sed -in 's#\$releasever#7#g' /etc/yum.repos.d/Centos7.repo
[root@rhel7 ~]# sed -in 's#\$basearch#x86_64#g' /etc/yum.repos.d/Centos7.repo

2.3 重新安装 yum 与依赖#

Terminal window
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/rpm-4.11.3-45.el7.x86_64.rpm
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-metadata-parser-1.1.4-10.el7.x86_64.rpm
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/python-urlgrabber-3.10-10.el7.noarch.rpm
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-3.4.3-168.el7.centos.noarch.rpm
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/yum-plugin-fastestmirror-1.1.31-54.el7_8.noarch.rpm
[root@rhel7 ~]# rpm -ivh --nodeps https://mirrors.aliyun.com/centos/7/os/x86_64/Packages/wget-1.14-18.el7_6.1.x86_64.rpm
[root@rhel7 ~]# yum clean all && yum makecache

3. 验证#

Terminal window
$ yum repolist
$ yum install -y vim

应该能看到 base / updates / extras 三个仓库就绪,并能正常下载安装包。

4. 常见问题#

现象处理
卸载 yum 后报 --nodeps 仍失败系统正在用 yum 自身,找其他工具如 rpm 卸载
装新版 yum 时 Failed dependenciesRHEL 7 必须按 1.2.3 节顺序逐个 rpm 安装,跳步会出问题
yum makecache 报变量未替换没跑 sed 替换 $releasever / $basearch
升级后系统标识混乱部分 RHEL 工具会检测 OS-Release,看到 CentOS 包后可能不稳定,混用风险自担

5. 同系列文章#

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