Chroot jail for SSH

From MEPIS Documentation Wiki

Jump to: navigation, search


This HOW-TO explains how to set up a chroot jail, an operation that changes the apparent disk root directory for a running program so that it cannot access or name files outside that directory. It should be used as a guide: while I have tried to cover everything by just retracing my steps after I got it all working, I haven't gone through this with a fine tooth comb.

This HOW-TO is based on Mepis 7 which is basically Debian Etch (For all the googlers :-P ) and presumes you have SSH server up and running already.


Install the PAM module which will be Chrooting our SSH Users

apt-get install libpam-chroot


Create the user (if you haven't already)

adduser testuser

Add the user you want to Chroot via SSH into /etc/security/chroot.conf like:

# username      chroot_dir
testuser	/home/testuser

add lines to /etc/pam.d/ssh like:

#Setup CHROOT Environment for users who SSH in
session    required debug

Setup the virtual file system for the chroot jail, Run the following commands as root:

mkdir -p /home/testuser/home/
cd /home/testuser
mkdir etc
mkdir bin
mkdir lib
mkdir -p usr/bin
mkdir dev
mknod dev/null c 1 3
mknod dev/zero c 1 5

Now setup a basic passwd file for the chroot jail:

touch etc/passwd
grep /etc/passwd -e "^root" > etc/passwd
touch etc/group
grep /etc/group -e "^root" -e "^users" > etc/group

Now we need to copy the applications we want to run into the chrooted jail, Create a txt file called, add the following to it:

APPS="/bin/bash /bin/ls /bin/mkdir /bin/mv /bin/pwd /bin/rm /usr/bin/id /usr/bin/ssh /bin/ping /usr/bin/dircolors"
for prog in $APPS;  do
        cp $prog ./$prog

        # obtain a list of related libraries
        ldd $prog > /dev/null
        if [ "$?" = 0 ] ; then
                LIBS=`ldd $prog | awk '{ print $3 }'`
                for l in $LIBS; do
                        mkdir -p ./`dirname $l` > /dev/null 2>&1
                        cp $l ./$l

Note: If you require other programs etc add the absolute path to them in the APPS line

One thing that happens when you try log in at present is you type in the password and receive:

/bin/bash: No such file or directory
Connection to SSH-SERVER closed.

The hasn't copied one of the libraries across, we are actually missing a copy of: So as root:

cp /lib/ /home/testuser/lib/

Now you should be able log into a chrooted home!

Guide derived from:

Original Article by Cam Mckenzie aka cAm34 of MepisLovers

Personal tools