DEV Community

QuickStack Media
QuickStack Media

Posted on Edited on AI-assisted

Running Claude Code 24/7 on a Sandboxed Linux VPS: Dev Container Firewall, KVM 4 vs KVM 8 Sizing & TCO

Running autonomous AI coding agents like Claude Code 24/7 on cloud infrastructure requires treating the runtime as an untrusted execution boundary. When an agent installs npm or Python dependencies, executes arbitrary shell tests, and reads local repository files unattended, a single prompt injection or hallucinated command can exfiltrate secrets or scan internal networks. Based on modeled production benchmarks and late-2026 hosting economics, this engineering guide breaks down how to isolate Claude Code inside a hardened Linux Dev Container with an iptables + ipset egress firewall, size Hostinger KVM 4 versus KVM 8 accurately, and calculate true 12-month and post-renewal Total Cost of Ownership (TCO).

Quick Verdict

Sustaining unattended Claude Code workflows without CPU throttling or out-of-memory (OOM) kills requires a dedicated 4-vCPU / 16 GB RAM floor, making flat-rate KVM VPS infrastructure dramatically cheaper than per-hour cloud dev sandboxes.

  • KVM 4 Baseline (Recommended for Solo & Single-Repo Loops): 4 vCPU, 16 GB RAM, 200 GB NVMe. Runs 1–2 concurrent Claude Code sessions, parallel Vitest/Jest test runners, and a local pgvector codebase index cleanly at $12.99/mo on a 24-month US promo ($155.88/yr; $311.76 upfront; renews at $28.99/mo or $347.88/yr).
  • KVM 8 Multi-Agent Tier (Teams & Co-Located n8n): 8 vCPU, 32 GB RAM, 400 GB NVMe. Required once you run 3+ concurrent agent sandboxes or co-locate n8n Queue Mode (Redis + PostgreSQL) on the same node, priced at $25.99/mo on a 24-month promo ($311.88/yr; $623.76 upfront; renews at $49.99/mo or $599.88/yr).
  • Verified 10% Checkout Discount: Clicking our Hostinger KVM VPS (Claude Code Template) link first and applying coupon code QUICKSTACK10 at checkout drops 24-month KVM 4 pricing to ~$11.69/mo ($280.58 for 24 months; $140.29/yr amortized) and KVM 8 to ~$23.39/mo ($561.38 for 24 months; $280.69/yr amortized).
  • The Winning Stack: A Hostinger KVM 4 VPS running a non-root Dev Container with default-deny iptables egress filtering and $4/mo off-server backups costs $188.29/yr in direct cash during Years 1–2 with QUICKSTACK10—roughly 5.6× cheaper than running an equivalent 4-core GitHub Codespace for just 8 hours per day ($1,063.68/yr).

Architectural Threat Model and Sandboxed Dev Container Design

Autonomous coding agents fail differently than traditional web services. Because Claude Code legitimately needs shell access, git credentials, and package registries to edit code and run test suites, host-level hypervisor isolation alone is not enough—a compromised dependency inside node_modules can still attempt outbound data exfiltration unless the container network namespace itself enforces a strict egress allowlist.

The critical architectural rule is to enforce the egress firewall inside the Dev Container, never by flushing iptables blindly on the VPS host. Flushing host INPUT and nat tables without explicit SSH and Docker bridge allowances will immediately sever your SSH session and break Docker container networking. Instead, keep standard UFW/SSH key-only hardening on the KVM host, run the coding agent as a non-root user inside a Dev Container, and grant only the minimum Linux network capabilities (NET_ADMIN and NET_RAW) scoped to a single root-owned firewall initializer via sudoers.

You can pull the complete, ready-to-run Dockerfile, .devcontainer, and firewall scripts directly from our QuickStack Starter Blueprints on GitHub under claude-code-vps-starter/.

Below is the production .devcontainer/devcontainer.json blueprint that bounds RAM/CPU per sandbox, persists Claude Code session state across container rebuilds, and executes the egress firewall automatically on startup:

