跳转到内容

仓库管理

适用于 CentOS Stream 9 & 10 / AlmaLinux 9.x & 10.x / Rocky Linux 9.x & 10.x

软件仓库(Repository)是 DNF 获取软件包的来源。合理管理仓库是系统管理的基本功——你需要知道系统目前配置了哪些仓库、如何添加新仓库、以及如何控制仓库的优先级。

  • 查看和管理已配置的仓库
  • 启用和禁用仓库
  • 添加第三方仓库
  • .repo 文件的结构
  • 配置仓库优先级
  • 使用 dnf config-manager
  • 一台已安装 EL 9.x / 10.x 的系统
  • 拥有 sudo 权限的用户账户
  • 系统已联网
  • 了解 DNF 基础操作
查看当前启用的仓库
$ dnf repolist

输出示例:

repo id repo name
appstream AlmaLinux 9 - AppStream
baseos AlmaLinux 9 - BaseOS
extras AlmaLinux 9 - Extras

列出所有仓库(包括已禁用的)

Section titled “列出所有仓库(包括已禁用的)”
查看所有仓库
$ dnf repolist all

输出示例:

repo id repo name status
appstream AlmaLinux 9 - AppStream enabled
appstream-debug AlmaLinux 9 - AppStream - Debug disabled
appstream-source AlmaLinux 9 - AppStream - Source disabled
baseos AlmaLinux 9 - BaseOS enabled
baseos-debug AlmaLinux 9 - BaseOS - Debug disabled
baseos-source AlmaLinux 9 - BaseOS - Source disabled
crb AlmaLinux 9 - CRB disabled
extras AlmaLinux 9 - Extras enabled
查看某个仓库的详细信息
$ dnf repoinfo baseos

输出示例:

Repo-id : baseos
Repo-name : AlmaLinux 9 - BaseOS
Repo-status : enabled
Repo-revision : 1711234567
Repo-updated : Mon 24 Mar 2026 08:00:00 AM CST
Repo-pkgs : 1,842
Repo-available-pkgs: 1,842
Repo-size : 1.5 G
Repo-baseurl : https://repo.almalinux.org/almalinux/9/BaseOS/x86_64/os/
Repo-expire : 86,400 second(s) (last: Mon 24 Mar 2026 10:15:00 AM CST)
Repo-filename : /etc/yum.repos.d/almalinux-baseos.repo

所有仓库配置文件存放在 /etc/yum.repos.d/ 目录下,以 .repo 为后缀。

查看 repo 配置文件列表
$ ls /etc/yum.repos.d/

一个典型的 .repo 文件内容如下:

/etc/yum.repos.d/almalinux-baseos.repo 示例
[baseos]
name=AlmaLinux $releasever - BaseOS
baseurl=https://repo.almalinux.org/almalinux/$releasever/BaseOS/$basearch/os/
enabled=1
gpgcheck=1
countme=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9
metadata_expire=86400

各字段说明:

字段说明
[baseos]仓库 ID,唯一标识符
name仓库名称,仅用于显示
baseurl仓库的 URL 地址
mirrorlist镜像列表 URL(与 baseurl 二选一)
enabled1 启用,0 禁用
gpgcheck1 启用 GPG 签名验证,0 禁用
gpgkeyGPG 公钥路径
metadata_expire元数据过期时间(秒)
priority仓库优先级(需要 dnf-plugins-core

EL 9 与 EL 10 都使用 DNF 4(EL 10 为 4.20),config-manager 语法相同:

禁用一个仓库
$ sudo dnf config-manager --set-disabled crb
启用一个仓库
$ sudo dnf config-manager --set-enabled crb
验证变更
$ dnf repolist | grep crb
crb AlmaLinux 9 - CRB

如果只想在某次操作中临时使用某个仓库,不必修改配置:

临时启用某个仓库来安装包
$ sudo dnf install --enablerepo=crb some-devel-package
临时禁用某个仓库
$ sudo dnf update --disablerepo=epel

大多数知名第三方仓库提供 RPM 包来自动配置仓库:

以 EPEL 为例
$ sudo dnf install epel-release

这会自动在 /etc/yum.repos.d/ 下创建相应的 .repo 文件并导入 GPG 密钥。

方法二:使用 dnf config-manager 添加

Section titled “方法二:使用 dnf config-manager 添加”
直接添加仓库 URL
$ sudo dnf config-manager --add-repo https://example.com/repo/el9/example.repo

这会将远程 .repo 文件下载到 /etc/yum.repos.d/

  1. 创建 .repo 文件

    创建自定义仓库配置
    $ sudo vi /etc/yum.repos.d/my-custom.repo

    写入以下内容:

    [my-custom-repo]
    name=My Custom Repository
    baseurl=https://repo.example.com/el9/$basearch/
    enabled=1
    gpgcheck=1
    gpgkey=https://repo.example.com/RPM-GPG-KEY-example
  2. 刷新缓存

    清除并重建缓存
    $ sudo dnf clean all
    $ sudo dnf makecache
  3. 验证仓库已生效

    确认新仓库出现在列表中
    $ dnf repolist

当多个仓库提供同名软件包时,可以通过优先级控制 DNF 优先从哪个仓库获取。

  1. 确认 dnf-plugins-core 已安装

    安装 DNF 插件核心包
    $ sudo dnf install -y dnf-plugins-core
  2. 编辑仓库配置文件,添加 priority 字段

    编辑仓库文件
    $ sudo vi /etc/yum.repos.d/epel.repo

    [epel] 段中添加:

    priority=10

优先级规则:

  • 数值越小优先级越高
  • 默认优先级为 99
  • 建议将官方仓库保持默认或设为较高优先级,第三方仓库设为较低优先级
仓库建议优先级
baseos / appstream10(默认即可)
EPEL20
其他第三方仓库30 及以上

EL 9 与 EL 10(DNF 4.20)语法一致:

查看某个仓库的配置
$ dnf config-manager --dump baseos
设置仓库的某个选项
$ sudo dnf config-manager --save --setopt=epel.priority=20
添加远程仓库文件
$ sudo dnf config-manager --add-repo https://example.com/repo.repo
启用仓库
$ sudo dnf config-manager --set-enabled <repo-id>
禁用仓库
$ sudo dnf config-manager --set-disabled <repo-id>

提示 “Cannot find a valid baseurl for repo”

Section titled “提示 “Cannot find a valid baseurl for repo””

通常是网络问题或仓库 URL 失效:

检查网络连通性
$ curl -I https://repo.almalinux.org/
如果使用 mirrorlist,检查 DNS 是否正常
$ dig mirrorlist.centos.org

如果使用国内服务器,可能需要替换为国内镜像源。

以阿里云镜像为例,编辑对应的 .repo 文件,将 baseurl 替换为阿里云地址:

备份原始配置
$ sudo cp /etc/yum.repos.d/almalinux-baseos.repo /etc/yum.repos.d/almalinux-baseos.repo.bak

仓库的 GPG 密钥可能未导入:

手动导入 GPG 密钥
$ sudo rpm --import https://repo.example.com/RPM-GPG-KEY-example
查看已导入的 GPG 密钥
$ rpm -qa gpg-pubkey*