#15: Sandboxing Bash Commands 🏖️
Today we'll look at the sandbox in Claude Code, which puts an operating-system-enforced boundary around the shell commands Claude runs.
With the sandbox on, we decide which files and network hosts those commands can reach. Because the OS enforces the limits while a command runs, Claude Code can run sandboxed commands without asking us to approve each one. It's off by default and works on macOS, Linux, and WSL2.
Let's dive in.
Turning It On
We start a session and run:
/sandboxThat opens a panel with a Mode tab, an Overrides tab, and a Config tab showing the resolved settings. On macOS it uses the built-in Seatbelt framework. On Linux and WSL2 it needs bubblewrap and socat, and a Dependencies tab shows up if anything's missing:
sudo apt-get install bubblewrap socatPicking a mode in the panel saves it to .claude/settings.local.json for the current project. To turn it on everywhere, we set it in ~/.claude/settings.json:
{
"sandbox": {
"enabled": true
}
}Two Modes
The restrictions are the same in both modes. The difference is who approves commands:
- Auto-allow: commands that run inside the sandbox are approved automatically, no prompt.
- Regular permissions: every Bash command still goes through the normal permission prompts.
Even in auto-allow, a few things still prompt, like content-scoped ask rules such as Bash(git push *) and rm commands aimed at a critical path. Deny rules still block, too.
Files And Network
By default, sandboxed commands can write to the working directory, a per-user temp directory, and any directories we've added. They can read most of the machine. To let a tool write somewhere else, we add the path:
{
"sandbox": {
"enabled": true,
"filesystem": {
"allowWrite": ["~/.kube", "/tmp/build"],
"denyRead": ["~/.aws"]
},
"network": {
"allowedDomains": ["github.com", "*.npmjs.org"]
}
}
}Network traffic goes through a proxy on our machine that checks each host. Allowed domains start empty, so the first time a command needs a new host, we get a prompt. Tools that read the proxy variables, like curl, npm, and git over HTTPS, work once their host is allowed. Plain ssh and most database drivers don't use the proxy, so they can't connect.
Escape Hatches
Some tools just don't work inside a sandbox. For a tool we trust with full access, excludedCommands runs matching commands outside it:
{
"sandbox": {
"excludedCommands": ["docker compose *"]
}
}Excluded commands go back through the regular permission flow. When a command fails in the sandbox, Claude can also ask to retry it unsandboxed. To turn that retry off for a session, we can pass settings right on the command line:
claude --settings '{"sandbox": {"enabled": true, "allowUnsandboxedCommands": false}}'The docs call this strict sandbox mode.
What It Doesn't Cover
The sandbox wraps Bash, PowerShell, and Monitor commands and the processes they start. Claude's own Read, Edit, and WebFetch tools follow permission rules instead, and hooks, MCP servers, and our status line command run with full access. Commands we type ourselves at the ! prompt usually run unsandboxed too.
The two layers work together. Permission rules decide whether a tool runs at all, and the sandbox limits what a command can touch once it does. Paths and domains from both get merged, so a WebFetch(domain:...) allow rule also pre-allows that host for sandboxed commands.
To check it's working, we ask Claude (not the ! prompt) to run a command that should fail:
touch ~/sandbox-probeInside the sandbox, that fails with Operation not permitted on macOS. Neat!
More info about sandboxing can be found at the Claude Code docs.