如何在 RHEL 9 上创建本地 Yum/DNF 仓库
软件包仓库 Yum DNF
![](/data/attachment/album/202206/11/164149y9zzm7kkxwsxgszw.jpg)


你好，技术兄弟，最近红帽发布了最新的操作系统 RHEL 9，RHEL 9 满足了混合云的所有要求。它可以安装在物理服务器、虚拟机和容器镜像中。


当我们没有订阅的时候，想安装软件包来做实验，那么设置本地的 Yum 或 DNF 仓库将是很方便的。


在本指南中，我们将介绍如何在 RHEL 9 上使用 DVD 或 ISO 文件一步一步地创建本地 Yum/DNF 资源库。


创建本地 Yum/DNF 资源库的先决条件:


* 最小化安装 RHEL 9 系统
* 具有管理权限的 sudo 用户
* RHEL 9 DVD 或 ISO 文件


### 1）挂载 RHEL 9 ISO 文件或 DVD


我们假设 RHEL 9 iso 文件已经被复制到系统中。运行下面的挂载命令，将 ISO 文件挂载到 `/opt/repo` 文件夹。



```
$ sudo mkdir /var/repo
$ sudo mount -o loop rhel-baseos-9.0-x86_64-dvd.iso /var/repo/

```

![Mount-RHEL9-ISO-File-Command](/data/attachment/album/202206/11/164154ilrr67em1oeqtq71.png)


如果是 DVD 光盘，运行：



```
$ sudo mount /dev/sr0 /var/repo/

```

### 2）在 /etc/yum.repos.d/ 目录中创建仓库文件


在 `/etc/yum.repos.d/` 目录下创建一个名为 “rhel9-local.repo` 的仓库文件，内容如下：



```
$ sudo vi /etc/yum.repos.d/rhel9-local.repo
[Local-BaseOS]
name=Red Hat Enterprise Linux 9 - BaseOS
metadata_expire=-1
gpgcheck=1
enabled=1
baseurl=file:///var/repo//BaseOS/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

[Local-AppStream]
name=Red Hat Enterprise Linux 9 - AppStream
metadata_expire=-1
gpgcheck=1
enabled=1
baseurl=file:///var/repo//AppStream/
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

```

保存并关闭该文件。


![RHEL8-Local-Repo-File](/data/attachment/album/202206/11/164154hncnb4945fedzj5p.png)


### 3）刷新 Yum/DNF 和订阅管理器的缓存


执行以下命令来清理 Yum 或 DNF 和订阅管理器的缓存。



```
$ sudo dnf clean all
$ sudo subscription-manager clean

```

![DNF-Subscription-Manager-Clean](/data/attachment/album/202206/11/164154h3aa78qxhq2pa8ps.png)


在上面的输出中，我们得到一个警告信息 `This system is not registered with an entitlement`（系统没有注册权限）。所以，为了抑制这个警告信息，编辑文件 `/etc/yum/pluginconf.d/subscription-manager.conf`，将参数 `enabled=1` 改为 `enabled=0`。



```
$ sudo vi /etc/yum/pluginconf.d/subscription-manager.conf

```

![Disable-Subscription-Parameter-RHEL-9](/data/attachment/album/202206/11/164154fdkf7smfnlg7tvvu.png)


保存并退出该文件。


### 4）使用本地仓库安装软件包


现在我们都准备好测试我们的本地仓库了。运行下面的命令来查看配置仓库。



```
$ sudo dnf repolist

```

输出：


![DNF-Repolist-RHEL-9](/data/attachment/album/202206/11/164154fecmp5m55i5tjb7c.png)


现在，试试用 `dnf` 命令通过上面配置的本地仓库安装软件包。



```
$ sudo dnf install nfs-utils

