Setup a chroot environment on Ubuntu with debootstrap

By | July 8, 2019

Chroot

Chroot provides a way to setup an isolated environment inside an OS, where specific applications can run without interacting with the rest of the system. It is also called a jail and is often used to create a test environment for applications which may not be directly installed on the system due to various concerns like stability or security.

Running 32 bit applications in a chroot environment 64 bit ubuntu

Because chroot isolates application into a separate environment, it is also used to run 32 bit applications inside a 64bit OS environment. Now a days most OSes and applications are 64bit on both desktop and servers.

However there are scenarios where applications cannot be migrated to 64 bit, due to various restrictions. This happens in organisations where large legacy applications have been running for long. For such applications to keep running it is necessary to either have a 32bit OS installed or create a 32bit environment inside a 64bit OS installation.

The chroot environment

A chroot environment is a way to create a separated so-called "jail" in your system, running a different operating system than the host. In other words, it's an operating system running within another operating system. Technically, the user changes temporarily the root directory (usually /) to another root directory (in our example, /srv/chroot).

In all Unix-based operating systems, the root directory is the top of the filesystem tree, and the applications can't access any file or directory higher up , therefore, having a new, different, root and changing to it, the applications running there, are isolated from the rest of the host system.

This approach has many advantages:

-  Test applications without the risk of compromising the entire host system.
-  From the security point of view, whatever happens in the chroot environment won't affect the host system (not even under root user)
-  A different operating system running in the same hardware

In this case, we will take advantage of this feature to run 32-bit applications natively in a 64 bit host.

The necessary packages to setup the chroot environment are debootstrap and schroot, which are available in the ubuntu repository. The schroot command is used to setup the chroot environment.

Debootstrap allows you to install a new fresh copy of any Debian (or debian-based) system in a directory with all the basic commands and binaries needed to run a basic instance of the operating system.

You can chroot directly as root by simply running chroot command, but normal users  are  not  able  to  use this command. The schroot allows access to chroots for normal users using the same mechanism,  but  with  permissions  checking  and allowing additional automated setup of the chroot environment, such as mounting additional filesystems and other configuration tasks.

This automated setup is done through the action of setup scripts which may be customized  and  extended  to  perform  any actions required, explained below.

These are the steps to implement this functionality in Ubuntu 12.04:

1. Install the packages:

The first step is to install the packages with the apt-get:

$ sudo apt-get install debootstrap 
$ sudo apt-get install schroot

2. Create a schroot configuration:

We have to configure schroot to suit our needs in the configuration file.

Choose a short name for the chroot, we used precise_i386 :

$ sudo gedit /etc/schroot/chroot.d/precise_i386.conf

Then proceed with the configuration, as in this example:

[precise_i386]
description=Ubuntu 12.04 Precise for i386
location=/srv/chroot/precise_i386
personality=linux32
root-users=bob
type=directory
users=alice,bob,charlie

Modify the values of the configuration parameters in the above example to fit your system:

location - This should be a directory that is outside of the /home tree. 
The latest schroot documentation recommends /srv/chroot.

personality - Enable this line if the host system is 64-bit running on an amd64/x64 computer 
and the chroot is 32-bit for i386. Otherwise, leave it disabled, by adding "#" as first character.

root-users - These are users on the host system that can invoke the schroot program and 
get direct access to the chroot system as the root user.

users - These are users on the host system that can invoke the schroot program and 
get access to the chroot system. Your username on the host system should be here.

3. Install 32bit ubuntu with debootstrap

Debootstrap downloads and installs a minimal operating system inside your chroot environment.

You can install any debian-based distro of your preference, as long as you have a repository available. I love this utility, it saves you a lot of time and work.

In our example we placed the chroot environment under the directory /srv/chroot/precise_i386 and this is the root directory of the chroot environment.

$ sudo mkdir -p /srv/chroot/precise_i386
$ sudo debootstrap --variant=buildd --arch=i386 precise /srv/chroot/precise_i386 http://archive.ubuntu.com/ubuntu/

It takes some time (depending on your bandwidth) to download, install and configure the complete system. It takes about 500M for a minimal installation.

4. Test the chroot environment

Now, we should have our 32 bit Ubuntu installed.

First list the available chroot environments using the schroot command.

$ schroot -l
chroot:precise_i386

you should expect precise_i386 as a result.

Now let's get into it, simply type as your regular user

$ schroot -c precise_i386 -u root

Now, you are into the chrooted environment as root!

Check that the prompt message is slightly different than usual, and you have new values for the following commands

lsb_release -a
uname -a

5. Additional configuration

Now, that we have a functional 32 bit environment, let's finish to install the minimal requirements and all the necessary libraries and applications.

$ apt-get install ubuntu-minimal

This will download and install the needed packages of a minimal ubuntu installation.

That's all.

You can begin to install the packages, libraries and dependencies that your application needs, as usual with apt-get or dpkg command. It will retrieve the i386 version, as long as you do it within the chroot environment.

You can update and upgrade it, as you do with the host system; edit your /etc/apt/sources.list and modify according to your needs.

The home directories of the users within the chroot, are shared with the host.
This is very useful in order to have access to applications, documents and files that you should need.

To finish this tutorial, in order to run a graphic application from the chroot, you have to export the DISPLAY environment variable.

$ DISPLAY=:0.0 ./yourapp

Please feel free to send any question or comment regarding this article.

Hope you liked it and found it of help!!

About Silver Moon

A Tech Enthusiast, Blogger, Linux Fan and a Software Developer. Writes about Computer hardware, Linux and Open Source software and coding in Python, Php and Javascript. He can be reached at [email protected].

9 Comments

Setup a chroot environment on Ubuntu with debootstrap
  1. Root of all Evil

    Unfortunately, the AWESOME tool that Chroot is (or was) seems to have been destroyed by the abomination that is SystemD.
    Here I am trying to create a remastered ISO respin for myself using a Chroot tool, and I can’t run graphical applications inside of it.
    Things like Thunar and Terminal will run inside of the chroot environment (throwing error messages) but things like xfce4-panel and xfce4-settings-manager will not (“not able to connect to xfconf”, “there is already a running instance”, etc etc)
    Because of systemd, chroot thinks that it already have these processes running because it is mistaking processed inside of chroot to processes external to it – a thing that should never happen – and in fact, they never happened before.
    Xfce is particularly bad with SystemD, don’t know about other desktops, but Xfce is having problems even saving user settings across reboots. That is the state of Linux in 2020. OMG, I don’t even know where to start from…
    This is guide is from 2013, pretty outdated by now (and dare I say made obsolete to all distros that use systemd)
    Is there any new updated guide to take systemd in consideration?
    Have you messed with Chroot during these last 7 years? (on a system with systemd)
    Any useful info that you might heard around the block?
    What a pain in the ass all of this is. Completely unnecessary and avoidable hassle.

  2. Shawn Donnelly

    I am new to linux…I was just wondering if it was mandatory to install chroot environment to properly install virtualbox on a linux host machine?

  3. Jeff K

    I am trying to log into my first chroot with this command:

    schroot -c precise_i386 -u root

    I keep getting authentication errors. I am using my user password when prompted…still doesnt work. Any advice?

    Jeff

Leave a Reply

Your email address will not be published. Required fields are marked *