How to Configure CentOS Local Yum Repository?



Yum repository is a central database of software that is used for installation/upgrade of packages on a Red Hat/CentOS based Linux server. This repository includes software that are used by system as well as for user level programs.

An administrator can set-up a custom local yum repository instead of using the default ones provided by the OS for several reasons like: to provide normal or specialized software packages possibly in a restricted network environment; to maintain version levels across a set of servers; to optimize network bandwidth usage; to adhere to organization policy for maintaining security or possibly a combination of these points.

To create a local repository, you'll need access to an account with superuser (root) level privileges on a CentOS server over a terminal window or SSH session.

Install Required Packages

To help with the setup of custom repositories, we'll need to install a couple of packages, if not already installed on the system. Issue below command to install httpd, createrepo and yum-utils package.

$ sudo dnf install -y httpd createrepo yum-utils

Configure Apache Web Server

Enable and start Apache web server (httpd).

$ sudo systemctl enable --now httpd

If OS firewall is enabled, allow http service via firewall and reload its configuration.

$ sudo firewall-cmd --permanent --add-service=http
$ sudo firewall-cmd --reload

If SELinux is enabled, you will need to configure SELinux context for Apache as below:

$ sudo semanage fcontext -a -t httpd_sys_content_t "/var/www/html/localrepo(/.*)?"
$ sudo restorecon -Rv /var/www/html/localrepo

Create Local Repo Directory

We need local directories to map the official repository directory structure (or to simply use custom repository for custom packages) and sync files to it. We'll be creating a directory named localrepo. More such directories can be created for other such repositories, if needed, like base, centos, extras and updates for mapping the repositories provided by CentOS.

$ sudo mkdir -p /var/www/html/localrepo

The reason we've created directories inside /var/www/html is because it is the default base directory served by httpd service.

Sync Repo Directory

Depending on your requirements, you can either put specific RPM files with their dependencies to be served by the repo in localrepo path or sync a complete repository from official or custom stores.

For downloading specific packages and their dependencies, you can use yumdownloader command as shown next:

$ sudo yumdownloader --destdir=/var/www/html/localrepo package_name

Where package_name is the name of one or more packages separated by spaces, ideally with their dependencies included.

Or for the latter option, to sync from an existing repository using reposync command:

$ sudo reposync -p /var/www/html/localrepo --download-metadata --repo=baseos

Create Repo Metadata

Next step is to generate the repository metadata using createrepo_c command.

$ sudo createrepo_c /var/www/html/localrepo

If you need to rerun the createrepo_c command later for updating changes to local repository, use:

$ sudo createrepo_c --update /var/www/html/localrepo

Configure Local Repository

Local repository is now ready to be used but we need to specify the baseurl parameter in our repo configuration files. If the repository packages have conflicts with packages in other repositories, it is better to resolve those conflicts first.

If the local repository is going to be the only repository configured or the client machine will have limited Internet connectivity, remove other/default repositories.

You can move all the existing repositories from /etc/yum.repos.d, which will also act as its backup in case you need to restore these configuration files because of any issues.

$ sudo mv /etc/yum.repos.d/*.repo /var/tmp

Locally on the repository server which is serving the yum (HTTP) content, you can create the local.repo file using vi or nano tool:

$ sudo vi /etc/yum.repos.d/local.repo

And add the following content to it:

[local]
name=CentOS 9 Local Repository
baseurl=file:///var/www/html/localrepo
enabled=1
gpgcheck=0

On a different CentOS machine(s) which will become client for this yum repository, the content of local.repo would be slightly modified:

[local]
name=CentOS 9 Local Repository
baseurl=http://server-ip/localrepo
enabled=1
gpgcheck=0

Where server-ip is the IP or resolvable DNS of our yum server.

Verifying Repository

We can test whether CentOS is able to read the package list from our created repo using dnf and its sub-commands.

$ sudo dnf repolist
$ sudo dnf repository-packages local list
$ sudo dnf install package-name

Conclusion

Creating local yum repository is a common administrator's activity which can be accomplished in few steps on CentOS as shown above.

Besides creating the repository and verifying from local client machines, as best practices, you should also:

  • Implement automation for regular repository updates
  • Use version control for package management
  • Properly document custom packages and their dependencies
  • Maintain regular backup of repository data and its configuration
  • Implement alerts for disk usage.
Updated on: 2025-01-28T16:22:28+05:30

178 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements