How to create local repository on CentOS 7 using reposync

A software repository (“repo” in short) is a central file storage location to keep and maintain software packages, from which users can retrieve packages and install on their computers.

Repositories are often stored on servers on a network for example the internet, which can be accessed by multiple users. However, you can create and configure a local repository on your computer and access it as a single user or allow access to other machines on your LAN (Local Area Network).

One advantage of a setting up a local repository is that you don’t need internet connection to all Linux server for installing software packages.

YUM (Yellowdog Updater Modified) is a widely used package management tool for RPM (RedHat Package Manager) based Linux systems, which makes software installation easy on Red Hat/CentOS Linux.

In this article, we will explain how to setup a local YUM repository over FTP on CentOS 7 VPS and also show you how to find and install software packages on client CentOS 7 machines.

Our Testing Environment

Yum Repository Server: CentOS 7 [192.168.2.10]

Client Machine: CentOS 7 [192.168.2.11]

Step 1: Install FTP Services.

First start by installing VSFTP server from the centos repository using the YUM package manager as follows.

# yum install vsftpd 

2. Once you have installed vsftpd, you can start it for the first time and enable it to start automatically at system boot.

# systemctl start vsftpd

# systemctl enable vsftpd

# systemctl status vsftpd

3. Next, you need to open port 21 to allow traffic to FTP service, update the system firewall rules to permit inbound packets on FTP using the commands below.

# firewall-cmd --zone=public --permanent --add-service=ftp

# firewall-cmd --reload

4. Now you can confirm that your ftp server is up and running, using the following URL.

ftp://192.168.2.10 

Step 2: Create Yum Local Repository

5. In this step, you need to install the required packages for creating, configuring and managing your local repository.

# yum install createrepo  yum-utils

6. Next, create the necessary directories (yum repositories) that will store packages and any related information.

# mkdir -p /var/ftp/pub/repos/{base,centosplus,extras,updates}

Then use the reposync tool to synchronize CentOS YUM repositories to the local directories as shown.

# reposync -g -l -d -m --repoid=base --newest-only --download-metadata --download_path=/var/ftp/pub/repos/

# reposync -g -l -d -m --repoid=centosplus --newest-only --download-metadata --download_path=/var/ftp/pub/repos/

# reposync -g -l -d -m --repoid=extras --newest-only --download-metadata --download_path=/var/ftp/pub/repos/

# reposync -g -l -d -m --repoid=updates --newest-only --download-metadata --download_path=/var/ftp/pub/repos/

In the above commands, the option:

-g – enables removing of packages that fail GPG signature checking after downloading.

-l – enables yum plugin support.

-d – enables deleting of local packages no longer present in repository.

-m – enables downloading of comps.xml files.

--repoid – specifies the repository ID.

--newest-only – tell reposync to only pull the latest version of each package in the repos.

--download-metadata – enables downloading all the non-default metadata.

--download_path – specifies the path to download packages.

8. Next, check the contents of your local directories to ensure that all the packages have been synchronized locally.

# du -sh /var/ftp/pub/repos/base/

# ls -l /var/ftp/pub/repos/base/Packages/ | wc -l

# du -sh /var/ftp/pub/repos/centosplus/

# ls -l /var/ftp/pub/repos/centosplus/Packages/ | wc -l

# du -sh /var/ftp/pub/repos/extras/

# ls -l /var/ftp/pub/repos/extras/Packages/ | wc -l

# du -sh /var/ftp/pub/repos/updates/

# ls -l /var/ftp/pub/repos/updates/Packages/ | wc -l

9. Now create a new repodata for the local repositories by running the following commands, where the flag -g is used to update the package group information using the specified .xml file.

# createrepo -g comps.xml /var/ftp/pub/repos/base/  

# createrepo -g comps.xml /var/ftp/pub/repos/centosplus/

# createrepo -g comps.xml /var/ftp/pub/repos/extras/  

# createrepo -g comps.xml /var/ftp/pub/repos/updates/  

10. Then restart your ftp server and view the repositories from a web browser using the following URL.

ftp://192.168.2.10/pub/

Step 3: Create Cron Job to Synchronize and Create Repositories

11. Next, add a cron job that will automatically synchronize your local repos with the official CentOS repos to grab the updates and security patches.

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

Add these commands in the script.

#!/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/ftp/pub/repos/

createrepo -g comps.xml /var/ftp/pub/repos/$REPO/  

done

Save the script and close it and set the appropriate permissions on it.

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

Step 4: Setup Local Yum Repository on Client Machines

12. Now on your CentOS client machines, add your local repos to the YUM configuration.

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

Copy and paste the configuration below in the file local-repos.repo (make changes where necessary).

[local-base]

name=CentOS Base

baseurl=ftp://10.28.198.122/pub/repos/base/

gpgcheck=0

enabled=1

[local-centosplus]

name=CentOS CentOSPlus

baseurl=ftp://10.28.198.122/pub/repos/centosplus/

gpgcheck=0

enabled=1

[local-extras]

name=CentOS Extras

baseurl=ftp://10.28.198.122/pub/repos/extras/

gpgcheck=0

enabled=1

[local-updates]

name=CentOS Updates

baseurl=ftp://10.28.198.122/pub/repos/updates/

gpgcheck=0

enabled=1

Save the file and start using your local YUM mirrors.

14. Next, run the following command to view your local repos in the list of available YUM repos, on the client machines.

#  yum repolist

OR

# yum repolist all

No comments:

Post a Comment