No description
Find a file
2026-10-28 11:41:48 +01:00
bin Consolidate fresh jail setup and document manual installation 2026-10-28 11:26:24 +01:00
docs Clarify LXC fstab device mount limitations 2026-10-28 11:41:48 +01:00
tests Initial SSH chroot toolkit 2026-10-28 10:17:02 +01:00
README.md Consolidate fresh jail setup and document manual installation 2026-10-28 11:26:24 +01:00

SSH chroot jail

A small Debian-oriented toolkit for creating a minimal OpenSSH chroot with rsync, SCP, SFTP and a shell. Each jail lives at /srv/ssh-jails/NAME; each account has its own writable /data/USER. This is a starting point for administrators, not a general container or a hardened multi-tenant sandbox.

Status: initial implementation; shell syntax and offline checks only. Test in a disposable VM or LXC before production. The scripts have not been exercised end-to-end against a running sshd, systemd or LXC in this environment.

Security and design

  • OpenSSH performs the chroot after authentication. Every component of the chroot path must be root-owned and not group/other writable. SSH host key validation is still required. The account must not have another unjailed login path. Review local PAM, AllowUsers, AllowGroups, Match, and other authentication policy.
  • dash is a real shell, not a restricted shell. Users who can upload files can potentially execute their own programs. /data/USER is writable and is not encrypted or quota-limited. A chroot shares the host kernel and is not a substitute for a VM or security boundary against a hostile user.
  • Runtime files are copies, not hard links. This works across filesystems and avoids changes to host inodes. Copies do not update when packages update; run bin/refresh-runtime NAME after updates, before exposing users again. The refresh is per-file, not atomic across the runtime; run during maintenance.
  • The systemd service persists /dev/null and /dev/tty using bind mounts; no mknod is needed. Some LXC configurations forbid mount operations inside a container; if so, arrange the bind mount using the container host's facilities. The service is ordered before SSH if both start, but does not block global SSH on failure. An active oneshot does not monitor mounts afterward.
  • This first version does not install persistent source-directory mounts, guard mount disappearance, or automatically refresh updates. Do not use it for secrets until you have independently arranged and verified a read-only mount and fail-closed access. See mounts for read-only and writable examples using either fstab or the existing jail systemd service.
  • An existing user, jail, group, key file or systemd unit is never knowingly repurposed. If a script fails partway through, inspect before retrying; account and filesystem changes are not transactional.

Requirements

Debian Trixie, systemd, OpenSSH server and client, rsync, coreutils, util-linux, Bash and glibc tools (ldd). Root access and a working backup/console are needed. Other distributions and versions require adjustment and testing.

apt-get update
apt-get install --no-install-recommends openssh-server openssh-client rsync unzip

Copy the ZIP to the server and unpack it in /root (or adjust the path):

cd /root
unzip ssh-chroot-jail-repository.zip
cd ssh-chroot-jail

Run the commands below as root. Inspect the scripts first. Keep an administrative SSH session and console access open: create-user validates and restarts ssh.service.

Set up a jail and account

cd /root/ssh-chroot-jail
./bin/create-jail deploy
./bin/enable-jail deploy
./bin/create-user deploy deployer

The user has a non-usable password hash, home / and a dedicated group. create-user checks that the jail service is enabled and active and both device binds are present before changing accounts or files. Other later failures can still leave partial setup. The jail includes a minimal passwd/group database, shell, ls, cp, mv, rm, rsync, scp, external sftp-server and shared libraries discovered by ldd. The writable directory belongs to the user; the chroot root remains root-owned. Only add trusted programs: ldd must not be used on untrusted executables.

Review the generated account, mounts and permissions:

id deployer
findmnt -M /srv/ssh-jails/deploy/dev/null -o TARGET,SOURCE,VFS-OPTIONS
findmnt -M /srv/ssh-jails/deploy/dev/tty -o TARGET,SOURCE,VFS-OPTIONS
ls -ld /srv /srv/ssh-jails /srv/ssh-jails/deploy /srv/ssh-jails/deploy/data/deployer

SSH configuration

