Running AI agents on Linux requires defense-in-depth: sandbox the runtime, enforce least privilege, harden the host kernel, and audit continuously. This guide consolidates everything you need to secure AI agent workloads on Linux—from Docker and gVisor sandboxing to seccomp, SELinux, and runtime monitoring.
Why AI Agent Security Is Different
AI agents aren’t ordinary applications. They:
- Execute arbitrary code — LLMs can generate and run shell commands, Python, JavaScript, and more
- Access sensitive data — API keys, databases, file systems, and network services
- Chain actions autonomously — One prompt can trigger a cascade of tool calls without human review
- Run with elevated context — Often deployed with broad permissions for “convenience”
The traditional perimeter model fails here. You must assume the agent will be compromised and limit the blast radius.
Layer 1: Runtime Sandboxing
Docker — Baseline Isolation
Start with Docker, but understand its limits:
# Minimal secure Docker run
docker run --rm
--user 1000:1000
--read-only
--tmpfs /tmp:noexec,nosuid,size=100m
--cap-drop ALL
--security-opt no-new-privileges:true
--network none
--memory 512m --cpus 0.5
my-ai-agent:latest
Key flags explained:
| Flag | Purpose |
|---|---|
--user 1000:1000 |
Run as non-root (UID 1000) |
--read-only |
Root filesystem immutable |
--tmpfs /tmp:noexec,nosuid |
Writable temp, no exec, no suid |
--cap-drop ALL |
Drop all Linux capabilities |
--security-opt no-new-privileges:true |
Prevent privilege escalation via setuid |
--network none |
No network by default |
--memory/--cpus |
Resource limits prevent DoS |
gVisor — Stronger Sandbox
gVisor intercepts syscalls in userspace, providing a second kernel boundary:
# Install gVisor (runsc)
curl -fsSL https://gvisor.dev/archive.key | sudo gpg --dearmor -o /usr/share/keyrings/gvisor-archive-keyring.gpg
echo "deb [arch=amd64 signed-by=/usr/share/keyrings/gvisor-archive-keyring.gpg] https://storage.googleapis.com/gvisor/releases release main" | sudo tee /etc/apt/sources.list.d/gvisor.list
sudo apt update && sudo apt install runsc
# Configure Docker to use runsc
cat <
Trade-off: gVisor adds ~10-20% syscall overhead but blocks entire classes of kernel exploits. Use for untrusted agent code.
Kata Containers / Firecracker — VM-Level Isolation
For maximum isolation, each agent gets a lightweight VM:
- Kata Containers: OCI-compatible, runs unmodified Docker images
- Firecracker: MicroVMs, used by AWS Lambda/Fargate
- gVisor vs Kata: gVisor = userspace kernel (~10% overhead); Kata = real VM (~2-5% overhead with hardware virt)
Layer 2: Least Privilege Filesystem
Bind Mounts — Only What's Needed
# Mount only specific directories, read-only where possible
docker run --rm
-v /host/data:/app/data:ro
-v /host/config:/app/config:ro
-v /host/logs:/app/logs:rw
--tmpfs /app/cache:noexec,nosuid,size=50m
my-ai-agent:latest
Rootless Containers (Podman)
Run containers without root daemon:
# Install Podman
sudo apt install podman
# Run rootless (no sudo needed)
podman run --rm
--user 1000:1000
--read-only
--cap-drop ALL
--security-opt no-new-privileges:true
my-ai-agent:latest
Podman benefits: no root daemon, better SELinux integration, systemd user services.
Layer 3: Linux Capabilities & seccomp
Drop All Capabilities
# Start with zero, add only what's needed
--cap-drop ALL
# If agent truly needs specific capabilities:
--cap-add=CAP_DAC_OVERRIDE # bypass file permissions (rarely needed)
--cap-add=CAP_NET_BIND_SERVICE # bind to ports <1024 (rarely needed)
Custom seccomp Profile
Create seccomp-profile.json:
{
"defaultAction": "SCMP_ACT_ERRNO",
"architectures": ["SCMP_ARCH_X86_64"],
"syscalls": [
{"names": ["read", "write", "openat", "close", "mmap", "munmap", "brk", "rt_sigaction", "rt_sigprocmask", "exit_group", "exit", "getpid", "getuid", "geteuid", "getgid", "getegid"], "action": "SCMP_ACT_ALLOW"},
{"names": ["clone", "fork", "vfork", "execve", "execveat"], "action": "SCMP_ACT_ERRNO"},
{"names": ["ptrace", "process_vm_readv", "process_vm_writev"], "action": "SCMP_ACT_ERRNO"}
]
}
Apply it:
docker run --security-opt seccomp=/path/to/seccomp-profile.json ...
SELinux / AppArmor
On Ubuntu, AppArmor profiles provide mandatory access control:
# Install apparmor-utils
sudo apt install apparmor-utils
# Create profile for AI agent
cat <
profile docker-ai-agent flags=(attach_disconnected,mediate_deleted) {
#include
# Read-only access to config
/host/config/** r,
# Read-only data access
/host/data/** r,
# Write logs
/host/logs/** rw,
# Deny exec
deny /{,usr/}bin/** ix,
# Network
network inet stream,
network inet6 stream,
# Capabilities
capability,
}
EOF
sudo apparmor_parser -r /etc/apparmor.d/docker-ai-agent
# Apply to container
docker run --security-opt apparmor=docker-ai-agent ...
Layer 4: Kernel Hardening
These sysctl settings reduce attack surface:
# /etc/sysctl.d/99-ai-agent-hardening.conf
# Disable SysRq
kernel.sysrq = 0
# Restrict dmesg
kernel.dmesg_restrict = 1
# Restrict perf
kernel.perf_event_paranoid = 3
# Hide kernel symbols
kernel.kptr_restrict = 2
# Disable kexec
kernel.kexec_load_disabled = 1
# Restrict unprivileged BPF
kernel.unprivileged_bpf_disabled = 1
# Restrict user namespaces (if not needed)
user.max_user_namespaces = 0
# Apply
sudo sysctl --system
Layer 5: Runtime Monitoring & Audit
Falco — Runtime Threat Detection
# Install Falco
curl -fsSL https://falco.org/repo/falcosecurity-3672BA8F.asc | sudo gpg --dearmor -o /usr/share/keyrings/falco-archive-keyring.gpg
echo "deb [signed-by=/usr/share/keyrings/falco-archive-keyring.gpg] https://download.falco.org/packages/deb stable main" | sudo tee /etc/apt/sources.list.d/falcosecurity.list
sudo apt update && sudo apt install falco
# Custom rules for AI agents
cat <
auditd — Kernel Audit
# /etc/audit/rules.d/ai-agent.rules
# Watch agent config and data directories
-w /host/config -p wa -k ai-agent-config
-w /host/data -p wa -k ai-agent-data
-w /host/logs -p wa -k ai-agent-logs
# Watch Docker socket
-w /var/run/docker.sock -p wa -k docker-socket
# Watch runsc/gVisor
-w /usr/bin/runsc -p x -k gvisor-exec
# Apply
sudo augenrules --load
sudo systemctl restart auditd
Layer 6: Supply Chain Security
Image Signing & Verification
# Install cosign
curl -O https://github.com/sigstore/cosign/releases/latest/download/cosign-linux-amd64
sudo mv cosign-linux-amd64 /usr/local/bin/cosign
sudo chmod +x /usr/local/bin/cosign
# Generate keypair
cosign generate-key-pair
# Sign image
cosign sign --key cosign.key my-ai-agent:latest
# Verify at deploy time
cosign verify --key cosign.pub my-ai-agent:latest
SBOM Generation
# Install syft
curl -sSfL https://raw.githubusercontent.com/anchore/syft/main/install.sh | sh -s -- -b /usr/local/bin
# Generate SBOM
syft my-ai-agent:latest -o spdx-json > sbom.spdx.json
# Scan for vulnerabilities
grype my-ai-agent:latest
Layer 7: Incident Response
Forensic Readiness
- Immutable logs: Ship container logs to centralized logging (Loki, Elasticsearch) with tamper-proof storage
- Memory dumps: Enable
docker commiton crash for post-mortem - Network captures: Use
tcpdumpsidecar for suspicious containers - Checkpoint/restore: CRIU for container state snapshots
Automated Containment
#!/bin/bash
# auto-contain.sh — triggered by Falco alert
CONTAINER_ID=$1
REASON=$2
# Snapshot for forensics
docker commit $CONTAINER_ID forensics/ai-agent-$(date +%s)
# Pause container (freezes processes)
docker pause $CONTAINER_ID
# Capture network state
docker exec $CONTAINER_ID ss -tulpn > /forensics/netstate-$CONTAINER_ID.txt
# Alert
curl -X POST $WEBHOOK_URL -d "{"container":"$CONTAINER_ID","reason":"$REASON"}"
Complete Deployment Checklist
| Layer | Control | Status |
|---|---|---|
| 1. Runtime | Docker + gVisor/Kata | ☐ |
| 2. Filesystem | Bind mounts, read-only, tmpfs | ☐ |
| 3. Capabilities | Drop ALL, custom seccomp | ☐ |
| 4. Kernel | Sysctl hardening | ☐ |
| 5. Monitoring | Falco + auditd | ☐ |
| 6. Supply Chain | cosign + syft + grype | ☐ |
| 7. IR | Forensic readiness + auto-contain | ☐ |
Related Tools on DevStackIO
- Docker Run to Compose Converter — Convert your secure
docker runto Compose - chmod Calculator — Calculate filesystem permissions
- SSH Hardening Guide — Harden host SSH access
- CSP Generator — Build Content-Security-Policy headers
References
- Docker Security Documentation — https://docs.docker.com/engine/security/
- gVisor Documentation — https://gvisor.dev/docs/
- Kata Containers — https://katacontainers.io/
- Falco Rules — https://falco.org/docs/rules/
- cosign/sigstore — https://docs.sigstore.dev/
- Linux Capabilities — man 7 capabilities
- seccomp — man 2 seccomp
- AppArmor — https://wiki.ubuntu.com/AppArmor