{
  "name": "QuickStack Claude Code 24/7 Sandboxed VPS",
  "build": {
    "dockerfile": "Dockerfile",
    "context": ".."
  },
  "remoteUser": "agent",
  "runArgs": [
    "--cap-add=NET_ADMIN",
    "--cap-add=NET_RAW",
    "--memory=10g",
    "--cpus=3.0"
  ],
  "containerEnv": {
    "NODE_OPTIONS": "--max-old-space-size=4096",
    "CLAUDE_CONFIG_DIR": "/home/agent/.claude"
  },
  "postStartCommand": "sudo /usr/local/bin/init-firewall.sh",
  "mounts": [
    "source=claude-code-state,target=/home/agent/.claude,type=volume"
  ]
}
Enter fullscreen mode Exit fullscreen mode

Four guardrails inside this container specification prevent common 24/7 operational failures:

  • Non-Root Agent Isolation: Running as agent with NOPASSWD sudo restricted exclusively to /usr/local/bin/init-firewall.sh ensures the agent process cannot edit firewall rules, install system packages, or tamper with root-owned binaries after startup.
  • Explicit Memory Ceiling (--memory=10g & max-old-space-size=4096): Caps runaway TypeScript compilers or webpack builds inside a KVM 4 (16 GB RAM) node so a single container never triggers the Linux kernel OOM killer against host SSH or background databases.
  • Persistent State Volume: Mounting /home/agent/.claude to a named Docker volume (pre-owned by agent:agent in the Dockerfile before USER agent) prevents EACCES permission crashes while retaining authentication and project context across container restarts.
  • Unattended Session Persistence: Running Claude Code inside tmux or a supervised container entrypoint inside the Dev Container removes the 30-minute idle timeout and 12-hour maximum lifetime caps enforced by hosted cloud IDE workspaces.

Container Egress Firewall: iptables + ipset Dynamic Allowlisting

Default Docker bridge containers permit unrestricted outbound internet access on every port. Locking egress inside the container requires three elements working together: preserving Docker's internal DNS NAT rules (127.0.0.11), populating an ipset of type hash:net (required for CIDR ranges—hash:ip fails immediately when fed subnet prefixes), and blocking IPv6 egress so dual-stack VPS nodes cannot silently bypass IPv4 rules.

The following hardened init-firewall.sh script dynamically pulls GitHub's live CIDR blocks from api.github.com/meta, resolves Anthropic and npm registry endpoints using -exist to prevent duplicate-IP crashes, enforces default DROP policies, rejects blocked egress with icmp-admin-prohibited, and runs mandatory live verification checks before exiting:

#!/usr/bin/env bash
set -euo pipefail

# 1. Preserve Docker internal DNS NAT rules (127.0.0.11) before flushing
DOCKER_DNS_RULES=$(iptables-save -t nat | grep "127\.0\.0\.11" || true)

iptables -F
iptables -X
iptables -t nat -F
iptables -t nat -X
iptables -t mangle -F
iptables -t mangle -X

if [ -n "$DOCKER_DNS_RULES" ]; then
  iptables -t nat -N DOCKER_OUTPUT 2>/dev/null || true
  iptables -t nat -N DOCKER_POSTROUTING 2>/dev/null || true
  echo "$DOCKER_DNS_RULES" | xargs -L 1 iptables -t nat
fi

# 2. Allow DNS, loopback, and local host bridge before fetching live allowlists
iptables -A OUTPUT -p udp --dport 53 -j ACCEPT
iptables -A INPUT -p udp --sport 53 -j ACCEPT
iptables -A OUTPUT -p tcp --dport 53 -j ACCEPT
iptables -A INPUT -p tcp --sport 53 -j ACCEPT
iptables -A INPUT -i lo -j ACCEPT
iptables -A OUTPUT -o lo -j ACCEPT

# 3. Create hash:net ipset (supports both /32 hosts and CIDR ranges)
ipset create allowed-domains hash:net -exist
ipset flush allowed-domains

