How to Configure YUM Repositories in CentOS/RHEL 7 and 8

Enabling RedHat Software Repositories

Registering a system to the subscription management service automatically configures access to software repositories based on the attached subscriptions. To view all available repositories:

[user@host ~]$ yum repolist all
Loaded plugins: langpacks

repo id                                    repo name                                        status
rhel-8-server-debug-rpms/8Server/x86_64    Red Hat Enterprise Linux 8 Server (Debug RPMs)   disabled
rhel-8-server-rpms/8Server/x86_64          Red Hat Enterprise Linux 8 Server (RPMs)         enabled: 5,071
rhel-8-server-source-rpms/8Server/x86_64   Red Hat Enterprise Linux 8 Server (Source RPMs)  disabled
repolist: 5,071

The yum-config-manager command can be used to enable or disable repositories. To enable a repository, the command sets the enabled parameter to 1. For example, the following command enables the rhel-8-server-debug-rpms repository:


[user@host ~]$ yum-config-manager --enable rhel-8-server-debug-rpms
Loaded plugins: langpacks
===================== repo: rhel-8-server-debug-rpms ======================
[rhel-8-server-debug-rpms]
async = True
bandwidth = 0
base_persistdir = /var/lib/yum/repos/x86_64/8Server
baseurl = https://cdn.redhat.com/content/dist/rhel/server/8/8Server/x86_64/debug
cache = 0
cachedir = /var/cache/yum/x86_64/8Server/rhel-8-server-debug-rpms
check_config_file_age = True
cost = 1000
deltarpm_percentage =
enabled = 1
...output omitted...

Non-Red Hat sources provide software through third-party repositories, which can be accessed by the yum command from a website, FTP server, or the local file system. For example, Adobe provides some of its software for Linux through a Yum repository. In a Red Hat classroom, the content.example.com classroom server hosts Yum repositories.

To enable support for a new third-party repository, create a file in the /etc/yum.repos.d/ directory. Repository configuration files must end with a .repo extension. The repository definition contains the URL of the repository, a name, whether to use GPG to check the package signatures and if so, the URL pointing to the trusted GPG key.

Creating Yum Repositories

Create Yum repositories with the yum-config-manager command. The following command creates a file named /etc/yum.repos.d/dl.fedoraproject.org_pub_epel_8_x86_64_.repo with the output shown:

[user@host ~]$ yum-config-manager --add-repo="http://dl.fedoraproject.org/pub/epel/8/x86_64/"
Loaded plugins: langpacks
adding repo from: http://dl.fedoraproject.org/pub/epel/8/x86_64/

[dl.fedoraproject.org_pub_epel_8_x86_64_]
name=added from: http://dl.fedoraproject.org/pub/epel/8/x86_64/ baseurl=http://dl.fedoraproject.org/pub/epel/8/x86_64/
enabled=1

Modify this file to provide customized values and location of a GPG key. Keys are stored in various locations on the remote repository site, such as, http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-8. Administrators should download the key to a local file rather than allowing yum to retrieve the key from an external source. For example:

[EPEL]
name=EPEL 8
baseurl=http://dl.fedoraproject.org/pub/epel/8/x86_64/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8

RPM Configuration Packages for Local Repositories

Some repositories provide a configuration file and GPG public key as part of an RPM package that can be downloaded and installed using the yum localinstall command. For example, the volunteer project called Extra Packages for Enterprise Linux (EPEL) provides software not supported by Red Hat but compatible with Red Hat Enterprise Linux

The following command installs the Red Hat Enterprise Linux 8 EPEL repo package:

[user@host ~]$ rpm --import http://dl.fedoraproject.org/pub/epel/RPM-GPG-KEY-EPEL-8
[user@host ~]$ yum install http://dl.fedoraproject.org/pub/epel/8/x86_64/e/epel-release-8-2.noarch.rpm

Configuration files often list multiple repository references in a single file. Each repository reference begins with a single-word name in square brackets.

[user@host ~]$ cat /etc/yum.repos.d/epel.repo
[epel]
name=Extra Packages for Enterprise Linux 8 - $basearch
#baseurl=http://download.fedoraproject.org/pub/epel/8/$basearch mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-8&arch=$basearch
failovermethod=priority
enabled=1
gpgcheck=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8

[epel-debuginfo]
name=Extra Packages for Enterprise Linux 8 - $basearch - Debug #baseurl=http://download.fedoraproject.org/pub/epel/8/$basearch/debug mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-debug-8&arch=
$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
gpgcheck=1

[epel-source]
name=Extra Packages for Enterprise Linux 8 - $basearch - Source #baseurl=http://download.fedoraproject.org/pub/epel/8/SRPMS
mirrorlist=https://mirrors.fedoraproject.org/metalink?repo=epel-source-8&arch=
$basearch
failovermethod=priority
enabled=0
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-EPEL-8
gpgcheck=1

To define a repository, but not search it by default, insert the enabled=0 parameter. Repositories can be enabled and disabled persistently with yum-config-manager command or temporarily with yum command options, –enablerepo=PATTERN and –disablerepo=PATTERN.