Category Archives: chroot

Accessing files from within a chroot jail

Recently, I had to create a Lenny chroot in my laptop running Squeeze. I also had to access, from the chroot environment, files which are outside the chroot directory. I asked around on IRC and received respose ranging from “Why do you need to access files outside your chroot” to “Try creating hard links”. After much asking and googling, the following method solved my problem:

Using sshfs
SSHFS  (Secure SHell FileSystem) is a file system for Linux (and other operating systems with a FUSE implementation, such as Mac OS X or FreeBSD) capable of operating on files on a remote computer using just a secure shell login on the remote  computer.  On the local computer where the SSHFS is mounted, the implementation makes use of the FUSE (Filesystem in Userspace) kernel module. The practical effect of this is that the end user can seamlessly interact with remote files being securely served over SSH  just as if they were local files on his/her computer. On the remote computer the SFTP subsystem of SSH is used. — From man sshfs

The trick is to use sshfs to mount the required directory on your local machine onto a directory inside the chroot directory. Say, if u have your chroot directory at /srv/lenny-chroot and you have to access /home/user/stuffs from within chroot, you can simply do 
sshfs localhost:/home/user/stuffs/ /srv/lenny-chroot/stuffs/
This will mount the required directory under /srv/lenny-chroot/stuffs . You must already have the /srv/lenny-chroot/stuffs directory created beforehand.

Now when you do a chroot /srv/lenny-chroot , your /home/user/stuffs directory and its contents will be accessible from /stuffs directory in the chroot environment. Once you are done with working inside the chroot and you exit it, you can unmount the stuffs directory by doing
fusermount -u /srv/lenny-chroot/stuffs