# Dynamically fetch GitHub web, api, and git CIDRs (enforce HTTPS only)
GH_META=$(curl --proto '=https' --proto-default https -fsSL [api.github.com/meta](https://api.github.com/meta))
for cidr in $(echo "$GH_META" | jq -r '(.web + .api + .git)[]' | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+/[0-9]+$'); do
  ipset add allowed-domains "$cidr" -exist
done

# Dynamically resolve core AI & package registry domains
for domain in "api.anthropic.com" "registry.npmjs.org"; do
  for ip in $(dig +short A "$domain" | grep -E '^[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+$' || true); do
    ipset add allowed-domains "${ip}/32" -exist
  done
done

# 4. Enforce default-deny policies with explicit icmp-admin-prohibited reject
iptables -P INPUT DROP
iptables -P FORWARD DROP
iptables -P OUTPUT DROP

iptables -A INPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
iptables -A OUTPUT -m set --match-set allowed-domains dst -j ACCEPT
iptables -A OUTPUT -j REJECT --reject-with icmp-admin-prohibited

# 5. Block IPv6 outbound egress so dual-stack VPS hosts cannot bypass IPv4 rules
if command -v ip6tables >/dev/null 2>&1; then
  ip6tables -F OUTPUT 2>/dev/null || true
  ip6tables -A OUTPUT -o lo -j ACCEPT 2>/dev/null || true
  ip6tables -A OUTPUT -j REJECT 2>/dev/null || true
fi

# 6. Mandatory self-verification: blocked domain must fail, GitHub API must pass
if curl --proto '=https' --proto-default https --connect-timeout 5 -fsS example.com >/dev/null 2>&1; then
  echo "ERROR: Firewall verification failed — example.com was reachable" >&2
  exit 1
fi

if ! curl --proto '=https' --proto-default https --connect-timeout 5 -fsS [api.github.com/zen](https://api.github.com/zen) >/dev/null 2>&1; then
  echo "ERROR: Firewall verification failed — [api.github.com/zen](https://api.github.com/zen) was unreachable" >&2
  exit 1
fi

echo "[QuickStack Firewall] Verified: default-deny active and allowlisted endpoints reachable."
Enter fullscreen mode Exit fullscreen mode

Using -exist on every ipset add call is non-negotiable in production: DNS resolvers routinely return duplicate IPv4 A records across CDN endpoints, and without -exist, ipset exits with code 1 and blocks container boot entirely.

KVM 4 vs. KVM 8 Sizing and Modeled Production Benchmarks

Choosing between KVM 4 and KVM 8 comes down to concurrent build contention and co-located infrastructure rather than LLM inference speed—because Claude Code streams inference over Anthropic's API, your VPS needs zero GPU hardware and spends its CPU/RAM budget on TypeScript/Rust compilation, test suites, Docker layers, and local databases.

Before locking in a 24-month commitment, you can model exact agent concurrency, RAM ceilings, and SaaS savings using our Interactive AI Agent VPS Sizer & TCO Calculator.

In our modeled production workloads, hardware headroom breaks cleanly across two operational profiles:

  • KVM 4 Capacity Envelope (4 vCPU, 16 GB RAM, 200 GB NVMe): Comfortably sustains 1–2 active Claude Code Dev Containers, parallel Jest/Vitest or pytest runs, and an optional local PostgreSQL + pgvector codebase index. Memory stays within a safe 8–11 GB working set with 5+ GB buffer for Linux page cache.
  • KVM 4 Bottleneck Threshold: Launching 3+ simultaneous coding sandboxes—or co-locating an n8n Queue Mode stack (main + webhook + 2 workers + Redis + Postgres) alongside active Node.js builds—drives CPU wait spikes, triggers Node garbage-collection pauses, and risks swapping during concurrent npm ci runs.
  • KVM 8 Capacity Envelope (8 vCPU, 32 GB RAM, 400 GB NVMe): Absorbs 3–5 isolated Dev Containers or a full team automation node combining Claude Code, Codex CLI, n8n Queue Mode, and a local pgvector index with zero memory starvation.
  • NVMe Build-Cache Longevity: The 400 GB NVMe volume on KVM 8 prevents disk-full crashes caused by Docker BuildKit layers, multi-repo node_modules trees, and nightly pg_dump staging archives—issues that frequently hit 50–100 GB entry VPS tiers within 60 days.

If you are provisioning a fresh node for this stack, click through our Hostinger KVM VPS (Claude Code Template) link first and enter QUICKSTACK10 at checkout to stack an extra 10% off the 24-month promotional rate.

Total Cost of Ownership (TCO): Dedicated KVM VPS vs. Cloud Sandboxes

An honest TCO model must separate direct infrastructure cash, post-promo renewal pricing, automated off-server backups, and realistic engineering maintenance—while recognizing that Anthropic API or Claude subscription token spend applies identically regardless of where your container runs.

Comparing flat-rate dedicated KVM infrastructure against metered cloud Dev Container environments (such as GitHub Codespaces at $0.36/hr for 4 cores / 16 GB RAM and $0.72/hr for 8 cores / 32 GB RAM, plus $0.07/GiB-mo storage) exposes how rapidly hourly sandbox billing compounds:

  • Hostinger KVM 4 (Years 1–2 Promo + Backups): List 24-month promo is $12.99/mo ($155.88/yr; $311.76 upfront). With our Hostinger KVM VPS (Claude Code Template) link + QUICKSTACK10, upfront cost drops to $280.58 ($11.69/mo or $140.29/yr amortized). Adding encrypted off-server Cloudflare R2/S3 backups ($4.00/mo / $48.00/yr) puts direct annual cash at $188.29/yr ($15.69/mo).
  • Hostinger KVM 4 (Year 3+ Renewal Reality): Standard renewal moves to $28.99/mo ($347.88/yr), bringing direct cash with $4/mo backups to $395.88/yr ($32.99/mo). Factoring in 1 hour/month of routine Linux/Docker maintenance ($100/hr amortized) yields a fully loaded TCO of $1,388.29/yr in Years 1–2 and $1,595.88/yr in Year 3+.
  • Hostinger KVM 8 (Years 1–2 Promo + Renewal): List 24-month promo is $25.99/mo ($311.88/yr; $623.76 upfront), dropping to $561.38 ($23.39/mo or $280.69/yr amortized) with QUICKSTACK10. With $4/mo backups, direct cash is $328.69/yr in Years 1–2 and $647.88/yr after renewal ($49.99/mo / $599.88/yr). Co-locating self-hosted n8n Community Edition ($0 license) and local pgvector also saves ~$300/yr ($25/mo) in external managed vector database fees.
  • Metered Cloud Dev Sandbox Baseline (GitHub Codespaces): Running a 4-core / 16 GB machine for 8 active hours/day (240 hrs/mo × $0.36/hr = $86.40/mo, plus 32 GiB storage at $2.24/mo) totals $88.64/mo ($1,063.68/yr)—and scales to $265.04/mo ($3,180.48/yr) for continuous 24/7 equivalent compute (730 hrs/mo), or $530.08/mo ($6,360.96/yr) on an 8-core / 32 GB shape with 64 GiB storage—before hitting Codespaces' 12-hour per-session lifetime limit.

In production, the single fastest way to break a 24/7 sandboxed Claude Code VPS is treating init-firewall.sh as a set-and-forget script after CDN providers rotate IPv4 pools behind api.anthropic.com or registry.npmjs.org. Schedule a lightweight cron or container restart hook that refreshes the allowed-domains ipset cleanly every 6 hours, pin explicit --memory caps on every Dev Container so runaway test suites never starve host SSH, and keep weekly encrypted pg_dump plus /home/agent/.claude snapshots syncing off-box to Cloudflare R2.

Disclosure: QuickStack Media may earn an affiliate commission when you sign up through links in our benchmarks at no extra cost to you.

Top comments (0)