RHEL7: Set up a local repository for a lab.

Share this link

To be able to quickly deploy new VMs, you need a local repository.

Install the Apache package:

# yum group install -y "Basic Web Server"

Create the directories for the CentOS distribution and the updates:

# cd /var/www/html
# mkdir -p conf repo/CentOS/7
# cd repo/CentOS/7
# mkdir -p centosplus/x86_64 extras/x86_64 os/x86_64 updates/x86_64

In the software requirements, it was specified to download the DVD version or the Everything version of the CentOS 7 distribution. It is now time to use it.

Mount the previously downloaded iso and copy it into the right place:

# mount -o loop CentOS-7.0-1406-x86_64-DVD.iso /mnt
# cd /mnt
# tar cvf - . | (cd /var/www/html/repo/CentOS/7/os/x86_64; tar xvf -)
# cd /; umount /mnt

In order to prepare the centosplus, extras and updates repositories, install the createrepo package:

# yum install createrepo

Prepare the centosplus, extras and updates repositories:

# cd /var/www/html/repo/CentOS/7/centosplus/x86_64; createrepo .
# cd ../../extras/x86_64; createrepo .
# cd ../../updates/x86_64; createrepo .

Go into the directory where the repository configuration is:

# cd /etc/yum.repos.d

Create the local.repo file and paste the following lines:

name=master - Base

name=master - Updates

name=master - Extras

name=master - CentosPlus

Note1: A yum update command will update the system with what the local Apache server’s got.
Note2: Don’t try to display the $releasever and $basearch variables with the echo command. They aren’t shell variables but specific values provided by yum (see this thread for more details).

Copy the local.repo file into the /var/www/html/conf directory to make it available to the new VMs:

# cp local.repo /var/www/html/conf

Assign the SELinux attributes to the files:

# restorecon -R /var/www/html

Activate the Apache server at boot and start it:

# systemctl enable httpd && systemctl start httpd

Clean up the yum configuration:

# yum clean all

Check the yum configuration:

# yum repolist all
Loaded plugins: fastestmirror, langpacks
centosplus                                            | 2.9 kB     00:00
extras                                                | 2.9 kB     00:00
os                                                    | 3.6 kB     00:00
updates                                               | 2.9 kB     00:00
(1/2): os/7/x86_64/group_gz                             | 157 kB   00:00
(2/2): os/7/x86_64/primary_db                           | 4.9 MB   00:00
Loading mirror speeds from cached hostfile
repo id                        repo name                       status
centosplus/7/x86_64            master - CentosPlus             enabled:     0
extras/7/x86_64                master - Extras                 enabled:     0
os/7/x86_64                    master - Base                   enabled: 8,465
updates/7/x86_64               master - Updates                enabled:     0
repolist: 8,465

Finally, you need an extra step to regularly update your lab local repository.
Create a Bash script called /root/rsync.sh and paste the following lines (where mirror.ovh.net/ftp.centos.org/7/ is the url of a close CentOS repository):

/usr/bin/rsync -av --delete --delete-excluded --exclude "local" --exclude "isos" --exclude "*.iso" --exclude "xen4" --exclude "*.i686.*" --exclude "i386" rsync://centos.mirrors.ovh.net/ftp.centos.org/7/ /var/www/html/repo/CentOS/7

Give the execution permissions:

# chmod u+x /root/rsync.sh

Put this script into the root crontab (# crontab -e):

00 02 * * * /root/rsync.sh

Note: The first synchronization will last a while but, after, it will be quick.

After synchronization, you will get this kind of result:

# yum repolist all
Loaded plugins: fastestmirror, langpacks
Loading mirror speeds from cached hostfile
repo id                          repo name                        status
centosplus/7/x86_64              master - CentosPlus              enabled:    70
extras/7/x86_64                  master - Extras                  enabled:    44
os/7/x86_64                      master - Base                    enabled: 8,465
updates/7/x86_64                 master - Updates                 enabled:   866
repolist: 9,445
1 Star2 Stars3 Stars4 Stars5 Stars (No Ratings Yet)

Leave a Reply

6 Comments on "RHEL7: Set up a local repository for a lab."

Notify of

Sort by:   newest | oldest
1 year 5 months ago

Depending on the disc you used to install CentOS 7, you may need to remove the currently existing repos to achieve the same output — and avoid a warning about each repo “being defined twice.”

Just do an rm /etc/yum.repos.d/* before you create your local.repo, and you should be set!

6 months 14 days ago
Hi, thanks for making such a great site! I have purchased the new RHEL 7 book by Michael Jang which you recomended which is being released today (8 April) – I can’t wait to receive it shortly. I have been following your lab tutorial and I have a problem with the rsync.sh command… when I type this into my terminal it gives me an error is this normal? [root@master yum.repos.d]# /usr/bin/rsync -av –delete –delete-excluded –exclude “local” –exclude “isos” –exclude “*.iso” –exclude “xen4” –exclude “*.i686.*” –exclude “i386” rsync://mirror.ovh.net/ftp.centos.org/7/ /var/www/html/repo/CentOS/7 @ERROR: Unknown module ‘ftp.centos.org’ rsync error: error starting client-server protocol (code 5)… Read more »

RHCSA7: Task of the day

Allowed time: 15 minutes.
Get an iso image of CentOS 7 (or a DVD) and set up a local repository.

RHCE7: Task of the day

Allowed time: 3 minutes.
Configure your machine to be a router.

Poll for favorite RHEL 7 book

What is your favorite RHEL 7 book to prepare RHCSA & RHCE exams?

View Results

Loading ... Loading ...

Poll for most difficult RHCSA 7 topic

What do you think is the most difficult RHCSA 7 topic?

View Results

Loading ... Loading ...

Poll for most difficult RHCE 7 topic

What do you think is the most difficult RHCE 7 topic?

View Results

Loading ... Loading ...