1. 程式人生 > >如何在CentOS上搭建chroot環境

如何在CentOS上搭建chroot環境

chroot環境可以用來在一個完整的大檔案系統上執行另外一個虛擬的檔案系統。可使用它來實現許多功能,比如生成虛擬共享主機賬號。每個使用者的賬號與一個chroot環境一一對應,使用者可使用此chroot內安裝的Linux發行版的整個檔案系統,但是不能觸及外層的大檔案系統。(譯者使用chroot除錯程式^_^)。

使用Red Hat和CentOS系統的安裝包管理器yum,非常容易建立一個chroot環境。步驟如下:

1. 新建一個chroot的目錄,例如:

mkdir -p /var/jail/chroot

2. 要搭建chroot環境,首先需要初始化rpm資料庫。

mkdir -p /var/jail/chroot/var/lib/rpm
rpm --rebuilddb --root=/var/jail/chroot

3.為節省時間,可以手動下載CentOS的發行包,使用rpm命令安裝:

wget http://mirror.centos.org/centos/6/os/i386/Packages/centos-release-6-0.el6.centos.5.i686.rpm (或者你想使用的任何版本)
rpm -i --root=/var/jail/chroot --nodeps centos-release-6-0.el6.centos.5.i686.rpm

4. 使用YUM工具安裝CentOS發行版的其餘包到虛擬的chroot環境。

yum --installroot=/var/jail/chroot install -y rpm-build yum

5. 最後,整個過程結束以後(這將花費一段時間),你就可以初始化chroot,嘗試新系統了:

chroot /var/jail/chroot

如果一切正常,你已經有了一個相對簡單的可執行的chroot環境。但是,如果你想實際使用此環境,還需要其它一些重要的檔案系統必要元件,比如/proc和/dev.關於此可按照以上鍊接中的指示操作。

譯者增加:proc檔案載入指令碼,判斷proc檔案是否已經載入,未載入呼叫mount:

mount -l | grep "/var/jail/chroot/proc" > /dev/null
if [ $? != 0 ]
then
   sudo mount -t proc chroot_proc /var/jail/chroot/proc/
fi

PS:發現原文網址已不可訪問,貼出來供參考:


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 http://mirror.centos.org/centos/6/os/i386/Packages/centos-release-6-0.el6.centos.5.i686.rpm (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.