Local YUM repository on Centos 7

Repository – the place where any data is stored and maintained. Most often, the data in the repository is stored in the form of files available for further distribution over the network. Among Linux distributions, repositories with the YUM metadata format for RPM-based distributions, and repositories with APT metadata for DEB-based distributions are popular.

Install software

$ sudo yum install createrepo yum-utils

Create directories os, updates, extras

$ mkdir -p /var/www/repo/centos/7/{os,updates,extras}/x86_64

For synchronization, we will use the Yandex mirror, since they have declared support for rsync (873 outgoing port)

$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/os/x86_64/ /var/www/repo/centos/7/os/x86_64/
$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/updates/x86_64/ /var/www/repo/centos/7/updates/x86_64/
$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/extras/x86_64/ /var/www/repo/centos/7/extras/x86_64/

Next, we need to bring up the web server. An example of NGINX configuration:

$ sudo cat /etc/nginx/site-avaliable/repo.conf
server {
    listen 80 default_server;
    server_name _;
    root /var/www/repo;
    charset UTF-8;
    default_type text/plain;

    location / {
        autoindex_exact_size off;
        autoindex on;
    }

    error_page   500 502 503 504  /50x.html;
    location = /50x.html {
        root /usr/share/nginx/html;
    }
}

Create repositories

$ createrepo -v /var/www/repo/centos/7/os/x86_64
$ createrepo -v /var/www/repo/centos/7/updates/x86_64
$ createrepo -v /var/www/repo/centos/7/extras/x86_64

EPEL local repository

To create a local EPEL repository, create a directory

$ mkdir -p /var/www/repo/centos/7/epel/x86_64

Synchronize with mirror.logol.ru, because they have declared rsync support for EPEL mirror

$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.logol.ru/epel/7/x86_64/ /var/www/repo/centos/7/epel/x86_64/

Create a repository

$ createrepo -v /var/www/repo/centos/7/epel/x86_64

Updating repositories

To update the repositories, you need to synchronize with the source

$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/os/x86_64/ /var/www/repo/centos/7/os/x86_64/
$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/updates/x86_64/ /var/www/repo/centos/7/updates/x86_64/
$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/extras/x86_64/ /var/www/repo/centos/7/extras/x86_64/
$ rsync -iavrt --delete --exclude="repo*" rsync://mirror.logol.ru/epel/7/x86_64/ /var/www/repo/centos/7/epel/x86_64/

And will update the service information

$ createrepo --update /var/www/repo/centos/7/os/x86_64
$ createrepo --update /var/www/repo/centos/7/updates/x86_64
$ createrepo --update /var/www/repo/centos/7/extras/x86_64
$ createrepo --update /var/www/repo/centos/7/epel/x86_64

This set of 4 repositories on disk occupies approximately 60 Gb

Automatic update of repositories

To automatically update the repositories, create a script

$ sudo nano /home/repos_update.sh
#!/bin/bash
PATH=/etc:/bin:/sbin:/usr/bin:/usr/sbin:/usr/local/bin:/usr/local/sbin

# os
rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/os/x86_64/ /var/www/repo/centos/7/os/x86_64/
createrepo --update /var/www/repo/centos/7/os/x86_64

# update
rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/updates/x86_64/ /var/www/repo/centos/7/updates/x86_64/
createrepo --update /var/www/repo/centos/7/updates/x86_64

# extras
rsync -iavrt --delete --exclude="repo*" rsync://mirror.yandex.ru/centos/7/extras/x86_64/ /var/www/repo/centos/7/extras/x86_64/
createrepo --update /var/www/repo/centos/7/extras/x86_64

# epel
rsync -iavrt --delete --exclude="repo*" rsync://mirror.logol.ru/epel/7/x86_64/ /var/www/repo/centos/7/epel/x86_64/
createrepo --update /var/www/repo/centos/7/epel/x86_64

Making the script executable

$ sudo chmod +x /home/repos_update.sh

And add the update task to crontab

$ crontab -e
# ежедневно в час ночи
0 1 * * * /home/repos_update.sh

Add local repos to client PCs / servers

First you need to disable the existing configuration files of the repositories

$ find /etc/yum.repos.d -type f -exec sed -i "s/enabled=1/enabled=0/g" {} ;

Create a file with settings for local repositories

$ sudo nano /etc/yum.repos.d/local.repo
[local]
name=Local Yum Repo
baseurl=http://192.168.1.9/centos/$releasever/os/$basearch/
enabled=1
gpgcheck=0
priority=1

[local-update]
name=Local Yum Repo for update packages
baseurl=http://192.168.1.9/centos/$releasever/updates/$basearch/
enabled=1
gpgcheck=0
priority=1

[local-extras]
name=Local Yum Repo for extras packages
baseurl=http://192.168.1.9/centos/$releasever/extras/$basearch/
enabled=1
gpgcheck=0
priority=1

Create a file with settings for the local EPEL repository

$ sudo nano /etc/yum.repos.d/local-epel.repo
[local-epel]
name=Local Extra Packages for Enterprise Linux 7
baseurl=http://192.168.1.9/centos/$releasever/epel/$basearch/
enabled=1
gpgcheck=0

Now you can update from local repositories

$ sudo yum update
Sidebar