When a build fails inside Claude Code's Bash sandbox, how do dangerouslyDisableSandbox, allowUnsandboxedCommands and excludedCommands differ?
answer
- a parameter, a switch, a list
- the retry Claude asks for
- strict sandbox mode
- every command in the call must match
- prefer allowWrite over exclusion
basics
~10 sdangerouslyDisableSandbox is a Bash parameter Claude sets to retry a failed command unsandboxed. allowUnsandboxedCommands: false makes Claude Code ignore it. excludedCommands is a settings list of patterns that always run outside the sandbox.
solid answer
~40 s`dangerouslyDisableSandbox` is not a setting. It is a parameter Claude puts on a Bash call when it retries a command that failed in the sandbox, and the retry runs unsandboxed. Who approves it depends on the mode: a prompt titled "Bash command (unsandboxed)" in Manual and `acceptEdits`, the classifier in auto mode, no prompt in `bypassPermissions`, and a refusal in `dontAsk`. `sandbox.allowUnsandboxedCommands` defaults to `true`. Setting it to `false` turns on **strict sandbox mode**, where Claude Code ignores the parameter. `sandbox.excludedCommands` lists `Bash(...)`-style patterns that always run outside the sandbox. A call leaves only when every command in it matches, and a `cd`, a redirect or a `$(...)` keeps it inside. Excluded commands still go through the permission flow, but they run with your full access. Reach for `allowWrite` or `allowedDomains` first.
code
json · 10 lines{
"sandbox": {
"enabled": true,
"allowUnsandboxedCommands": false,
"excludedCommands": ["docker compose *"]
},
"permissions": {
"ask": ["Bash(docker compose *)"]
}
}go deeper
Remember that a failed sandboxed command can be retried outside the sandbox, and that someone must approve that retry unless settings forbid it.
Tell a parameter, a Boolean setting and a pattern list apart, and explain who approves an unsandboxed retry in each permission mode.
Fix sandbox failures with the narrowest control: allowWrite or allowedDomains first, then a tight exclusion paired with an ask rule.
Decide when strict sandbox mode is worth the friction, and which tool exclusions an organization approves centrally instead of per repository.
## Three different kinds of thing Some tools will not work inside the sandbox. `docker` is incompatible with it, `jest` with `watchman` hangs, and some Go-based CLIs fail TLS verification under macOS Seatbelt. Claude Code gives you three controls that look alike but are different kinds of thing: | Identifier | What it is | Who uses it | | --- | --- | --- | | `dangerouslyDisableSandbox` | A **parameter on a Bash call** | Claude, when it retries a failed command | | `sandbox.allowUnsandboxedCommands` | A **Boolean setting**, default `true` | You or your admin, to allow or forbid that retry | | `sandbox.excludedCommands` | A **settings list** of command patterns | You or your admin, for tools that must always run outside | ## The unsandboxed retry When a command fails in the sandbox, Claude sees why. If a network connection was blocked, the result names the denied host. Claude may then retry with `dangerouslyDisableSandbox`, and **the retried command runs unsandboxed**. In an interactive session, who approves it depends on the permission mode: - `bypassPermissions`: runs without a prompt. - Manual and `acceptEdits`: a prompt titled "Bash command (unsandboxed)". - Auto mode: the classifier evaluates the underlying command. - `dontAsk`: the retry is denied. Two rules change that. An allow rule that matches the command, such as `Bash(curl *)`, also approves the retry, so the command leaves the sandbox silently. And an ask rule for `Bash(dangerouslyDisableSandbox:true)` forces a prompt on every retry, even in auto mode and `bypassPermissions`, and it beats a matching allow rule. ## Strict sandbox mode Setting `"allowUnsandboxedCommands": false` turns on **strict sandbox mode**, which the `/sandbox` **Overrides** tab labels by that name. Claude Code then ignores the parameter. While the sandbox is running, every command Claude runs stays sandboxed unless it matches `excludedCommands`. Pair it with `failIfUnavailable: true`, or a machine where the sandbox cannot start will still run commands unsandboxed. Where you set it matters. From Claude Code v2.1.285, a `false` in user settings, `--settings` or managed settings holds even when a project's settings say `true`. A `false` from managed settings or `--settings` also makes the sandbox **admin-required**, and Claude Code then ignores the repository settings that would loosen it, `excludedCommands` included. Commands you type yourself at the `!` prompt still run outside the sandbox in most sessions. ## excludedCommands: matching rules Entries use the same syntax as the inside of a `Bash(...)` permission rule: 1. **End with ` *`** to cover arguments. `docker` matches only a bare `docker`; `docker *` matches it with or without arguments. 2. **Every command in the call must match.** `npm ci && docker compose build` stays sandboxed unless another entry covers `npm ci`. 3. **Matching uses the text of the call.** A script or `make` target that runs `docker` internally does not match, and neither does `/usr/local/bin/docker`. 4. **Some shapes always stay sandboxed**, including a `cd`, a redirect such as `> build.log`, a command substitution or subshell, a command starting with `sudo`, `eval` or `xargs`, and a command name taken from a variable. An excluded command still goes through the **regular permission flow**. Read-only commands and commands covered by your allow rules run without a prompt, and auto mode sends the rest to the classifier. Exclusion is a convenience, not a security boundary. The command runs with **no filesystem limits and no proxy**. ## Choosing the narrowest fix - A tool needs one more directory: use `sandbox.filesystem.allowWrite`. It stays sandboxed. - A tool needs one more host: approve it or add it to `network.allowedDomains`. - A tool cannot work in the sandbox at all, and you trust it with full access: add a **narrow** `excludedCommands` pattern, such as `docker compose *` rather than `docker *`. - A tool fails only occasionally: let the retry prompt you, and decline when you are unsure. The docs warn about broad patterns. If a pattern covers an interpreter, a script in your working directory, or a tool that acts on a file there, as `docker compose` does with its compose file, Claude can edit that file and then run it outside the sandbox. Pairing an exclusion with an ask rule for the same pattern lets you review each run.
- Your auto-mode session keeps retrying failed commands outside the sandbox without asking you. How do you get a prompt back without forbidding retries?Add an ask rule for `Bash(dangerouslyDisableSandbox:true)`. It prompts on every unsandboxed Bash retry, even in auto mode and `bypassPermissions`, and takes precedence over a matching allow rule. To forbid retries entirely, set `sandbox.allowUnsandboxedCommands` to `false`, which turns on strict sandbox mode.
- With an entry `docker *`, why does `cd app && docker compose up` stay sandboxed?A `cd` anywhere in the call keeps the whole call sandboxed, and adding a `cd` entry doesn't change that. The same applies to redirects, command substitutions, subshells, and commands starting with `sudo`, `eval` or `xargs`. Have Claude run `docker compose` from the working directory, or point it at the compose file with an argument instead of changing directory.
- dangerouslyDisableSandbox is a key you set in settings.json.
- Excluded commands skip the permission prompt because they are trusted.
- A docker * entry also covers cd app && docker compose up.
- allowUnsandboxedCommands false also keeps excludedCommands inside the sandbox.
- Excluding a tool is the right fix when it needs one more writable directory.
- In dontAsk mode an unsandboxed retry runs without a prompt.
Assumes Claude Code 2.1.286; from 2.1.285 a user-level allowUnsandboxedCommands false holds against a project's true, and an admin-required sandbox ignores a repository's excludedCommands.