Traditional SSH sandboxes face a fundamental tension, provide enough freedom to the user but keep the environment secure to block malicious users from compromising the whole system. Virtual machines are quite heavy and user namespaces require kernel support and careful privilege management.
In this article we will setup ephemeral linux sessions where each ssh connection spawns a fresh Docker container that lives as long as the session.
This setup can be used as a template for a basic linux sandbox environmement and edited to match your requirements.
Architecture
The solution will be made of three components:
- sshd for user access and rate limiting.
- ForceCommand to intercept login shell and redirect the user to the container spawner.
- Docker to run the sandbox environments.
The SSH configuration
In /etc/ssh/sshd_config, we define a dedicated user with a forced command. We disallow SSH features such as tunnel creation, remote desktop or agent forwarding:
Match User sandbox
ForceCommand sudo /usr/local/bin/spawn-sandbox.sh
PermitTTY yes
X11Forwarding no
PermitTunnel no
AllowAgentForwarding no
AllowTcpForwarding no
AllowStreamLocalForwarding no
The container spawner
Create /usr/local/bin/spawn-sandbox.sh
#!/bin/bash
set -euo pipefail
# Generate a unique container name based on PID and timestamp
CONTAINER_NAME="sandbox-${USER}-${SSH_CONNECTION%% *}-$$-$(date +%s)"
# Cleanup function to ensure container destruction
cleanup() {
docker rm -f "$CONTAINER_NAME" 2>/dev/null || true
}
trap cleanup EXIT INT TERM
# Extract client IP from SSH_CONNECTION
CLIENT_IP="${SSH_CONNECTION%% *}"
# Log the connection attempt
logger -t sandbox-ssh "Connection from $CLIENT_IP (container: $CONTAINER_NAME)"
# Spawn the container with strict isolation
exec sudo docker run \
--name "$CONTAINER_NAME" \
--rm \
--interactive \
--tty \
--network none \
--read-only \
--tmpfs /tmp:rw,noexec,nosuid,size=100m \
--tmpfs /root:rw,noexec,nosuid,size=50m \
--memory 256m \
--memory-swap 256m \
--pids-limit 50 \
--cpu-quota 50000 \
--security-opt no-new-privileges \
--cap-drop ALL \
--workdir /root \
alpine:latest \
/bin/sh
and make it executable
chmod +x /usr/local/bin/spawn-sandbox.sh
exec replaces the shell processes with Docker, ensuring clean signal propagation. When the SSH session closes, the container receives SIGHUP directly.
--rm adds automatic cleanup on exit, combined with the trap, this ensures containers are destroyed even if the script is killed ungracefully.
--network none disables access to the network preventing users from using the container for network attacks and thus, degrading your IP reputation.
- The root is readonly, only the /home and /tmp directories can be modified by users. They are temporal file systems and disappear on user exit.
- Containers have ressource limits that prevent ressources to get exhausted by a single container.
- All capabilities such as
CAP_NET_RAW and CAP_SYS_ADMIN are dropped.
Creating the sandbox user
useradd -r -s /usr/local/bin/spawn-sandbox.sh -m sandbox
We can setup a password for the sandbox user, this password will be shared with the users that will use the sandbox
password sandbox
Allowing sandbox user to spawn a docker container
To allow sandbox user to spawn a Docker container, we need to give it sudo access to the docker run command.
In /etc/sudoers.d/sandbox add:
sandbox ALL=(root) NOPASSWD: /usr/local/bin/spawn-sandbox.sh
Rate limiting with iptables
We can use iptables to limit connections per IP to prevent abuse.
# Create a new chain for SSH rate limiting
iptables -N SSH_RATE_LIMIT
# Allow established connections
iptables -A SSH_RATE_LIMIT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
# Limit new connections to 2 per IP, then drop excess
iptables -A SSH_RATE_LIMIT -m conntrack --ctstate NEW \
-m recent --set --name ssh_limit --rsource
iptables -A SSH_RATE_LIMIT -m conntrack --ctstate NEW \
-m recent --update --seconds 60 --hitcount 3 --name ssh_limit --rsource \
-j DROP
iptables -A SSH_RATE_LIMIT -j ACCEPT
# Apply to incoming SSH connections
iptables -I INPUT -p tcp --dport 22 -j SSH_RATE_LIMIT
Using your own linux image
In the demo, we are using a basic Alpine image, despite being light, it provides only a few packages and the user experience is limited.
You can customize the sandbox environment by using custom images.
Write this in a Dockerfile file
FROM alpine:latest
RUN apk add --no-cache vim jq tree python3 htop
Build the container with this command
docker build -t sandbox-alpine .
And edit the image in /usr/local/bin/spawn-sandbox.sh
exec sudo docker run \
# ....
sandbox-alpine \ # Instead of alpine:latest
# ...
Security consideration
Despite security measures being implemented, this setup doesn't protect against:
- Kernel exploits: Containers share the host kernel. A container escape vulnerability could compromise the host. Keep your kernel patched.
- Ressource contention: While individual containers are limited, multiple containers can still overhelm a host with limited RAM. Consider setting
MaxSessions in sshd_config based on your hardware.
- Timing attacks: Users might be able to infer information about other containers through timing analysis of CPU scheduling or I/O operations. For truly sensitive environments, consider VM-level isolation.