(Last Updated On: August 12, 2018)
As a Linux System Admin, It’s alwasy an obligation to install sofware packages on a server.However, it’s not always compulsary to install packages from official CentOS repositories over the internet.A good example is when you have slow internet connection and would like to save bandwidth by creating a repository on LAN.

This guide will help you to exactly accomplish that.We have provided all steps needed to have a working local yum repository created using CentOS DVD ISO image.Below are steps used:
Step 1: Downloading CentOS-7-x86_64-Everything ISO or CentOS 7 DVD ISO image.
I’ll be using CentOS-7-x86_64-Everything ISO image which can be downloaded from download page in Official CentOS 7.You can also use CentOS 7 DVD ISO.
Step 2: Create Mount points and Mount ISO image.
After downloading, we’ll have to mount it.There are two mounting Options we can use:

  1. Mounting the ISO file to a local directory in our Linux system

a) Create a directory for mounting

[[email protected]]# mkdir /mnt/centos7

b) Mount ISO image of CentOS Everything or CentOS 7 DVD ISO

[[email protected]]# mount -t iso9660 -o loop CentOS-7-x86_64-Everything-1511.iso /mnt/centos7

c) Confirm it’s mounted using du or ls command.

[[email protected] ~]# du -sch /mnt/centos7/*
512     /mnt/centos7/CentOS_BuildTag
5.9M   /mnt/centos7/EFI
512    /mnt/centos7/EULA
18K   /mnt/centos7/GPL
466M /mnt/centos7/images
84M  /mnt/centos7/isolinux
280M /mnt/centos7/LiveOS
6.5G  /mnt/centos7/Packages
25M  /mnt/centos7/repodata
2.0K  /mnt/centos7/RPM-GPG-KEY-CentOS-7
2.0K  /mnt/centos7/RPM-GPG-KEY-CentOS-Testing-7
3.0K  /mnt/centos7/TRANS.TBL
7.4G total
[[email protected] ~]#
  1. Mounting the ISO image file to a web server directory.

I’m using Apache server running on another CentOS server.If you’re doing this on any other Linux server, replace the root directory of web server with the one provided here.
a) Create a directory for mounting

[[email protected]]# mkdir /var/www/html/centos/
[[email protected]]# mount -t iso9660 -o loop CentOS-7-x86_64-Everything-1511.iso /var/www/html/centos/

The output you get from last command is:
mount: /dev/loop0 is write-protected, mounting read-only
Contents of /var/www/html/centos/ directory should be as shown:

[[email protected]]# ls -lh /var/www/html/centos/
total 1.5M
-r--r--r--    1 root root 14 Dec 10 01:35 CentOS_BuildTag
dr-xr-xr-x3 root root 2.0K Dec 10 01:33 EFI
-r--r--r--    1 root root 215 Dec 10 01:35 EULA
-r--r--r--    1 root root 18K Dec 10 01:35 GPL
dr-xr-xr-x  3 root root 2.0K Dec 10 01:33 images
dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 isolinux
dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 LiveOS
dr-xr-xr-x 2 root root 1.5M Dec 10 01:34 Packages
dr-xr-xr-x 2 root root 4.0K Dec 10 01:35 repodata
-r--r--r--   1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-7
-r--r--r--   1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r-- 1 root root 2.9K Dec 10 01:36 TRANS.TBL
[[email protected] ~]#
  1. If you had CentOS 7 DVD ISO image on CD/DVD drive. Create a mount point and mount cdrom using the following commands:
[[email protected]]# mkdir -p /mnt/cent/cdrom
[[email protected]]# mount /dev/cdrom /mnt/cent/cdrom

You can see the contents of /mnt/cent/cdrom/

[[email protected]]# ls -lh /mnt/cent/cdrom/
total 1.5M
-r--r--r-- 1 root root 14 Dec 10 01:35 CentOS_BuildTag
dr-xr-xr-x 3 root root 2.0K Dec 10 01:33 EFI
-r--r--r--     1 root root 215 Dec 10 01:35 EULA
-r--r--r--     1 root root 18K Dec 10 01:35 GPL
dr-xr-xr-x 3 root root 2.0K Dec 10 01:33 images
dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 isolinux
dr-xr-xr-x 2 root root 2.0K Dec 10 01:33 LiveOS
dr-xr-xr-x 2 root root 1.5M Dec 10 01:34 Packages
dr-xr-xr-x 2 root root 4.0K Dec 10 01:35 repodata
-r--r--r--     1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-7
-r--r--r--     1 root root 1.7K Dec 10 01:35 RPM-GPG-KEY-CentOS-Testing-7
-r--r--r--     1 root root 2.9K Dec 10 01:36 TRANS.TBL
[[email protected] ~]#

