24/7/365 Support

Creating a YUM repository in CentOS

If you maintain multiple CentOS servers in your local network and want to save Internet bandwidth or speed up the downloading of the same remote repository packages over and over again, or are within a very restrictive network environment where access to any remote CentOS repository is blocked for your clients, you might want to consider running your own YUM repository. Having your own repository is also an excellent solution if you want to roll out a few custom or unofficial RPM packages (for example in-house configuration files or programs) to your local crowd or if you just want to create an official CentOS 7 repository mirror site. Here in this process, we will show you how to set up your own first YUM CentOS 7 repository and how to serve it to your local network.

To Start With: What Do You Need?

To complete this process, you will require a working installation of the CentOS 7 operating system with root privileges, a console-based text editor of your choice, and a connection to the Internet to facilitate the download of additional packages. For this process to work, you will also need to place the CentOS 7 Everything DVD iso file image in your server’s root home directory, if you haven’t downloaded it yet, refer to a detailed description in the first process, Installing CentOS (but download the latest CentOS-7-x86_64Everything-XXXX.iso file instead of the minimal iso file). Also, we need a running Apache web server to share our YUM repository to our local network; please read the process, Providing Web Services in order to learn how to set it up.

The Process

To create our own YUM repository, we need the createrepo program, which is not installed on CentOS 7 by default. Let’s begin our journey by installing it. In this example, we will use the IP address, 192.168.1.7, for our YUM repository server:

  1. Log in as root on your server and install the following package:
    yum install createrepo
  2. Next, for every repository you want to share, create a subfolder beneath the Apache web root folder under /var/www/html/repository/, which will be publicly available when Apache is running; for example, to share the complete CentOS 7 Everything repository packages, you could use:
    mkdir -p /var/www/html/repository/centos/7.1
  3. Now, put all your RPM package files of choice into the repository folders created here. In our example, we will put all RPM packages from the Everything iso image file into our new local repository location after we have mounted the content of the iso file to the filesystem:
    mount ~/CentOS-7-x86_64-Everything-1503-01.iso /mnt/
    cp -r /mnt/Packages/* /var/www/html/repository/centos/7.1/
  4. Afterward, we need to update the SELinux security contexts for all the new files copied into the Apache web root directory:
    restorecon -v -R /var/www/html
  5. Now, for every repository we want to set up, run the following command:
    createrepo --database /var/www/html/repository/centos/7.1
  6. Congratulations, you now have successfully created your first YUM repository, which can be accessed from any computer in the same network through the running Apache web server. In order to test it, log in as root to any other CentOS 7-based system that can ping our repository server and add our new repository to its YUM repository configuration directory:
    vi /etc/yum.repos.d/myCentosMirror.repo
  7. Add the following content to this empty file (change the baseurl appropriately to fit your own needs):
    [myCentosMirror]
    name=my CentOS 7.1 mirror
    baseurl=http://192.168.1.7/repository/centos/7.1
    gpgcheck=1
    gpgkey=http://mirror.centos.org/centos/RPM-GPG-KEY-CentOS-7
  8. Save and close the file, then test if your new repository is available (it should appear on the list) on your client:
    yum repolist | grep myCentosMirror
  9. Now, to test our new YUM repository, we can try the following command:
    yum --disablerepo="*" --enablerepo="myCentosMirror" list available

How Does It Work?

In this process, we have shown you how easy it is to install and set up a local YUM repository. However, we have only shown you how to create a mirror site of all the CentOS 7 Everything iso RPM packages, but you can repeat this process for creating YUM repositories of every kind of package that you want to share with your network.

So, what did we learn from this experience?

Setting up your own YUM repository was simply a matter of installing the createrepo package and copying all the RPM packages that you want to share into a subfolder of your choice beneath your Apache’s document root directory (In our example, we had to mount the CentOS 7 Everything iso file to the filesystem, in order to access its included RPM package files that we want to share). As the Apache’s document root directory is under the control of SELinux, afterward we needed to set the security context for all the new RPM files in this directory to the httpd_sys_content_t type label; otherwise, no access through the web server would be possible. Finally, we needed to run the createrepo command on our new repository folder, which will create our new repository’s metadata that is needed for any YUM client that wants to connect to the repository later to make queries to it.

Afterward, to test our new repository, we created a new repository definition file on another CentOS 7 system that wants to use this new service and that must be in the same network as our YUM repository server. In this custom .repo configuration file, we put the correct URL path to the repository, enabled gpg checks, and took the standard CentOS 7 gpgkey so that our YUM client can proof the validity of the RPM packages official repository packages. Finally, we used the yum command with the --disablerepo="*" and --enablerepo="myCentosMirror" parameters, which will make sure to only use our new custom repository as a source. You can use these two parameters in combination with any other yum command such as install, search, info, list, and so on. This was just for testing; if you want to combine your new repository with the existing ones, please use YUM priorities for it. 

There's more…

Now, before we announce our new centralized YUM repository to our network, we should first make an update of all the RPM packages that have changed since the release of the CentOS Everything iso. In order to do this, visit http://www.centos.org and choose a rsync:// mirror link that is geographically near your current location. For example, if you are located in Germany one option could be rsync://ftp.hosteurope.de/centos/ (for more detailed instructions on navigating the CentOS website, read the first process, Installing CentOS). Also, before we can use the rsync protocol, we need to install the rsync package (yum install rsync), if not done already. Now, open the following empty script file vi ~/update-myCentosMirror-repo.sh file and put in the following content (replacing the rsync:// location accordingly, if needed):

rsync -avz rsync://ftp.hosteurope.de/centos/7/os/x86_64/Packages/
/var/www/html/repository/centos/7.1
restorecon -v -R /var/www/html

Now, make the file executable using chmod +x ~/update-myCentosMirror-repo.sh, and run it with ~/update-myCentosMirror-repo.sh. This should update your repository to the latest version. Finally, to automate this process, let’s create a cron job that will update our repository packages with the other mirror site every night at 2:30 am (open crontab -e):

30 2 * * * /root/update-myCentosMirror-repo.sh

 

Help Category:

What Our Clients Say