Monday, 23 July 2012

Creating A Local Yum Repository on CentOS

Creating A Local Yum Repository on CentOS

Reducing the costs of I.T without reducing the functionally of your systems is one of the major obstacles to overcome. One of these costs is bandwidth.

One of the first bandwidth saving tips any organization should know is the importance of creating a local YUM repository on your LAN. Not only do you decrease the time it takes to download and install updates, you also decrease bandwidth usage. This saving will definitely please the suites of any organization.

This “How To” show’s you a simple yet effective way of setting up your local YUM server and client.

Preliminary Note:

This article assumes that you already have a CentOS machine (Physical/ Virtual) setup. 

I am using CentOS 5.8 64 BIT for this tutorial, however you can use these same steps for setting up a YUM server on most of the Linux platforms, including Ubuntu, RHEL, Fedora etc.

NOTE: If you want to create a CentOS VM using VMware Workstation, then follow the guide HERE.


Add an additional HDD (Min 15 GB – Max 30 GB) to your VM.

CentOS Repositories don’t take up much space as compared to Ubuntu. Ubuntu Repos can go upto 50 GB in size.

NOTE: To know how to add an external HDD to an existing VM and formatting it for use, click HERE

Once you have successfully created and mounted a HDD to the VM (20 GB HDD used in this tutorial), open up a terminal and type the following command:

# fdisk -l

You should see a similar output as shown below:

The next step is to create a new directory and mounting this newly created HDD to it.
We will create a new directory named “centos” at /var/www/html folder location as shown. 

# mkdir /var/www/html/centos

Once created, mount the device to the centos folder using the “mount” command.

# mount /dev/<your device name> /var/www/html/centos

If the drive is successfully mounted, you should see a folder “lost + found” automatically created in your mounted folder location (/var/ww/html/centos) as shown:

If you run a “df –h” command, you will see the partitions and their mount points as shown:

In this case, we see our 20 GB volume attached to the folder "centos" that we just created

Next, to get this volume attached to this VM each time the VM boots, we need to make an entry in a file. To do so, type in the following commands:

# cd /etc
# gedit fstab

NOTE: you can use whatever editor you are comfortable with to perform this operation, including VI Editor.

Once done, make an entry towards the end of the file in the following format:

<your device name> <mount point location> <type of device’s filesystem> <defaults> <0> <0> 

In our case, we get:

/dev/sdb1      /var/www/html/centos             ext3     defaults           0          0

Save the file once you have done your changes.

Now, we will create the file structure of our repository:

Based on your requirements, you can create directories for downloading both the 32 bit (i386) as well as 64 bit (x86_64) RPMs for your CentOS repo.

I am creating folders for 64 Bit RPMs and only for CentOS version 5.8

NOTE: You can create a similar structure for various CentOS versions and architectures.

# mkdir -p /var/www/html/centos/5.8/os/x86_64
# mkdir -p /var/www/html/centos/5.8/updates/x86_64
# mkdir -p /var/www/html/centos/5.8/centosplus/x86_64

Create a bash script that will rsync your local YUM Repo server with your local YUM mirror 

NOTE: For this tutorial, I will be using rsync from Internet Solutions – You can select your own rsync mirror from CentOS mirror list.

In /opt folder, create the following Bash Script:

# vi


rsync="rsync -avrt --bwlimit=256"

baselist="os updates centosplus"

for ver in $verlist
 for arch in $archlist
  for base in $baselist
    $rsync $remote $local/$ver/$base/$arch/

Provide execute permission to this script using “chmod” command:

# chmod 755

Run the script once done:

# ./

This will connect your CentOS VM to the rsync mirror and start downloading all the rpms from the mirror and placing them in the folder structure that we created at /var/www/html/centos directory

The download takes time based on your internet connection.

While your download is taking place, you can configure the Apache server of your local YUM Server
Type the following command to edit the httpd configuration file:

# cd /etc/httpd/conf
# vi httpd.conf

This opens up the httpd.conf file.

Edit two places in this file. 

1)      ServerAdmin root@<your local YUM server’s IP>
2)      ServerName <your local YUM server’s IP>:80

Save the file.

Start the httpd service as shown:

# service httpd start

You can test whether the httpd service has actually started by entering the IP address of the YUM Server in a browser.
You should see a Apache Test Page as shown below:

Once the download completes, all you have to do is configure the rest of the CentOS VMs in your LAN to take packages from your newly created YUM Server.

Configure YUM client servers as follows:

Rename all existing yum repositories from *.repo to *.old

NOTE: These repo files are located at /etc/yum.repos..d

# vi /etc/yum.repos.d/localCentOS-Base.repo
name=CentOS-$releasever - Base
baseurl=http://<REPO SERVER IP ADDRESS>/centos/$releasever/os/$basearch/

name=CentOS-$releasever - Updates
baseurl=http://<REPO SERVER IP ADDRESS>/centos/$releasever/updates/$basearch/

name=CentOS-$releasever - centosplus
baseurl=http://<REPO SERVER IP ADDRESS>/centos/$releasever/centosplus

name=CentOS-$releasever - Extras
mirrorlist=http://<REPO SERVER IP ADDRESS>/?release=$releasever&arch=$basearch&repo=extras


Test your setup by running a yum update on your client machine.

# yum update

That’s it… !! You now have a local YUM server at your disposal.. just remember to copy paste the above localCentOS-Base.repo file in all your CentOS machines on LAN..

1 comment :

Nuy Nurhasanah said...

thank you very useful information admin, and pardon me permission to share articles here may help Obat mioma uteri

Post a Comment