Step 3: Create a repo and put it inside /etc/yum/repos.d/ directory. 
This is done on Server with CentOS minimal installed, or server that you need to install packages on while pulling the packages from local reposotory you just added.
a) For Step 2 part 1:

[[email protected] ~]# cat >> /etc/yum.repos.d/centos7-local.repo<< EOF
[centos7-local]
name=centos7-local
baseurl=file:///mnt/centos7/
enabled=0
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF

b) For Step 2 part 2: : The IP address of the server I’m using is 192.168.1.60. Below is the repo configuration.

[[email protected] ~]# cat > /etc/yum.repos.d/centos7-local.repo<< EOF
[centos7-local]
name=centos7-local
baseurl=http://192.168.1.60/centos
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF

c) For Step 2 part 3: The mount point we used was /mnt/centos/cdrom/.

[[email protected] ~]# cat > /etc/yum.repos.d/centos7-local.repo<< EOF
[centos7-local]
name=centos7-local
baseurl=file:///mnt/cent/cdrom/
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF

Check the repo added:

[[email protected] ~]# cat /etc/yum.repos.d/centos7-local.repo
[centos7-local]
name=centos7-local
baseurl=http://192.168.1.60/centos
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

Then do yum clean all remove cached date and update repo list

[[email protected] ~]# yum clean all
Loaded plugins: fastestmirror, langpacks, priorities
Cleaning repos: base epel extras remi-safe updates
Cleaning up everything
Cleaning up list of fastest mirrors

NOTE: Enabling the repository locally added alone is not necessary enough to let us install software packages from it.This is because by default, any package being installed will use CentOS Base repository.We’ll discuss on a working solution to this shortly.
Package groups available in CentOS 7 repository are:

[[email protected]~]# yum group list
Loaded plugins: fastestmirror
There is no installed groups file.
Maybe run: yum groups mark convert (see man yum)
Loading mirror speeds from cached hostfile
* base: mirror.bitco.co.za
* epel: epel.mirrors.ovh.net
* extras: mirror.bitco.co.za
* updates: mirror.bitco.co.za
Available Environment Groups:
Minimal Install
Compute Node
Infrastructure Server
File and Print Server
MATE Desktop
Basic Web Server
Virtualization Host
Server with GUI
GNOME Desktop
KDE Plasma Workspaces
Development and Creative Workstation
Available Groups:
CIFS file server
Compatibility Libraries
Console Internet Tools
Desktop
Desktop Platform
Desktop Platform Development
Development Tools
Eclipse
Educational Software
Electronic Lab
FCoE Storage Client
Fedora Packager
General Purpose Desktop
Graphical Administration Tools
Haskell
Legacy UNIX Compatibility
Messaging Client Support
Messaging Server Support
Milkymist
MySQL Database client
MySQL Database server
NFS file server
Network Storage Server
SNMP Support
Scientific Support
Security Tools
Server Platform
Server Platform Development
Smart Card Support
Storage Availability Tools
System Administration Tools
System Management
TeX support
TurboGears application framework
Virtualization
Web-Based Enterprise Management
Xfce
iSCSI Storage Client
Done
[[email protected] ~]#

NOTE: When other CentOS repos are enabled and you specify centos-local repo, yum will always try to download latest package from repo with the latest package.The default repo being CentOS Base repo.
If you want to force yum to use local repository, you have to disable all other repos and enable them once you are done.You have two options you can use to achieve this.
Option 1: The simplest solution can be like below:

[[email protected] ~]# mkdir ~/repos
[[email protected] ~]# cp -r /etc/yum.repos.d/* ~/repos/
[[email protected] ~]# ls -l ~/repos/
total 56
-rw-r--r-- 1 root root 143 Mar 17 02:33 centos7-local.repo
-rw-r--r-- 1 root root 1664 Mar 17 02:33 CentOS-Base.repo
-rw-r--r-- 1 root root 1309 Mar 17 02:33 CentOS-CR.repo
-rw-r--r-- 1 root root 649 Mar 17 02:33 CentOS-Debuginfo.repo
-rw-r--r-- 1 root root 290 Mar 17 02:33 CentOS-fasttrack.repo
-rw-r--r-- 1 root root 630 Mar 17 02:33 CentOS-Media.repo
-rw-r--r-- 1 root root 516 Mar 17 02:33 CentOS-OpenStack-liberty.repo
-rw-r--r-- 1 root root 1331 Mar 17 02:33 CentOS-Sources.repo
-rw-r--r-- 1 root root 1952 Mar 17 02:33 CentOS-Vault.repo
-rw-r--r-- 1 root root 957 Mar 17 02:33 epel.repo
-rw-r--r-- 1 root root 1056 Mar 17 02:33 epel-testing.repo
-rw-r--r-- 1 root root 1177 Mar 17 02:33 remi-php70.repo
-rw-r--r-- 1 root root 2340 Mar 17 02:33 remi.repo
-rw-r--r-- 1 root root 449 Mar 17 02:33 remi-safe.repo
[[email protected] ~]# 
[[email protected] ~]# rm -rf /etc/yum.repos.d/*
[[email protected] ~]# ls -l /etc/yum.repos.d/
total 0

