温馨提示×

yum如何添加仓库

小樊
33
2026-07-23 07:54:28
栏目: 编程语言

在 CentOS / RHEL / Rocky / Alma 等使用 yum / dnf 的系统中,添加仓库(repository)常见有以下几种方式。


一、通过 .repo 文件手动添加(最常用 ✅)

1️⃣ 进入仓库配置目录

cd /etc/yum.repos.d/

2️⃣ 新建一个 .repo 文件

例如添加一个自定义仓库:

vim myrepo.repo

3️⃣ 写入仓库配置

示例:

[myrepo]
name=My Custom Repository
baseurl=http://example.com/repo/
enabled=1
gpgcheck=0

常用参数说明:

参数 说明
[myrepo] 仓库 ID
name 仓库名称
baseurl 仓库地址(http/https/ftp/file)
enabled=1 启用仓库
gpgcheck=0 是否校验 GPG(0 关闭,1 开启)
gpgkey GPG 公钥地址(如果开启 gpgcheck)

✅ 如果是本地目录:

baseurl=file:///mnt/localrepo

二、使用 yum-config-manager 添加(推荐)

1️⃣ 安装工具(如果没有)

yum install -y yum-utils

2️⃣ 添加仓库

yum-config-manager --add-repo=http://example.com/repo.repo

或:

yum-config-manager --add-repo http://example.com/repo/

三、添加 EPEL 仓库(最常见示例)

yum install -y epel-release

或者手动:

yum-config-manager --add-repo=https://mirrors.aliyun.com/epel/epel-release-latest-$(rpm -E %rhel).noarch.rpm

四、验证仓库是否添加成功

yum repolist

或:

yum repolist all

五、临时禁用 / 启用仓库

yum --disablerepo=myrepo install xxx
yum --enablerepo=myrepo install xxx

六、清理缓存(添加仓库后建议)

yum clean all
yum makecache

七、常见错误排查

❌ 仓库无法访问

ping example.com
curl http://example.com/repo/

❌ GPG 错误

  • 关闭 gpgcheck=0(测试用)
  • 或导入 GPG key:
rpm --import /path/to/RPM-GPG-KEY

如果你愿意,可以告诉我:

  • ✅ 系统版本(CentOS 7 / Rocky 8 / RHEL 9 等)
  • ✅ 要添加的是 官方仓库 / 第三方仓库 / 本地仓库

我可以给你一条完全可直接执行的命令

0