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 rollout 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 recipe 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 complete this recipe, 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 recipe 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 recipe in Chapter 15, Installing CentOS (but download the latest CentOS-7-x86_64-Everything-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 first recipe in Chapter 12, Providing Web Services in order to learn how to set it up.
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:
yum install createrepo
/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
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/
restorecon -v -R /var/www/html
createrepo --database /var/www/html/repository/centos/7.1
vi /etc/yum.repos.d/myCentosMirror.repo
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
yum repolist | grep myCentosMirror
yum --disablerepo="*" --enablerepo="myCentosMirror" list available
In this recipe, 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, afterwards 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.
Afterwards, 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 (as shown in another recipe in this chapter).
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 recipe in Chapter 15, 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