仓库管理
适用于 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 基础操作
列出已启用的仓库
Section titled “列出已启用的仓库”$ dnf repolist输出示例:
repo id repo nameappstream AlmaLinux 9 - AppStreambaseos AlmaLinux 9 - BaseOSextras AlmaLinux 9 - Extras列出所有仓库(包括已禁用的)
Section titled “列出所有仓库(包括已禁用的)”$ dnf repolist all输出示例:
repo id repo name statusappstream AlmaLinux 9 - AppStream enabledappstream-debug AlmaLinux 9 - AppStream - Debug disabledappstream-source AlmaLinux 9 - AppStream - Source disabledbaseos AlmaLinux 9 - BaseOS enabledbaseos-debug AlmaLinux 9 - BaseOS - Debug disabledbaseos-source AlmaLinux 9 - BaseOS - Source disabledcrb AlmaLinux 9 - CRB disabledextras AlmaLinux 9 - Extras enabled查看仓库详细信息
Section titled “查看仓库详细信息”$ dnf repoinfo baseos输出示例:
Repo-id : baseosRepo-name : AlmaLinux 9 - BaseOSRepo-status : enabledRepo-revision : 1711234567Repo-updated : Mon 24 Mar 2026 08:00:00 AM CSTRepo-pkgs : 1,842Repo-available-pkgs: 1,842Repo-size : 1.5 GRepo-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.repo 文件结构
Section titled “.repo 文件结构”所有仓库配置文件存放在 /etc/yum.repos.d/ 目录下,以 .repo 为后缀。
$ ls /etc/yum.repos.d/一个典型的 .repo 文件内容如下:
[baseos]name=AlmaLinux $releasever - BaseOSbaseurl=https://repo.almalinux.org/almalinux/$releasever/BaseOS/$basearch/os/enabled=1gpgcheck=1countme=1gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-AlmaLinux-9metadata_expire=86400各字段说明:
| 字段 | 说明 |
|---|---|
[baseos] | 仓库 ID,唯一标识符 |
name | 仓库名称,仅用于显示 |
baseurl | 仓库的 URL 地址 |
mirrorlist | 镜像列表 URL(与 baseurl 二选一) |
enabled | 1 启用,0 禁用 |
gpgcheck | 1 启用 GPG 签名验证,0 禁用 |
gpgkey | GPG 公钥路径 |
metadata_expire | 元数据过期时间(秒) |
priority | 仓库优先级(需要 dnf-plugins-core) |
启用和禁用仓库
Section titled “启用和禁用仓库”使用 dnf config-manager
Section titled “使用 dnf config-manager”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 crbcrb AlmaLinux 9 - CRB临时启用/禁用仓库
Section titled “临时启用/禁用仓库”如果只想在某次操作中临时使用某个仓库,不必修改配置:
$ sudo dnf install --enablerepo=crb some-devel-package$ sudo dnf update --disablerepo=epel添加第三方仓库
Section titled “添加第三方仓库”方法一:安装仓库 RPM 包
Section titled “方法一:安装仓库 RPM 包”大多数知名第三方仓库提供 RPM 包来自动配置仓库:
$ sudo dnf install epel-release这会自动在 /etc/yum.repos.d/ 下创建相应的 .repo 文件并导入 GPG 密钥。
方法二:使用 dnf config-manager 添加
Section titled “方法二:使用 dnf config-manager 添加”$ sudo dnf config-manager --add-repo https://example.com/repo/el9/example.repo这会将远程 .repo 文件下载到 /etc/yum.repos.d/。
方法三:手动创建 .repo 文件
Section titled “方法三:手动创建 .repo 文件”-
创建
.repo文件创建自定义仓库配置 $ sudo vi /etc/yum.repos.d/my-custom.repo写入以下内容:
[my-custom-repo]name=My Custom Repositorybaseurl=https://repo.example.com/el9/$basearch/enabled=1gpgcheck=1gpgkey=https://repo.example.com/RPM-GPG-KEY-example -
刷新缓存
清除并重建缓存 $ sudo dnf clean all$ sudo dnf makecache -
验证仓库已生效
确认新仓库出现在列表中 $ dnf repolist
当多个仓库提供同名软件包时,可以通过优先级控制 DNF 优先从哪个仓库获取。
-
确认
dnf-plugins-core已安装安装 DNF 插件核心包 $ sudo dnf install -y dnf-plugins-core -
编辑仓库配置文件,添加
priority字段编辑仓库文件 $ sudo vi /etc/yum.repos.d/epel.repo在
[epel]段中添加:priority=10
优先级规则:
- 数值越小优先级越高
- 默认优先级为
99 - 建议将官方仓库保持默认或设为较高优先级,第三方仓库设为较低优先级
| 仓库 | 建议优先级 |
|---|---|
| baseos / appstream | 10(默认即可) |
| EPEL | 20 |
| 其他第三方仓库 | 30 及以上 |
dnf config-manager 常用操作汇总
Section titled “dnf config-manager 常用操作汇总”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/$ dig mirrorlist.centos.org如果使用国内服务器,可能需要替换为国内镜像源。
使用国内镜像源
Section titled “使用国内镜像源”以阿里云镜像为例,编辑对应的 .repo 文件,将 baseurl 替换为阿里云地址:
$ sudo cp /etc/yum.repos.d/almalinux-baseos.repo /etc/yum.repos.d/almalinux-baseos.repo.bak提示 “GPG check FAILED”
Section titled “提示 “GPG check FAILED””仓库的 GPG 密钥可能未导入:
$ sudo rpm --import https://repo.example.com/RPM-GPG-KEY-example$ rpm -qa gpg-pubkey*- DNF 包管理基础 — 日常包管理操作
- EPEL 与第三方源 — 安装和使用 EPEL
- RPM 基础 — 底层包管理工具
man dnf.conf— DNF 配置文件详细说明man yum.conf— 仓库配置文件格式参考