How to Build a Chroot Environment in CentOS

The purpose of a chroot jail is to allow you to run a virtual file system within a larger one. You can use it for any number of reasons, including virtual shared hosting accounts. The user whose login account is associated with a chroot jail can use an entire virtual Linux distribution within the jail, but that user cannot navigate beyond the jail into the larger system.

Using YUM, the Red Hat and CentOS package manager, it is relatively easy to create a chroot jail. Just follow these steps.

1. Create a directory to house your chroot jail. For example:
mkdir -p /var/jail/chroot
2. To setup the chroot environment, you need to initialize the rpm database.
mkdir -p /var/jail/chroot/var/lib/rpm
rpm --rebuilddb --root=/var/jail/chroot

3. To save time, you can manually install the CentOS release package:
wget (or whichever version you are using)
rpm -i --root=/var/jail/chroot --nodeps centos-release-6-0.el6.centos.5.i686.rpm
4. Use YUM to install the rest of the CentOS distribution into your little virtual jail.
yum --installroot=/var/jail/chroot install -y rpm-build yum
5. Finally, when that process is finished (it will take some time), you can initiate chroot and try out the new system:
chroot /var/jail/chroot
If all goes well, you should have a relatively simple working chroot installation. Assuming you actually want to make the installation usable, you will need to add some other important file system necessities such as /proc and /dev. Follow the instructions in the above link for more help with this.