```

输出：


![Install-RPM-Package-via-local-repo-rhel9](/data/attachment/album/202206/11/164154rc600lj6jj4qj0s0.png)


![Package-Installation-Completion-RHEL9-DNF-Command](/data/attachment/album/202206/11/164154p73x023ca9cxo8g9.png)


完美，上述输出证实了 `nfs-utils` 包及其依赖项已经通过本地配置的 Yum 或 DNF 仓库成功安装。


这就是本指南的全部内容。我希望你觉得它有参考价值。请在下面的评论区发表你的疑问和反馈。




---


via: <https://www.linuxtechi.com/create-local-yum-dnf-repository-rhel/>


作者：[Pradeep Kumar](https://www.linuxtechi.com/author/pradeep/) 选题：[lkxed](https://github.com/lkxed) 译者：[geekpi](https://github.com/geekpi) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
Hello techies, recently Red Hat has released its latest operating system RHEL 9. RHEL 9 fulfill all the requirements of hybrid cloud. It can be installed on physical server, virtual machine and inside the container image.

When we don’t have subscription and want to install packages for doing the POCs then setting up local yum or dnf repository will be handy.

In this guide, we will cover how to create local yum/dnf repository on RHEL 9 using DVD or ISO file step by step.

Prerequisites for creating local Yum/DNF repository

- Minimal Install
[RHEL 9](https://www.linuxtechi.com/how-to-install-rhel-9-step-by-step/)system - Sudo User with admin privileges
- RHEL 9 DVD or ISO file

## 1 ) Mount RHEL 9 ISO File or DVD

We are assuming RHEL 9 iso file is already copied into the system. Run following mount command to mount ISO file on /opt/repo folder.

$ sudo mkdir /var/repo $ sudo mount -o loop rhel-baseos-9.0-x86_64-dvd.iso /var/repo/

In case of dvd, run

$ sudo mount /dev/sr0 /var/repo/

## 2) Create Repo File in ‘/etc/yum.repos.d/’ Directory

Create a repo file with name ‘rhel9-local.repo’ under the folder /etc/yum.repos.d/ with following content

$ sudo vi /etc/yum.repos.d/rhel9-local.repo [Local-BaseOS] name=Red Hat Enterprise Linux 9 - BaseOS metadata_expire=-1 gpgcheck=1 enabled=1 baseurl=file:///var/repo/BaseOS/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release [Local-AppStream] name=Red Hat Enterprise Linux 9 - AppStream metadata_expire=-1 gpgcheck=1 enabled=1 baseurl=file:///var/repo/AppStream/ gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-redhat-release

Save and close the file.

## 3) Flush Yum/DNF & Subscription Manager Cache

Execute following commands to clean yum or dnf and subscription manager cache.

$ sudo dnf clean all $ sudo subscription-manager clean

In the above output, we are getting a warning message ‘This system is not registered with an entitlement’. So, to suppress this warning message, edit the file ‘/etc/yum/pluginconf.d/subscription-manager.conf’ , change the parameter ‘enabled=1’ to ‘enabled=0’.

$ sudo vi /etc/yum/pluginconf.d/subscription-manager.conf

Save and exit the file.

## 4) Install Packages using Local Repository

Now we are all set to test our local repository. Run beneath command to view configure repository.

$ sudo dnf repolist

Output,

Now, try Install packages using dnf command via above configure local repository.

$ sudo dnf install nfs-utils

Output,

Perfect, above output confirms that nfs-utils package along with its dependencies are installed successfully via locally configured yum or dnf repository.

That’s all from this guide. I hope you have found it informative. Kindly do post your queries and feedback in below comments section.

Also Read: [How to Install PostgreSQL 15 on RHEL 9 Step by Step](https://www.linuxtechi.com/how-to-install-postgresql-on-rhel/)

Charles PhamThe instruction very clearly. Thank you very much

manaduvortesWorked perfectly, thank you very much.

easyplease how did you do this first step

“We are assuming RHEL 9 iso file is already copied into the system. Run following mount command to mount ISO file on /opt/repo folder.”

Pradeep KumarYou need to copy RHEL 9 ISO file to your system and then use mount command to mount it.

Glenn MIn step 1, mounting the iso, you state in the written step port to mount it to /opt/repo. However in your example and display snip, you have it as /var/repo. May be a bit confusing to some.

Catherine🙁 it didn’t work for me. After turned enable=0 on /etc/yum/pluginconf.d/subscription-manager.conf. Run sudo dnf repolist and install… All repos were removed.

# sudo dnf clean all

Updating Subscription Management repositories.

17 files removed

# sudo subscription-manager clean

All local data removed

# sudo vi /etc/yum/pluginconf.d/subscription-manager.conf

# sudo dnf repolist

No repositories available

# sudo dnf install nfs-utils

Error: There are no enabled repositories in “/etc/yum.repos.d”, “/etc/yum/repos.d”, “/etc/distro.repos.d”.

When I checked back to the redht.repo. It’s empty file.