Skip to main content

How to mirror a repository in Linux

Discover how reposync makes it easy to mirror repositories in Red Hat Enterprise Linux 7 and 8.
storage cabinets with books in the background

Image by klimkin from Pixabay

Some of you might share the following use case: You need (or want) to mirror repos to provide packages for the hosts on your LAN that are not connected to the internet, and therefore cannot access the upstream repos directly. In this article, I explain how to accomplish this task easily by using reposync, which is shipped with your Red Hat Enterprise Linux (RHEL) 7 or Red Hat Enterprise Linux 8 subscription.

Before we get started, I would like to point out the important requirements. You need a valid subscription for every repo you would like to mirror because your mirror server can only sync those repos that are included in its own subscription. All of your other hosts that will use this mirror need to have a valid subscription, too.

If you have any questions about subscriptions, please take a look at the following URLs: the Red Hat subscription model FAQ and the Red Hat subscription service overview.

Setting up mirrored repos for Red Hat Enterprise Linux 7

To start, you’ll need to choose the web server that will serve the mirrored repos to your LAN. Then, if you’re using Red Hat Enterprise Linux 7, then to create a basic repo you have to start by installing createrepo. Don’t worry, this tool is included in your subscription, too. Install it on your web server with the following:

# yum install yum-utils createrepo

Next, you need the repository ID for the repo you would like to sync. Run yum repolist, and you’ll find the repo ID in the output’s first column:

$ yum repolist
Loaded plugins: langpacks, nvidia, product-id, search-disabled-repos, subscription-
              : manager
repo id                                    repo name                             status
rhel-7-server-optional-rpms/7Server/x86_64 Red Hat Enterprise Linux 7 Server - O 19.490
rhel-7-server-rpms/7Server/x86_64          Red Hat Enterprise Linux 7 Server (RP 26.766
virtualbox/7Server/x86_64                  Oracle Linux / RHEL / CentOS-7Server      77

In this case, rhel-7-server-rpms is the ID for the standard Red Hat Enterprise Linux 7 repo. You can get a complete list of repos your subscription has access to with the command:

$ sudo subscription-manager repos --list

You’ll find the repo ID in there, too.

For example, to sync the packages from the rhel-7-server-rpms repo and create a basic local repository, execute the following commands:

# reposync --gpgcheck -l --repoid=rhel-7-server-rpms --download_path=/var/www/html  # Sync RPMS
# cd /var/www/html/rhel-7-server-rpms
# createrepo -v /var/www/html/rhel-7-server-rpms  # Create the repo

Setting up mirrored repos for Red Hat Enterprise Linux 8

For Red Hat Enterprise Linux 8, this process is even easier. You only need the reposync command:

# reposync -p <download-path> --download-metadata --repo=<repo id>

The createrepo command is not needed anymore because reposync downloads everything, including the repodata.

Sharing the local repository

Once the packages are fetched and the local repo is created using either of these methods, the local repository can be distributed among the offline/disconnected systems using a simple web server, FTP, or NFS. The following knowledgebase articles provide the necessary information regarding your choice of distribution:

For more sophisticated setup than described in this article, consider using Red Hat Satellite.

[ Want to try out Red Hat Enterprise Linux? Download it now for free. ]

Topics:   Linux  
Author’s photo

Jörg Kastning

Jörg has been a Sysadmin for over ten years now. His fields of operation include Virtualization (VMware), Linux System Administration and Automation (RHEL), Firewalling (Forcepoint), and Loadbalancing (F5). More about me

Try Red Hat Enterprise Linux

Download it at no charge from the Red Hat Developer program.