Then add add local repo.Do something like:

[[email protected] ~]#cat > /etc/yum.repos.d/centos7-local.repo<< EOF
[centos7-local]
name=centos7-local
baseurl=http://192.168.1.60/centos
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7
EOF

Check to see if successfully added:

[[email protected] ~]# cat /etc/yum.repos.d/centos7-local.repo 
[centos7-local]
name=centos7-local
baseurl=http://192.168.1.60/centos
enabled=1
gpgcheck=1
gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-7

You can do any installation.The only repo that yum will use is centos7-local. Examples:

  1. To install LDAP Service to configure OpenLDAP server:
[[email protected] ~]# yum install openldap-servers openldap-clients 
Loaded plugins: fastestmirror, langpacks, priorities
centos7-local | 3.6 kB 00:00:00 
Loading mirror speeds from cached hostfile
Resolving Dependencies
--> Running transaction check
---> Package openldap-clients.x86_64 0:2.4.40-8.el7 will be installed
---> Package openldap-servers.x86_64 0:2.4.40-8.el7 will be installed
--> Finished Dependency Resolution 
Dependencies Resolved 
=======================================================================================
Package Arch Version Repository Size
=======================================================================================
Installing:
openldap-clients x86_64 2.4.40-8.el7 centos7-local 186 k
openldap-servers x86_64 2.4.40-8.el7 centos7-local 2.1 M 
Transaction Summary
========================================================================================
Install 2 Packages 
Total download size: 2.3 M
Installed size: 5.2 M
Is this ok [y/d/N]: y
Downloading packages:
(1/2): openldap-clients-2.4.40-8.el7.x86_64.rpm | 186 kB 00:00:00 
(2/2): openldap-servers-2.4.40-8.el7.x86_64.rpm | 2.1 MB 00:00:00 
-----------------------------------------------------------------------------------------------------------------------------------------------------
Total 21 MB/s | 2.3 MB 00:00:00 
Running transaction check
Running transaction test
Transaction test succeeded
Running transaction
Installing : openldap-servers-2.4.40-8.el7.x86_64 1/2 
Installing : openldap-clients-2.4.40-8.el7.x86_64 2/2 
Verifying : openldap-clients-2.4.40-8.el7.x86_64 1/2 
Verifying : openldap-servers-2.4.40-8.el7.x86_64 2/2 
Installed:
openldap-clients.x86_64 0:2.4.40-8.el7 openldap-servers.x86_64 0:2.4.40-8.el7 
Complete!
[[email protected] ~]#

As you you can see from below screenshot, the repository used is centos7-local.

  1. To install all packages belonging to “GNOME Desktop” Environment Group, we’ll do:
[[email protected] ~]# yum groupinstall "GNOME Desktop"
  1. To install single package like mariadb,mariadb-server, we’ll do:
[[email protected] ~]# yum install mariadb mariadb-server

Option 2: Using –enablerepo=centos7-local command option and –disablerepo=* command option,
For example;
To install all packages belonging to “Virtualization Host” Environment Group, we’ll do:

[[email protected] ~]# yum --disablerepo=* --enablerepo=centos7-local groupinstall "Virtualization Host"

To install openldap

[[email protected] ~]# yum --disablerepo=* --enablerepo=centos7-local install openldap-servers openldap-clients

If you had CentOS 7 Minimal version and installed GNOME Desktop Environment Group packages, don’t forget to tell systemd to boot to GNOME Desktop Automatically.

[[email protected] ~]# ln -sf /lib/systemd/system/graphical.target /etc/systemd/system/default.target

Later on you can return the repos to original directory:

[[email protected] ~]# cp -r ~/repos/* /etc/yum.repos.d/

That’s all. Remember to copy repositories back from ~/repos/ to  /etc/yum.repos.d/  directory.