Latest LQ Deal: Complete CCNA, CCNP & Red Hat Certification Training Bundle
Go Back > Forums > Linux Forums > Linux - Distributions > Linux From Scratch
User Name
Linux From Scratch This Forum is for the discussion of LFS.
LFS is a project that provides you with the steps necessary to build your own custom Linux system.


  Search this Thread
Old 10-02-2017, 07:11 PM   #1
Registered: Oct 2006
Location: Leiden, Netherlands
Distribution: LFS, Ubuntu Hardy
Posts: 231

Rep: Reputation: 42
Chroot with second desktop environment - Howto and notes

Background: This is a description of how to start up a working desktop environment within a chroot jail with the particular case that X and/or a desktop environment is already running on the main operating system. Some existing documentation for these procedures may be out of date and no longer be sufficient. Further, some of the available solutions have quirks that may hinder functionality.

The following commands work on (B)LFS and some minimal to moderate tweaking may be necessary on other distributions. This arose out of an attempt to run a desktop environment from BLFS 7.9 on a laptop running BLFS 7.5; the laptop does not support kvm so virtualization is out. Although there is no virtualization, I will refer to the main operating system as the "host" and the chroot'ed environment as the "guest." Further, this has been adapted for the case that the guest resides not on an actual partition or directory but rather a (read-only) dd image of the partition from which it was originally run.

There are three main possibilities for accomplishing this:
(1) running another X server
(2) Xnest (a means of running a "nested" X within a running X on the host)
(3) Xephyr (like Xnest, but runs on the guest)

First, mount the disk image and prepare the chroot environment like so (all as root):

mkdir /media/MountPoint
mount -t ext3 -o ro,loop DiskImage.img /media/MountPoint
mount -t proc /proc /media/MountPoint/proc
mount -o bind /dev /media/MountPoint/dev
mount -o bind /dev/pts /media/MountPoint/dev/pts
mount -o bind /run /media/MountPoint/run
mount -o bind /tmp /media/MountPoint/tmp
Now, for the user which you would like to log in as, duplicate the home folder into a readable/writable directory on the host and then bind-mount it into the guest

# mount -o bind /CopiedHomeFolder /media/MountPoint/home/UserName

You may also wish to bind-mount an empty directory to /media/MountPoint/var/cache if know your desktop environment writes to that directory.

If you want network access within the guest, make sure the host is connected to the network. Then bind-mount the /etc/resolv.conf file into the guest if the guest's /etc/resolv.conf does not have the proper information. (Doing this for a file - rather than a directory - is unusual but it works here since we are not changing any file contents.)

# mount -o bind /etc/resolv.conf /media/MountPoint/etc/resolv.conf

The rest of this tutorial does not pertain to this use-case of a read-only image.

The guest needs to have a copy of the host's ~/.Xauthority file, so copy it over

cp ~/.Xauthority /media/MountPoint/home/UserName

Now you are ready to chroot to the guest:

sudo chroot /media/MountPoint /bin/bash

Once inside:

su UserName
cd ~

Now adjust any environment variables as necessary.

In the guest terminal, type "xhost +" to allow X applications to display. To test this, try running a simple X program like xclock and see if the window shows up on your host desktop.

That is basic chroot + X. But desktop environments require a bit more handling. Option 1 above requires starting a second X server on the host. The way to do this is a bit obscure; just typing startx won't work. For this we need a separate tty outside the running X. Press CTRL+ALT+F2 to switch to tty2 if it is unused. Before continuing, temporarily rename any .xinitrc files - we are only testing here. In fact, DO NOT use the xinitrc files as specified in the BLFS book; these probably involve consolekit, dbus, and the like which are already running if you have a desktop environment up and running already. So, with no xinitrc files, let's continue.

A popular way to start a second X server is to run

