Chroot Environment

A chroot environment is commonly used for development-related work and is basically an install of build-related software. It is always a good idea to do development work in a chroot environment, as it often requires the installation of development packages (whose main purpose is for building packages). An example is when a certain application requires the headers and development version of a library to build (e.g. libabc-dev). A normal user would not require the development version of libabc. Thus it is better to install such development packages in a chroot, leaving the normal operating environment clean and uncluttered. First, install the required packages:

sudo apt-get install dchroot debootstrap
[Important]

Make sure to install at least the version of debootstrap that is from the Ubuntu release for which you are trying to create the chroot. You may have to download it from packages.ubuntu.com and manually install it with dpkg -i.

The next steps are to create, configure, and enter the chroot environment.

sudo mkdir /var/chroot
echo "mychroot /var/chroot" | sudo tee -a /etc/dchroot.conf
sudo debootstrap --variant=buildd dapper /var/chroot/ http://archive.ubuntu.com/ubuntu/
[Note]

Creating a chroot environment will take some time as debootstrap downloads and configures a minimal Ubuntu installation.

sudo cp /etc/resolv.conf /var/chroot/etc/resolv.conf
sudo cp /etc/apt/sources.list /var/chroot/etc/apt/
sudo chroot /var/chroot/

In order to be able to use apt in the chroot, add Ubuntu sources to the chroot's apt sources. For the moment, ignore any warnings about package authentication:

echo "deb     http://archive.ubuntu.com/ubuntu dapper main restricted \
	universe multiverse" > /etc/apt/sources.list
echo "deb-src http://archive.ubuntu.com/ubuntu dapper main restricted \
	universe multiverse" >> /etc/apt/sources.list
apt-get update
apt-get install build-essential dh-make automake pbuilder gnupg lintian \
	wget debconf devscripts gnupg sudo
apt-get update
exit

Run the following command to configure locales:

sudo chroot /var/chroot/
apt-get install locales dialog
dpkg-reconfigure locales
exit

Next, fix the user and root passwords for the chroot environment. The last line below is to avoid sudo warnings when resolving in the chroot environment:

sudo cp /etc/passwd /var/chroot/etc/
sudo sed 's/\([^:]*\):[^:]*:/\1:*:/' /etc/shadow | sudo tee /var/chroot/etc/shadow
sudo cp /etc/group /var/chroot/etc/
sudo cp /etc/hosts /var/chroot/etc/

To enable sudo, set up your root password and the first sudo user in the admin group (for the chroot environment). In the following commands, substitute "<user>" with the username that will be used in the chroot environment:

sudo cp /etc/sudoers /var/chroot/etc/
sudo chroot /var/chroot/
dpkg-reconfigure passwd
passwd <user>
exit

The system fstab needs to be modified so that the chroot environment will have access to the system home directories, temp directory, etc. Note that the actual system home directory is used in the chroot environment.

sudo editor /etc/fstab

Add these lines:

/home           /var/chroot/home        none    bind            0       0
/tmp            /var/chroot/tmp         none    bind            0       0 
proc-chroot     /var/chroot/proc        proc    defaults        0       0 
devpts-chroot   /var/chroot/dev/pts     devpts  defaults        0       0 

Mount the new fstab entries

sudo mount -a

The default bash profile includes chroot information in the prompt. To make this visible:

sudo chroot /var/chroot/
echo mychroot > /etc/debian_chroot
exit

Now use your chroot (you may omit the -c mychroot if there's only one or you just want the first one in /etc/dchroot.conf). The -d parameter means that your environment will be preserved. This parameter is generally useful if you want chrooted applications to seamlessly use your X server, your session manager, etc.

dchroot -c mychroot -d