setup local yum repository

How to Setup Local Yum Repository on CentOS/RHEL

Software repositories are collections of software packages that can be used by users to download, install and upgrade software packages on their system. Typically, these software repositories are located on the internet so that they can be accessed by many users. Package managers such as Yum maintain a list of software repositories to download & install packages on your system. Sometimes you may need to create and maintain a local software repository on your system, and allow access to other users on your network. This is required mostly if you need to maintain a repository in your organization’s intranet, or your software needs to be isolated from outside network. Such a configuration does not require internet connection since users can easily install software packages via your intranet/LAN. In this article, we will learn how to setup local yum repository on CentOS/RHEL. Yum is a popular package manager for CentOS/RHEL systems. It maintains a list of internet repositories to easily download & install packages. But you can configure it to fetch packages from your local repository too.


How to Setup Local Yum Repository on CentOS/RHEL

Here are the steps to setup local yum repository in CentOS/RHEL. Our yum repository will be hosted on an NGINX HTTP server on CentOS system while our client will also be a CentOS system.


1. Install NGINX Server

First we will install NGINX server to host our repository. Open terminal and run the following command to do the same from EPEL repository using Yum manager.

# yum install epel-release
# yum install nginx 

Once you have installed NGINX server, you can start it for the first time & enable it to automatically start on system reboot, using the following commands.

# systemctl start nginx
# systemctl enable nginx
# systemctl status nginx

Next, you need to open port 80 and 443 to allow web traffic to NGINX server, update firewall service to allow inbound HTTP and HTTPS, using the following commands.

# firewall-cmd --zone=public --permanent --add-service=http
# firewall-cmd --zone=public --permanent --add-service=https
# firewall-cmd --reload

Once you have installed NGINX server, you can test it by opening web browser and going to the following URL. Replace SERVER_DOMAIN_NAME_OR_IP with your server’s IP address or domain name.

http://SERVER_DOMAIN_NAME_OR_IP 


2. Create Local Yum Repository

Next, you need to run the following command to create, configure and manage your local repository.

# yum install createrepo  yum-utils

Next, create necessary directories that will store yum packages.

# mkdir -p /var/www/html/repos/{base,centosplus,extras,updates}

Next, we will run the following command to synchronize CentOS Yum repositories to local directories.

# reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/www/html/repos/
# reposync -g -l -d -m --repoid=centosplus --newest-only --download-metadata --download_path=/var/www/html/repos/
# reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/var/www/html/repos/
# reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/var/www/html/repos/

Here is a brief explanation of the various options used in above commands.

  • -g – enable removal of packages that fail GPG signature check after download
  • -l – enable yum plugin support.
  • -d – enable deletion of local packages no longer present in the repository
  • -m – enable download of comps.xml files.
  • --repoid – specify the repository ID.
  • --newest-only – tell reposync to only pull the latest version packages.
  • --download-metadata – enable download all the non-default metadata.
  • --download_path – specify the path to download packages.

Next, run the following command to check your local directories to ensure all packages are synchronized locally.

# ls -l /var/www/html/repos/base/
# ls -l /var/www/html/repos/base/Packages/
# ls -l /var/www/html/repos/centosplus/
# ls -l /var/www/html/repos/centosplus/Packages/
# ls -l /var/www/html/repos/extras/
# ls -l /var/www/html/repos/extras/Packages/
# ls -l /var/www/html/repos/updates/
# ls -l /var/www/html/repos/updates/Packages/

Next, create new repodata for local repositories by running following commands where flag -g is used to update package group information with the help of an XML file.

# createrepo -g comps.xml /var/www/html/repos/base/  
# createrepo -g comps.xml /var/www/html/repos/centosplus/	
# createrepo -g comps.xml /var/www/html/repos/extras/  
# createrepo -g comps.xml /var/www/html/repos/updates/  

Next, we need to create a server block in NGINX that will respond to requests for packages and repositories.

# vim /etc/nginx/conf.d/repos.conf 

Add the following lines to it. Add your server IP address or name in the part shown in bold.

server {
        listen   80;
        server_name  <server_name_or_ip>;
        root   /var/www/html/repos;
        location / {
                index  index.php index.html index.htm;
                autoindex on;	#enable listing of directory index
        }
}

Restart NGINX server to apply changes. Open browser and go to http://server_ip_or_domain.


3. Create Cron Job to Synchronize and Create Repositories

We will create a cron job that will automatically synchronize your local repos with official CentOS repos, download the latest updates and security patches.

# vim /etc/cron.daily/update-localrepos

Add the following lines to the above empty script file.

#!/bin/bash
##specify all local repositories in a single variable
LOCAL_REPOS=”base centosplus extras updates”
##a loop to update repos one at a time 
for REPO in ${LOCAL_REPOS}; do
reposync -g -l -d -m --repoid=$REPO --newest-only --download-metadata --download_path=/var/www/html/repos/
createrepo -g comps.xml /var/www/html/repos/$REPO/  
done

Save and exit the file. Run the following command to make it an executable.

# chmod 755 /etc/cron.daily/update-localrepos

Now this script will be automatically executed daily.


4. Setup Local Yum Repository on Client

You need to execute the following steps on client system (not server). For this purpose, add your local repos to Yum configuration.

# vim /etc/yum.repos.d/local-repos.repo

Add the following lines to it. Replace the part in bold with your server’s IP address or domain name.

[local-base]
name=CentOS Base
baseurl=http://<server_ip_or_domain>/base/
gpgcheck=0
enabled=1

[local-centosplus]
name=CentOS CentOSPlus
baseurl=http://<server_ip_or_domain>/centosplus/
gpgcheck=0
enabled=1

[local-extras]
name=CentOS Extras
baseurl=http://<server_ip_or_domain>/extras/
gpgcheck=0
enabled=1

[local-updates]
name=CentOS Updates
baseurl=http://<server_ip_or_domain>/updates/
gpgcheck=0
enabled=1

Save and exit the file.

Run the following command to view your local repos in list of available repos on client machines.

#  yum repolist
OR
# yum repolist all

You will see a list of available repositories in your system.

Also read:

How to List Files Installed in RPM or DEB Package
How to Change Console Fonts in Ubuntu Server
How to Set Password for Single User Mode in Linux
How to Change Kernel Parameters At Runtime
How to Install Kernel Headers in RHEL & CentOS

Leave a Reply

Your email address will not be published. Required fields are marked *