Debian Unstable inside a Chroot (LEGACY!)

Before you start!

WARNING: We do not recommend following this tutorial. This tutorial exists for historical purposes. What you probably want is to use a Container instead of creating a chroot.

The tutorial

  • Install the base system
sudo apt-get install dchroot debootstrap
sudo su
mkdir -p /var/sid-amd64-chroot
debootstrap --arch amd64 sid /var/sid-amd64-chroot
  • Create a script for mounting, entering, exiting and unmounting the necessary system directories (this is instead of using the fstab file which can be dangerous when, for example, you decide to delete the chroot with the system directories still mounted. If you do rm -rf /var/sid-amd64-chroot, you may not only delete the chroot directory but the system directories as well!!): This is the script: (copy it to /usr/local/bin/
CHROOT_NAME=`basename $0 .sh` 

if [ ! -e /var/run/$CHROOT_NAME ]
    sudo touch /var/run/$CHROOT_NAME
    sudo bash -c "echo 1 > /var/run/$CHROOT_NAME"
    sudo bash -c "echo `expr 1 + \`cat /var/run/$CHROOT_NAME\`` > /var/run/$CHROOT_NAME"

if [ `cat /var/run/$CHROOT_NAME` -eq 1 ]
    echo "First chroot invocation. Mounting host system directories"
    sudo mkdir -p $CHROOT_DIR/$CHROOT_NAME/hostfs
    #mount -o bind /home/ $CHROOT_DIR/$CHROOT_NAME/home
    sudo mount proc -t proc $CHROOT_DIR/$CHROOT_NAME/proc
    sudo mount -o bind /dev $CHROOT_DIR/$CHROOT_NAME/dev
    sudo mount sys -t sysfs $CHROOT_DIR/$CHROOT_NAME/sys
    sudo mount none -t devpts $CHROOT_DIR/$CHROOT_NAME/dev/pts
    sudo mount -o bind / $CHROOT_DIR/$CHROOT_NAME/hostfs
    sudo mount -o bind /run/shm $CHROOT_DIR/$CHROOT_NAME/run/shm

echo "Starting chroot."
#dchroot -c $CHROOT_NAME
sudo chroot $CHROOT_DIR/$CHROOT_NAME su - $USER
echo "Chroot closed."

if [ `cat /var/run/$CHROOT_NAME` -eq 1 ]
    echo "Closing last invocation. Unmounting host system directories"
    for i in dev/pts hostfs proc dev sys run/shm
  sudo umount $CHROOT_DIR/$CHROOT_NAME/$i
        sleep 0.5

sudo bash -c "echo `expr \`cat /var/run/$CHROOT_NAME\` - 1` > /var/run/$CHROOT_NAME"

if [ `cat /var/run/$CHROOT_NAME` -eq 0 ]
    sudo rm /var/run/$CHROOT_NAME
  • Please remember to name the above script with a name exactly as the directory where the chroot is plus “.sh” at the end. The script uses this name to find the chroot and to do other things.
  • Also, don't forget to give execute permissions to this script.
  • Now configure the /etc/schroot/schroot.conf file with: (please use between the [] the exact same name of the subdirectory containing the chroot, otherwise the script will not work)
description=Debian sid (unstable)

Now you are done. With:

You will get your chroot running. (this will not work because you don't have your same user in chroot yet, look down)

Some things to do initially

  • Start the chroot as root user:
  • Add a normal user
adduser username
  • Install some basic useful applications
apt-get install emacs joe mc locales sudo bash-completion less python
  • Add more sources to your sources.list file:
deb sid main contrib non-free
deb testing main contrib non-free
deb unstable main contrib non-free
deb experimental main contrib non-free
  • Install locales to stop getting anoying warnings.
apt-get install locales
dpkg-reconfigure locales
  • Increase bash history. Edit ~/.bashrc and change:
  • Follow configuration of xstow

Some notes

Using fstab instead of the script

  • If you don't want to use the script above. You will need to add the following to your /etc/fstab file:
# sid-amd64 chroot
#/home           /var/sid-amd64-chroot/home none   bind            0       0
none           /var/sid-amd64-chroot/proc proc   defaults        0       0
/dev            /var/sid-amd64-chroot/dev  none   bind            0       0
none            /var/sid-amd64-chroot/sys  sysfs   defaults            0       0
none            /var/sid-amd64-chroot/dev/pts  devpts   defaults            0       0
  • Mount this directories:
sudo mount -a

Using the same users and home directory of the host computer

  1. Remember that this may not be what you really need…
  2. If you erase something in chroot home it gets erased in the host computer also.
  3. If you have different versions of the same programs in the chroot and the host computer, the local home configurations may not work properly or could get corrupted.
  • Manually copy the user entries in files /etc/passwd and /etc/shadow from host to the chroot that have 1000 UID or higher. This must be done everytime a new user is added to the host and wants to use the chroot environment.
  • If you are using the script above, add the home directory mount command in the mount and umount section. In the mount section use -o bind mount option. In the umount section simply add the directy to the list of directories to umount.
  • If you are using fstab, just uncomment the home line and rerun mount -a .
  • Install and configure sudo to allow users to install packages

W: Failed to change to directory ....

This happens because the chroot doesn't have any users initially (unless you followed the instructions to use the users from the host computer), only root You can fix this problem by first logging as root in the chroot and then adding a user with the same name and id of your user in the host computer.

adduser --uid <user number in host computer> <username of host computer>

Then you can logging with no errors.

If you are using some mirrors

When you do apt-get update you may get an error like this:

E: Release file for is expired (invalid since 3d 17h 44min 46s). Updates for this repository will not be applied.

You can still update the the mirror if you use this command instead:

apt-get -o Acquire::Check-Valid-Until=false update

chroot and unionfs: Base installation and multiple setups

You can do a base installation and setup to chroot directory: /chroot/base and then unionfs mount this directory to other directories using cow (copy on write), to create specific application chroots. You may save disk space by not replicating the base system several times, and time by not having to install and configure multiple times.

unionfs-fuse -o cow  -o default_permissions -o use_ino -o suid -o noinitgroups -o allow_other -o nonempty /chroot/base=RO:/chroot/specific_chroot.union=RW /chroot/specific_chroot


Enter your comment. Wiki syntax is allowed:
Creating directory /var/lib/dokuwiki/data/tmp/captcha/2021-05-07 failed
If you can't read the letters on the image, download this .wav file to get them read to you.
  • tutorials/debian_inside_a_chroot.txt
  • Last modified: 2019/05/06 08:55
  • by dgarcia