跳转至

7. 软件仓库配置

本章介绍 RHEL 9 的软件仓库管理,涵盖三种核心场景:已注册订阅的官方仓库、无订阅的 ISO 本地源,以及补充社区包的 EPEL 仓库。合理配置仓库是系统软件安装、安全更新的直接基础。

仓库文件位置

yum/dnf 仓库配置文件存放于 /etc/yum.repos.d/,后缀为 .repo。已注册订阅时,Red Hat 官方仓库由 subscription-manager 管理,配置写入 /etc/yum.repos.d/redhat.repo不要手动编辑此文件


7.1 前提条件

  • 已完成订阅注册(第 6 章);或准备了 RHEL 9 ISO 文件用于本地源。
  • 通过 SecureCRT(SSH 端口 2222)以 ops 用户、密钥认证方式登录 192.168.1.100,执行 sudo -i 切换至 root 环境。
  • 网络连接正常(官方仓库和 EPEL 仓库需联网;本地 ISO 源无需联网)。

7.2 官方订阅仓库

7.2.1 启用核心仓库

# 确认订阅状态
subscription-manager status

# 启用 BaseOS(核心系统包)和 AppStream(应用流模块)
subscription-manager repos \
  --enable=rhel-9-for-x86_64-baseos-rpms \
  --enable=rhel-9-for-x86_64-appstream-rpms

# 刷新元数据缓存
dnf makecache

检查点

dnf repolist 输出中两个仓库显示 enabled,BaseOS 包数约 2000+,AppStream 包数约 5000+。

7.2.2 管理仓库开关

# 查看所有可用仓库(含禁用)
subscription-manager repos --list

# 禁用不需要的仓库(减少元数据同步耗时)
subscription-manager repos \
  --disable=rhel-9-for-x86_64-supplementary-rpms

架构说明

ARM64 服务器将 x86_64 替换为 aarch64;IBM Power 替换为 ppc64le;IBM Z 替换为 s390x


7.3 本地 ISO 仓库(无订阅 / 离线环境)

适用于尚未完成订阅注册、测试环境或完全隔离的内网服务器。

7.3.1 上传 ISO 文件

通过 SecureFX(SFTP,端口 2222)将 rhel-9.x-x86_64-dvd.iso 上传至服务器 /root/ 目录。

7.3.2 持久挂载 ISO

将挂载写入 /etc/fstab,确保重启后自动挂载:

# 创建挂载点
mkdir -p /mnt/rhel-iso

# 临时挂载验证内容
mount -o loop,ro /root/rhel-9.x-x86_64-dvd.iso /mnt/rhel-iso
ls /mnt/rhel-iso/

# 写入 /etc/fstab 实现持久挂载
echo '/root/rhel-9.x-x86_64-dvd.iso  /mnt/rhel-iso  iso9660  loop,ro  0 0' \
  >> /etc/fstab

# 验证 fstab 配置正确
mount -a
df -hT /mnt/rhel-iso

7.3.3 创建本地仓库配置文件

tee /etc/yum.repos.d/local-rhel.repo << 'EOF'
[local-baseos]
name=Local RHEL 9 BaseOS
baseurl=file:///mnt/rhel-iso/BaseOS
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[local-appstream]
name=Local RHEL 9 AppStream
baseurl=file:///mnt/rhel-iso/AppStream
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
EOF

导入 GPG 公钥(若尚未导入):

rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

验证本地仓库:

dnf clean all
dnf makecache
dnf repolist

检查点

dnf repolistlocal-baseoslocal-appstream 显示 enabled,包数量不为零。

注意

本地 ISO 仓库不提供安全更新,仅包含发布时的版本。生产环境应尽快完成订阅注册后切换至官方仓库。


7.4 EPEL 仓库

EPEL(Extra Packages for Enterprise Linux)由 Fedora 社区维护,提供 RHEL 官方仓库未收录的高质量软件包(如 htopiperf3certbotnmap 等)。

7.4.1 安装 EPEL 仓库

dnf install -y \
  https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm

7.4.2 验证

dnf repolist | grep epel

7.4.3 使用建议

# 安装 EPEL 包时明确指定仓库来源(便于追踪)
dnf install --enablerepo=epel htop -y

# 查询某个包来自哪个仓库
dnf info htop

生产环境提示

EPEL 包不受 Red Hat 官方支持。生产环境安装 EPEL 包前,确认官方仓库中无等效替代,并评估对系统稳定性的影响。


7.5 仓库管理常用命令

# 查看所有仓库(含禁用)
dnf repolist --all

# 清理所有缓存(仓库配置变更后必须执行)
dnf clean all

# 重建元数据缓存
dnf makecache --refresh

# 查看已安装包的来源仓库
dnf list installed | grep '@'

# 检查可用安全更新
dnf check-update --security

7.6 常见问题与排查

问题现象 排查步骤
dnf repolist 为空 检查订阅状态;确认 --enable 命令已执行
本地仓库无法读取 确认 ISO 已挂载:df -hT /mnt/rhel-iso;确认目录路径正确
GPG key not found 执行 rpm --import /etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release
EPEL 安装超时 检查网络;添加 --setopt=timeout=120 参数
仓库间包冲突 使用 dnf install --repo=<repo-name> 指定来源;或用 dnf module 管理模块流

7.7 实践任务

  1. 登录 linuxdc(SecureCRT 端口 2222,ops 用户,密钥认证,sudo -i 切换 root)。
  2. 若已注册订阅:启用 BaseOS 与 AppStream,执行 dnf repolist 验证。
  3. 若未注册:挂载 ISO,创建本地仓库配置文件,执行 dnf repolist 验证。
  4. 安装并验证 EPEL 仓库:

    dnf install -y https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm
    dnf repolist | grep epel
    
  5. 安装 htop 并确认来源:

    dnf install -y htop
    dnf info htop
    htop --version
    

7.8 自测问题

Q1:为什么不应直接编辑 /etc/yum.repos.d/redhat.repo

该文件由 subscription-manager 自动生成和管理,手动编辑的内容会在下次执行 subscription-manager 命令时被覆盖。应通过 subscription-manager repos --enable/--disable 命令管理官方仓库。

Q2:本地 ISO 仓库设置 gpgcheck=1 是否必要?

是的。虽然 ISO 本身已通过完整性校验,但启用 GPG 校验可确保每个 RPM 包的签名链完整,防止 ISO 内容被意外修改,是企业安全基线的要求。

Q3:如何确认安装的软件包来自预期的仓库而非其他来源?

安装后执行 rpm -qi <package-name> 查看 From repo 字段,或执行 dnf info <package-name> 查看 Repository 字段,确认来源仓库名称。


7.9 章节总结

本章介绍了 RHEL 9 三种软件仓库场景的完整配置:官方订阅源(首选)、本地 ISO 源(离线备用)和 EPEL 补充仓库。正确启用仓库并开启 GPG 校验,是系统软件供给安全的第一道保障,也是后续安全更新与合规管理的基础。