create-user invokes bin/configure-ssh, which creates /etc/ssh/sshd_config.d/00-jail-USER.conf, validates sshd -t and the effective Match settings, and restarts ssh.service rather than sending SIGHUP. The account is created with /usr/sbin/nologin until the restart succeeds, and its authorized-key file is initially empty. A failed operation can leave a partially created account; inspect the output before retrying.

If you ran the earlier version of create-user and already have the dedicated account, matching jail passwd entry and empty root-owned key file, run:

./bin/configure-ssh deploy deployer

The script refuses to overwrite a drop-in or use a nonempty key file. It checks settings for 127.0.0.1; existing address-specific Match rules may produce different results for the actual client. Check that address explicitly before adding a key:

sshd -T -C user=deployer,host=localhost,addr=CLIENT_ADDRESS | \
  grep -E '^(chrootdirectory|authorizedkeysfile|authorizedkeyscommand|trustedusercakeys|authenticationmethods|pubkeyauthentication|passwordauthentication|kbdinteractiveauthentication|disableforwarding|permittunnel|permituserrc|permittty|forcecommand) '

Compare the output with the generated drop-in. An existing Match rule can take precedence. AllowUsers, AllowGroups, PAM, expiry, or other policy can affect login. Make sure no other SSH daemon or host access path permits an unjailed login. On r46p we observed reload fail with socket activation; the script uses a restart, with a brief interruption for new connections. If SSH fails, use the open session or console, inspect systemctl status ssh.service and the journal.

Confirm a new administrative login before proceeding. Then put only the client's public key in /etc/ssh/authorized_keys/deployer and validate it with ssh-keygen -lf. Ensure the account cannot authenticate through another key source or policy. Check the server host key fingerprint out of band.

# On client: replace host and local paths.
ssh -6 -tt deployer@example.org
# Exit the shell, then test a command and transfer:
ssh -6 -T deployer@example.org 'ls -l /data/deployer'
rsync -rlt -e 'ssh -6' ./files/ deployer@example.org:/data/deployer/
sftp -6 deployer@example.org

SCP defaults to SFTP with recent OpenSSH; scp -O uses the legacy scp binary. Test both if needed. SFTP logs may require a jail /dev/log. Host sshd's configured SFTP subsystem must be the copied /usr/lib/openssh/sftp-server or internal-sftp; verify with sshd -T. Interactive sessions are available when SSH and authorized-key policy permit a PTY. A login shell reads /.profile and shows username@hostname:/directory$ , with a space after the dollar sign. The prompt is written automatically when the jail is created; no separate prompt setup is needed.

Data binds: fstab or the jail service

The mount guide explains how to expose additional host directories read-only or writable, test permissions, and make them persistent. Choose either fstab entries or an ExecStartPost drop-in on the existing ssh-jail-NAME.service. The latter keeps device and data mount setup in one service. Neither method adds a per-login mount check; follow the guide before exposing secrets.

Manual setup and command alternatives

To set up the jail, account, mounts, SSH configuration and prompt without the provided scripts, follow the manual guide.

General-purpose commands

# Make the required chroot root hierarchy (not a complete runnable jail).
install -d -o root -g root -m 0755 /srv/ssh-jails/example
# Mount an existing device; the target must already be an empty file.
mount --bind /dev/null /srv/ssh-jails/example/dev/null
# Verify the precise mount, not merely the service state.
findmnt -M /srv/ssh-jails/example/dev/null -o TARGET,SOURCE,VFS-OPTIONS
# Test commands without an interactive terminal.
ssh -T user@example.org 'ls -l /data/user'
# Pull a read-only source exposed in the jail, after the safeguards in mounts.md.
rsync -rlt -e 'ssh -6' user@example.org:/certs/ /root/certsync-test/

Maintenance and recovery

  • After package upgrades: ./bin/refresh-runtime deploy, inspect output, and re-test. Refreshing running programs does not replace their mapped files.
  • Service status: systemctl status ssh-jail-deploy.service and findmnt -M.
  • The service intentionally does not unmount on stop; inspect and manually unmount before removing the jail. Never recursively delete mounted directories.
  • No script modifies firewall rules or service manager policy globally.

References