startx -- :1
This gives the server the name ":1" (the existing one is probably ":0.0" and can be found by noting the value of the DISPLAY environment variable, i.e.

We can specify which tty the second server will be on (if the existing one is tty7, we might want the second one on tty8) by doing this:

startx -- :1 vt8
If this is successful, pressing CTRL+ALT+F8 should take us to the second X server, which, as it is in its unconfigured state should show the traditional three xterm windows. If we go back to tty7 and go to the chroot terminal, and type something like

DISPLAY=":1" xclock
The xclock program should show up on tty8. The problem here is that we would like to run a desktop on tty8 but startx gives us the usual twm manager and the like. We need a "naked" X server, so instead of using startx, we should do

/usr/bin/X :1 vt8
instead. Note: You need to remove the guest's .Xauthority file and replace it with the host's (again) if you copied this file before starting the second server!

Now we could go to the chroot terminal and launch our desktop environment, making sure to prepend the command like so:

DISPLAY=":1" mate-session
I got MATE desktop to start properly this way; Plasma 5 locked up my display completely. Not all desktop environments will work this way.

Option 2 (xnest) will work similarly. On the host, type

Xnest -ac :1
which will show a blank nested X window. Now you can launch your desktop environment as above with the proper DISPLAY, and hopefully it will launch properly. Xnest does not support a lot of X extensions so Plasma 5 will NOT work here. Also, Xnest does not repaint the window when it should, so use at your own risk.

Option 3 (Xephyr) is probably the easiest. It is part of the xorg-server package and as of version 1.18 does not build even if the prerequisites are present. So, rebuild the package adding --enable-kdrive --enable-xephyr to the configure line. Note: Rebuild xorg-xserver for the GUEST, not the host. Using any other Xephyr executable other than one built for the guest OS is likely to cause problems. Use the prefix and other arguments as specified when you built xorg-server. If you don't want to replace your existing executables, don't run "make install" - just find the compiled Xephyr and use it. It has no dependent shared objects that aren't already present.

In the guest (!), run

Xephyr -br -ac -noreset -screen 1280x720 :1 &
But don't make the size any bigger than you can handle. Don't forget the "&" - now start your desktop environment. For example

# Instead of using i.e. DISPLAY=":1" command
# we should probably use export DISPLAY=":1"
# for complicated scripts like that used by

export DISPLAY=":1"
I got Plasma 5 to run this way as well as Mate. Note that Xephyr does not (as of xorg-server 1.18) get the font DPI right, so you will need to edit the appropriate setting in your desktop environment (mine should be 96 dpi, yours may be different).

Because we bind-mounted /run, /run/dbus is present and accessible to the guest. Also, note that we mounted /proc using "mount -t proc /proc ..." rather than "mount -t proc none ..." which lists host processes in the guest; programs like mate-system-monitor or ksysguard in the guest will show (and kill, if told to) programs in the host, so use with care.

Overall Xephyr is probably the best solution, but if you want a true second X server, then this is dependent on how your second desktop environment reacts.


Gentoo Wiki for chrooting
How to start a second X session
ArchWiki Xephyr entry
Ubuntu basic chrooting
How to make a live CD/DVD from your hard disk installation
Sync problems with bind-mounted single file

Last edited by ordealbyfire83; 10-02-2017 at 07:40 PM. Reason: Clocked "submit" before finished
Old 10-03-2017, 06:05 AM   #2
Keith Hedger
Senior Member
Registered: Jun 2010
Location: Wiltshire, UK
Distribution: Linux From Scratch, Slackware64, Partedmagic
Posts: 2,623

Rep: Reputation: 659Reputation: 659Reputation: 659Reputation: 659Reputation: 659Reputation: 659
Nice how to, I don't often do this but on the odd occasion I want I can never remember how and have to trawl google for the solution, I'll bookmark this!
Old 10-03-2017, 04:41 PM   #3
Registered: Mar 2009
Distribution: Linux From Scratch, Bedrock, Gentoo
Posts: 287
Blog Entries: 41

Rep: Reputation: 12

This may be a way to achieve the docker-like effect of running something not in one's distro in one's distro. Good for older and 32-bit only programs.
Old 10-19-2017, 12:28 PM   #4
Wayne Sallee
Registered: Jun 2011
Location: Florida
Distribution: The one that I built. (lfs)
Posts: 256

Rep: Reputation: 15
This seems like a lot of work.

Are you running both desktops as the same user, or 2 different users?

If, the same user, what are the advantages of doing this? What are some examples of why you do it?

I can easily do this with 2 different users using lxdm.

I once ran gnome desktop and kde desktop on the same screen with the same user at the same time. It was interesting, funny, and not very practical, but it fun seeing if it could be done. :-)

Wayne Sallee

Last edited by Wayne Sallee; 10-19-2017 at 12:30 PM.
Old 10-22-2017, 01:58 PM   #5
Registered: Oct 2006
Location: Leiden, Netherlands
Distribution: LFS, Ubuntu Hardy
Posts: 231

Original Poster
Rep: Reputation: 42
This was all done as one user.(*) For BLFS users a main advantage would be running a newly compiled OS in order to work the kinks out (without rebooting, using another computer, or virtualization) before deploying it on your computer. Or as was said above (jr_bob_dobbs) "running something not in one's distro" - think Live CD with ALL the bells and whistles.

Right now I am running 32 bit BLFS-7.5 (with 64 bit kernel) with KDE 4 while having Xephyr with 64 bit BLFS-7.9 with KDE/Plasma 5. Using it for multilib is also nice too.

In reading other chroot tutorials it is often said that one purpose for using chroot is to be able to run older software. But it is equally possible to run newer software - as described, this chroot setup carries its own libc and thus only shares the same kernel. In addition starting init scripts within the guest also works - as an example, I was able to print from Libreoffice in BLFS 7.5 using cups from BLFS 7.9. (Just remember to stop these init scripts before you tear down the chroot.) This was useful in my case because I have built hplip with BLFS-7.9 but never did this for 7.5.

Now this isn't just pertinent to BLFS. You can download your favorite Live CD's from one distribution and try chrooting to them on some other distribution (i.e. mount the squashfs partition).

Or the reverse case: you could also do it for security, or suppose your hard drive got hosed but you have a dd backup of your partition burned to a DVD. You could boot a system rescue CD/USB and get all of your familiar environment up and running (though, in the specific case of system rescue CD chroot+X11 forwarding doesn't always work). This way you have all of your usual software running from a read-only image file while booted from a LiveCD.

Now for the (*): do note that we bind-mounted a separate directory for the guest's home folder. Sharing a home folder between the two might be problematic, i.e. if you are running the same DE on both the host and the guest (think of the case of having separate themes running on each simultaneously). Or who knows how compatible all those config directories would be for running KDE 4/5 at the same time.


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
howto about SB Live! & audigy Rx - any ideas, advices, notes? WiseDraco Slackware 1 03-19-2014 11:16 PM
chroot environment shiva1990 Linux - Newbie 2 01-25-2011 08:55 AM
squid under chroot environment. bzlaskar Linux - Security 3 04-29-2007 11:39 PM
Out of the chroot environment? R2RO Linux - Networking 0 05-14-2003 04:01 PM
Getting in and out and in again in the CHROOT environment? kRu_ZaDeR Linux From Scratch 8 01-13-2003 12:20 AM > Forums > Linux Forums > Linux - Distributions > Linux From Scratch

All times are GMT -5. The time now is 04:50 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration