SSH and SFTP Chroot Jail

Submitted on Jan 09, 2014, 4:54 p.m.

Jail imageFor a little while now I've wanted to be able to chroot both SFTP and SHH accounts on one of my multi-user VPSs. SFTP on its own is not so difficult. OpenSSH 4.9p1 and above includes the ChrootDirectory directive. And an SFTP chroot is a little more forgiving in so far as it doesn't actually require any supporting system or userpsace services (a shell, ls, cp, etc.), which is why you often see ChrootDirectory accompanied with `ForceCommand internal-sftp` which will prevent SSH access altogether.

What I'd like to do is create a restricted environment for both SSH and SFTP. I spent a little while looking at a very interesting project from Olivier Sessink called [Jailkit]( Jailkit has most of what I was looking for but, it has quite a few moving parts, including the need to replace a users shell with a special Jailkit shim that hands over to Jailkit. This is okay but it means changes to passwd are required, and editing your `/etc/ssh/sshd_config` to use `Subsystem sftp /usr/lib/openssh/sftp-server` and not `Subsystem sftp internal-sftp` if you want to chroot and jail both SFTP and SHH logins.

It turns out that OpenSSH gets us most of the way there with the ChrootDirectory directive. And so here are the steps required to create a minimal chroot jail on Ubuntu 12.04 LTS. ## Let's get Jailed! ### Step 1: Create your chroot directories I've seen a few strategies for this including placing the chroot directory under /var/chroot. In this case all of the clients on this server have a public_html subdirectory structure under their home directories. To make it easy to see who's been jailed we'll put our chroot jail in /home. Thanks to [Allan Field]( for the headstart here...

1#Create our directories
2sudo mkdir -p /home/jail/{dev,etc,lib,lib64,usr,bin,home}
3sudo mkdir -p /home/jail/usr/bin
5#Set owner
6sudo chown root:root /home/jail
8#Needed for the OpenSSH ChrootDirectory directive to work
9sudo chmod go-w /home/jail

### Step 1: Choose your commands We'll offer a limited set of userspace applications. For these to work you need to copy the binary into its corresponding directory in the jail, as well as copy over any linked dependencies. Allan Field pointed me to a handy script that can be used for bringing binary dependencies over for a given executable (as opposed to manually - via ldd and copying the results.) The script can be found here... []( We're going to offer bash, cp, ls, clear, and mkdir to our jailed users (for starters).

1#First the binaries
2cd /home/jail/bin
3sudo cp /bin/bash .
4sudo cp /bin/ls .
5sudo cp /bin/cp .
6sudo cp /bin/mv .
7sudo cp /bin/mkdir .
9#Now our l2chroot script to bring over dependencies
10sudo /bin/bash
11sudo /bin/ls
12sudo /bin/cp
13sudo /bin/mv
14sudo /bin/mkdir

(This should really be wrapped up into a single bash script that takes both the binary and its dependencies). The clear command requires terminal definitions...

1# clear command
2cd /home/jail/usr/bin
3sudo cp /usr/bin/clear .
4sudo /usr/bin/clear
5#Add terminal info files - so that clear, and other terminal aware commands will work.
6cd /home/jail/lib
7sudo cp -r /lib/terminfo .

### Step 2: Create your user and jail group Create the jail group `sudo groupadd jail` You can either create a new user using `sudo adduser --home /home/jail/home/username username`, or copy (and then later remove) the home directory of an existing user into the `home/jail/home` directory. If you create a new user using `sudo adduser --home /home/jail/home/username username` - the home directory will be created in the jail, but the user's home directory in `/etc/passwd` will need to be edited to return it to `/home/username` - since the jail root will put home at the root again once the user is logged in. Now add the user to the jail group `sudo addgroup username jail` ### Step 3: Update sshd_config We're going to edit the sshd_config file, removing the `ForceCommand internal-sftp` directive - since we don't want to limit our users to SFTP (you could maintain a second group and configuration for this).

1Match Group jail
2 ChrootDirectory /home/jail
3 X11Forwarding no
4 AllowTcpForwarding no
5 AuthorizedKeysFile /home/jail/home/%u/.ssh/authorized_keys

We've chrooted to /home/jail - and both SFTP and SSH logins will default to the user's home directory below the jail. Restart the sshd daemon, and you're ready to go `sudo /etc/init.d/ssh restart` or `service ssh restart` Try logging in via SSH or SFTP, and your jailed user will be dropped into their home directory under /home/jail/home, with a limited set of userspace applications, and no access to the parent environment. ### Step 4: Bonus marks - give the user MySQL access I'd like users to be able to upload and configure a site, including be able to perform mysql dumps and restores. Here's how to give them a MySQL prompt.

1#Binaries for MySQL Client
2sudo mkdir /home/jail/usr/local/mysql/bin
3cd /home/jail/usr/local/mysql/bin
4sudo cp /usr/local/mysql/bin/mysql .
5sudo /usr/local/mysql/bin/mysql
6cd /home/jail/lib/x86_64-linux-gnu
7sudo cp /lib/x86_64-linux-gnu/ .

Note the 'undiscovered' dependency on ## Wrapping Up! This is a nice and simple solution that works thanks to OpenSSH's built in ChrootDirectory directive. It doesn't require any modification to the `passwd` file, and could fairly easily be wrapped up into a consolidated shell scrip for creating, updating and adding applications to the jail. And that's it! And remember - Linux is fun ;-)