I am currently in the process of switching some servers from Debian 4.0 to CentOS 5.3. Almost all Linux distributions provide convenient tools to keep the software selection safe and current by installing updates. As I run multiple servers, these update processes are unnecessarily bandwith-hungry, because by default every host connects to the distributions update servers on its own.
There are different strategies to get around this. On Debian I use apt-proxy for this task. It is neatly integrated into the apt package management tool and works like charm most of the time.
For CentOS I wasn´t able to find such a specialized proxy. So I decided to try to set up a private mirror. It wasn´t that hard at all and this post outlines all necessary steps. I used this howto as a starting point, but found it faulty and outdated. So here is how I did it.
BE WARNED: I am not responsible for any data loss, malfunction or any other inconvinience you encounter by following this howto. It worked for me, it might not work for you as well. Be sure to have a backup of all your data.
I assume you set up the mirror on a CentOS host itself. Other host operating systems work as well, but you will have to adjust some paths. Per distribution release version and architecture you will need about 10GB of disk space, but be sure to keep it extensible (by using LVM for example). All these steps apply to CentOS 5.x, other versions of CentOS are slightly different.
Start by installing the required software (if not done yet). You will need rsync and a http server (I assume you use Apache web server here). To install everything, run
su -c 'yum install httpd rsync'
I assume here that you don´t want to use virtual hosts, so following steps apply to the standard installation of Apache. Be also sure that your mirror stays private if you want it to. See the Apache HTTP server documentation for more on securing the web server and virtual hosting.
First create the directory, that will hold all the RPM files:
su -c 'mkdir -p /var/www/html/centos/5/{os,updates}/x86_64'
Please note that you have to keep the 5 (printed bold) even you use a point release like 5.3. Adjust x86_64 to the used architecture.
Now mount the CentOS installation DVD and copy the relevant files (If you use installation media on CD-ROM, repeat these steps per disk):
su -c 'mount /dev/cdrom /mnt' su -c 'cp -rv /mnt/CentOS /mnt/repodata /var/www/html/centos/5/os/x86_64/' su -c 'umount /mnt'
At this point you have set up a private mirror for the distribution´s installation media. To verify it´s working, open a web browser and open http://ip.of.you.server/centos/5/os/x86_64 (Replace ip.of.you.server by the real ip). You should get either a “Forbidden” message or a file list containing two directories, repodata and CentOS. There must be no “File not found message”.
Now on the updates. Unlike the installation media they change often. So we need a way to keep our mirror in sync with the distributions update servers. rsync is the tool for the job. It scans a directory tree for changes and applies these changes to a local directory. It transfers only the delta which makes it very bandwith saving.
We will set up rsync to run once per day. To do so open up the file /etc/cron.daily/yum-repos-rsync-update as root and put the following content into it:
#!/bin/sh rsync -avrt rsync://your.rsync.mirror.server/centos/5.3/updates/x86_64 \ --exclude=debug/ /var/www/html/centos/5/updates/ > /dev/null
Make the file executable:
su -c 'chmod 755 /etc/cron.daily/yum-repos-rsync-update'
Now run the update once manually once:
/etc/cron.daily/yum-repos-rsync-update
It will take some time and afterwards there should be some files in /var/www/html/centos/5/os/x86_64.
That was all on the server part. On all clients open the file /etc/yum.repos.d/CentOS-Base.repo and make it look like (Comment out the bold lines starting with “mirrorlist” and add the bold line starting with baseurl. Adjust ip.of.your.server to the right value):
... [base] name=CentOS-$releasever - Base baseurl=http://ip.of.your.server/centos/$releasever/os/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=os #baseurl=http://mirror.centos.org/centos/$releasever/os/$basearch/
gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 #released updates [updates] name=CentOS-$releasever - Updates baseurl=http://ip.of.your.server/centos/$releasever/updates/$basearch/ #mirrorlist=http://mirrorlist.centos.org/?release=$releasever&arch=$basearch&repo=updates #baseurl=http://mirror.centos.org/centos/$releasever/updates/$basearch/ gpgcheck=1 gpgkey=file:///etc/pki/rpm-gpg/RPM-GPG-KEY-CentOS-5 ...
Finally run
su -c 'yum clean all' su -c 'yum update'
If all runs well, you have properly set up a private CentOS mirror. Congratulations!
Thank you for a very concise tutorial, I setup a central repo for all my CentOS installations (both i386 and x86_64) w/o trouble in little time.
I have run into a problem, maybe it’s something to do with firewall or apache but maybe you can help.
The part with the ‘yum-repos-rsync-update’ does not work correctly. I’m assuming I am to use the ip of my server, or should it be an outside repository mirror? Either way I get this error:
“rsync: failed to connect to 192.168.1.4: Connection refused (111)
rsync error: error in socket IO (code 10) at clientserver.c(107) [receiver=2.6.8]“
@Derek Yes, the reason is that it must be the outside mirror server. So instead of 192.168.1.4 you would have to use a server listed on http://www.centos.org/modules/tinycontent/index.php?id=30
I have created the local yum repository using centos5 in this way-
1. mkdir -pv /var/www/html/centos/5/local/i386/RPMS
2. Copy all rpm files into the /var/www/html/centos/5/local/i386/RPMS directory.
3.createrepo /var/www/html/centos/5/local/i386
4. create a local.repo file and save it into the /etc/yum.repos.d directory. The contains of local.repo is-
[local]
name=CentOS-$releasever – local packages for $basearch
baseurl=http://127.0.0.1/centos/$releasever/local/$basearch
enabled=1
gpgcheck=0
protect=1
Thats all. For this we need createrepo tools and httpd server.Thanks.
Thanks for this howto…it’s *much* better than the one on howtoforge.com. Thanks for doing this!
[...] yum install on new server it was failing over as it could not find repo data so I have moved it. http://nerdicism.com/2009/04/how-to-…centos-mirror/ I seem to get different error currently Error Downloading Packages: [...]
Thanks for this tutorial!
I got my repo server to work, but I’m running into a problem with the rsync update. Every time rsync runs (once a day) and synchronizes with a public mirror, it seems to overwrite my repo XML database stored in /repodata. I have to run createrepo after each update, or else the updates won’t show up when running ‘yum clean all’ and ‘yum update’ on a client. Rsync downloads both the /RPMS and /repodata directories from the public mirror. Any idea?
Thanks a lot,I follow your tutorial and make one.
Clearly and careful documentation.
Danke!