DebootstrapChroot

Revision 3 as of 2005-08-11 11:28:02

Clear message

DebootstrapChroot

You can work anywhere - this howto will assume you're using /var/chroot

If you are running warty and want a hoary chroot you will need to install the debootstrap package from hoary http://higgs.djpig.de/ubuntu/www/hoary/admin/debootstrap

If you are running hoary and want a breezy chroot you will need this debootstrap package or recompile debootstrap from breezy yourself: http://archive.ubuntu.com/ubuntu/pool/main/d/debootstrap/debootstrap_0.3.1.4ubuntu4_all.deb

 sudo apt-get install dchroot debootstrap
 sudo mkdir /var/chroot
 sudo editor /etc/dchroot.conf

Append this line

 mychroot /var/chroot

If you want a 32-bit chroot on amd64 add --arch i386 to this command line. If you use the chroot to build packages add --variant=buildd

 sudo debootstrap [--variant=buildd] [--arch i386] hoary /var/chroot/ http://archive.ubuntu.com/ubuntu/
 sudo cp /etc/resolv.conf /var/chroot/etc/resolv.conf
 sudo cp /etc/apt/sources.list /var/chroot/etc/apt/
 sudo chroot /var/chroot/
 apt-get update
 apt-get install wget debconf devscripts gnupg  #For package-building
 apt-get install locales dialog  #If you don't talk en_US 
 dpkg-reconfigure locales
 exit

Fix the user and root password

 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/ # avoid sudo warnings when it will resolvin chroot

To enable sudo and setup your passwords for root and the first sudo user in the admin group:

 sudo cp /etc/sudoers /var/chroot/etc/
 sudo chroot /var/chroot/
 dpkg-reconfigure passwd
 passwd <your first ubuntu user in the admin group>
 exit

Finish things up:

 sudo editor /etc/fstab

Add these lines: (/media/cdrom is optional, of course)

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

Mount them:

sudo mount /var/chroot/home
sudo mount /var/chroot/tmp
sudo mount /var/chroot/media/cdrom
sudo mount /var/chroot/proc
sudo mount /var/chroot/dev/pts

The default bash path includes chroot information. To make this visible:

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

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

 dchroot -c mychroot -d

From unknown Sun Apr 17 05:43:14 +0100 2005 From: Date: Sun, 17 Apr 2005 05:43:14 +0100 Subject: Using symlinks for passwd, groups, shadow, etc..? Message-ID: <20050417054314+0100@https://www.ubuntulinux.org>

Wouldn't it be possible to use symlinks for the files that get copeid into the chroot? Like /etc/hosts? Would it work with /etc/passwd and the like?

From MichaelShigorin Sun Apr 17 13:42:38 +0100 2005 From: Michael Shigorin Date: Sun, 17 Apr 2005 13:42:38 +0100 Subject: nope Message-ID: <20050417134238+0100@https://www.ubuntulinux.org>

...but you can mount --bind them one be one. Smile :)

From goofrider Thu May 12 19:26:45 +0100 2005 From: goofrider Date: Thu, 12 May 2005 19:26:45 +0100 Subject: chroot and symlinks Message-ID: <20050512192645+0100@https://www.ubuntulinux.org>

You can't symlinks from inside the chroot to somwhere outside of it, because once you chroot into it, the new chroot will becomes /, and all symlinks will be resolved relative to this new /. Use mount --bind instead (though hard links should work too). --GoofRider 2005-05-12

From Sam Fri May 13 09:22:44 +0100 2005 From: Sam Date: Fri, 13 May 2005 09:22:44 +0100 Subject: mount -a Message-ID: <20050513092244+0100@www.ubuntulinux.org>

You can use $ sudo mount -a for mounting all the entries in fstab instead of mounting them one by one.

From LukaszStelmach Sun May 15 00:06:59 +0100 2005 From: Lukasz Stelmach Date: Sun, 15 May 2005 00:06:59 +0100 Subject: Using symlinks Message-ID: <20050515000659+0100@www.ubuntulinux.org>

You can make hardlink to filas (but only when your chroot dir is on te same partition):

ln /etc/passwd /var/chroot/etc/