Hey good people. Today in this short tutorial I’ll share with you tips on how to create local CentOS 6 and CentOS 7 repository mirrors. This is beneficial when you have a good number of systems and you want to save on bandwidth, fasten package installations, network installations and systems updates.

This guide will use rsync to sync the mirrors from upstream. Path used to save sync contents is /var/mirrors/. Make sure you adjust the path to a filesystem that has enough space, or mount a new disk/partition.

Bash Scripts to sync CentOS 6 and 7 mirrors:

Below script will sync both CentOS 6 and CentOS 7 mirrors. substitute directory variables with your desired. Save script to a file, make

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then
    echo "Updates via rsync already running."
    exit 0
fi

base_dir="/var/mirrors/centos/"
centos_7_mirror_dir="/var/mirrors/centos/7/"
centos_6_mirror_dir="/var/mirrors/centos/6/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$centos_7_mirror_dir"  && -d "$centos_6_mirror_dir" ]] ; then
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/7/  "$centos_7_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/6/  "$centos_6_mirror_dir" && \
    # sync keys
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-7  "$base_dir" && \
    rsync  -avSHP --delete rsync://mirror.liquidtelecom.com/centos/RPM-GPG-KEY-CentOS-6  "$base_dir" && \
    rm -rf /var/lock/subsys/rsync_updates

else
	echo "Directories doesn't exist"

fi

Bash Script to sync Epel mirrors:

#!/bin/bash

if [ -f /var/lock/subsys/rsync_updates ]; then
    echo "Updates via rsync already running."
    exit 0
fi

epel6_mirror_dir="/var/mirrors/epel/6/x86_64"
epel7_mirror_dir="/var/mirrors/epel/7/x86_64"
base_dir="/var/mirrors/epel/"

touch /var/lock/subsys/rsync_updates

if [[ -d "$epel6_mirror_dir"  && -d "$epel7_mirror_dir" ]] ; then
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/6/x86_64/ "$epel6_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/7/x86_64/ "$epel7_mirror_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-7 "$base_dir" && \
    rsync  -avSHP --delete rsync://mirror.wbs.co.za/fedora-epel/RPM-GPG-KEY-EPEL-6 "$base_dir" && \
    rm -rf /var/lock/subsys/rsync_updates

else
	echo "Directories doesn't exist"

fi

if [[ $? -eq '0' ]]; then
    echo ""
    echo "Sync successful.."
else
    echo " Syncing failed"
    exit 1
fi

These scipts can be run in cron to keep the mirrors current.

Serve the mirrors with Nginx

After the sync is complete, you can begin using them by serving with nginx,

Install nginx:

yum -y install epel-release
yum -y install nginx

Then add configuration files:

cat /etc/nginx/conf.d/centos.conf
server {
	listen 80;
	server_name centos.mirrors.domain.com;
        root /var/mirrors/centos/;

       location / {
                autoindex on;
        }
}

For EPEL:

cat /etc/nginx/conf.d/epel.conf
server {
	listen 80;
	server_name epel.mirrors.domain.com;
        root /var/mirrors/epel;

       location / {
                autoindex on;
        }
}

Start and enable nginx:

systemctl start nginx
systemctl enable nginx

Configuring CentOS Server to use the mirrors

CentOS Base mirrors:

cd /etc/yum.repos.d/
sudo mkdir old
sudo mv CentOS* old
sudo vim centos-local.repo

Add content below.


[base]
name=CentOS-$releasever - Base
baseurl=http://centos.mirrors.domain.com/$releasever/os/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7


[updates]
name=CentOS-$releasever - Updates
baseurl=http://centos.mirrors.domain.com/$releasever/updates/$basearch/
enabled=1
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[extras]
name=CentOS-$releasever - Extras
baseurl=http://centos.mirrors.domain.com/$releasever/extras/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

[centosplus]
name= CentOS-$releasever - Plus
baseurl=http://centos.mirrors.domain.com/$releasever/centosplus/$basearch/
enabled=0
gpgcheck=1
gpgkey=http://centos.mirrors.domain.com/$releasever/os/$basearch/RPM-GPG-KEY-CentOS-7

Test that the repos work just fine by updating repo cache.

sudo yum clean all
sudo yum makecache fast
sudo yum -v repolist

Epel Mirrors:

$ sudo vim /etc/yum.repos.d/epel.repo
[epel]                                    
name=Extra Packages for Enterprise Linux 7 - $basearch                               
baseurl=http://epel.mirrors.domain.com/$releasever/$basearch/                         
enabled=1                                 
gpgcheck=1                                
gpgkey=http://epel.mirrors.domain.com/RPM-GPG-KEY-EPEL-$releasever

Also check:

How To Sync CentOS 8 repositories on Katello / Satellite

Your support is our everlasting motivation,
that cup of coffee is what keeps us going!


As we continue to grow, we would wish to reach and impact more people who visit and take advantage of the guides we have on our blog. This is a big task for us and we are so far extremely grateful for the kind people who have shown amazing support for our work over the time we have been online.

Thank You for your support as we work to give you the best of guides and articles. Click below to buy us a coffee.

LEAVE A REPLY

Please enter your comment!
Please enter your name here