Claude Code guide in three levels, Beginner to Advanced: hands-on lessons with checked exercises, every volatile fact cited to its source. Independent community guide.
Permissions, settings scopes and the sandbox
Intermediate · Lesson 4 of 8 · about 20 minutes · Needs: Organize project memory and see what loaded · Verified against Claude Code v2.1.308 (stable) on 2026-10-25
Goal
By the end of this lesson you can choose a narrow permission rule, put it in the right settings scope, and verify the sandbox's boundary.
You need
- Your practice copy with Node.js LTS for the Worked example and the check.
- One of your own repositories with a test command for Your turn; or use the practice copy for both. Keep your home directory outside the project's writable folders for the sandbox probe.
- macOS, Linux or WSL 2. On Linux and WSL 2, the sandbox needs
bubblewrapandsocat(Set up Linux and WSL2); a Codespace on your template copy has both. - Usage: light.
The idea
The file a rule goes in decides who it affects (Settings):
| File | Affects | Put here |
|---|---|---|
~/.claude/settings.json |
You, in every project | Personal defaults |
.claude/settings.json, committed |
Everyone in the project | Team rules |
.claude/settings.local.json, kept out of git |
You, in this project | Personal choices |
| Managed settings | Your organization | Organization policy |
When the same key is set in more than one place, the higher level wins: managed, then the command line, then local, then shared project, then user (Settings precedence). Rule lists such as permissions.allow merge across the files instead. Among the rules, deny is checked first, then ask, then allow, and the first match decides (Configure permissions). In auto mode, an action that matches one of your rules resolves before the classifier sees it, apart from a few exceptions such as writes to protected paths: deny blocks it, ask prompts you, allow lets it run. Auto mode drops broad allow rules, such as package-manager run commands, and keeps narrow ones like Bash(npm test) (How auto mode evaluates actions).
By default, the sandbox restricts shell writes to the project, a per-user temporary folder and added directories, using an operating-system boundary.
Worked example
In your practice copy:
Open a terminal in your practice copy and start a Manual session with
claude --permission-mode default. Check that the footer saysmanual mode on. Manual is the deliberate exercise here: auto mode can approve a test run without a prompt, so the missing prompt alone would not demonstrate an allow rule (Choose a permission mode). Then run/status. ItsSetting sourcesline lists the settings files this session loaded. Open/sandbox; keep the sandbox off for the rule comparison, then close the panel.Save the team's rules as
.claude/settings.json. Merge this block with any settings already there, rather than replacing unrelated keys. These are the docs' rules for keeping Claude out of.envfiles (Exclude sensitive files), plus the exact test command and an ask rule for pushes:{ "permissions": { "allow": ["Bash(npm test)"], "ask": ["Bash(git push *)"], "deny": ["Read(./.env)", "Read(./.env.*)"] } }Auto mode on its own lets Claude read
.env(What the classifier blocks by default): the deny rules are what stop it. The ask rule makes Claude Code prompt before a push, even in auto mode (Add a human checkpoint). Claude Code reloads permission rules when the file changes (When edits take effect). Run/permissions: it lists every rule and the settings file it comes from.The practice copy has no
.env, so make a dummy one with a fake value, and ask Claude to read it:echo "DEMO_TOKEN=not-a-real-secret" > .envRead .env and tell me what it holds.The deny rule blocks the read: deny rules prevent Claude Code from using the tool on that path. Delete the dummy file afterwards:
rm .env.Ask Claude to
Run npm test. Read the test result. In Manual mode, with the sandbox off and no matching ask or deny rule, this exact command runs without a permission prompt because the allow rule covers it. If it asks, inspect the command and rule sources instead of approving a broader grant.Turn on the sandbox. Run
/sandboxand, on the Mode tab, select auto-allow. Claude Code saves the setting to.claude/settings.local.json, your personal file (Get started). Permission approval and that boundary answer different questions: may this command run, and what can it reach? If/sandboxshows only a Dependencies tab, installbubblewrapandsocat, restart Claude Code and run/sandboxagain.Ask Claude to try a write outside the project:
Run this exact command and show me its output: touch ~/sandbox-probeRead the tool's output:
Operation not permittedon macOS, orRead-only file systemon Linux and WSL 2 (Confirm commands run inside the sandbox). If Claude offers to retry outside the sandbox, decline. The rejected write is the useful evidence, not whether a permission prompt appeared. If thetouchsucceeds, delete~/sandbox-probeand run/sandboxto check that the sandbox is on.
Your turn
Audit a proposed team setup in your own repository. A teammate suggests Bash(npm *) so tests run without prompts, wants your personal sandbox choice committed too, and asks Claude in the conversation not to push without asking. Keep .env from Claude for everyone, keep tests prompt-free, and make pushes ask first.
Choose the exact command from your repository's test script. Share that narrow rule, an ask rule for pushes and the
.envdenies; keep your personal sandbox choice local.Bash(npm *)would also approve unrelated npm commands. A request made in the conversation can be lost when compaction drops the message; an ask rule always prompts for a matching command, even in auto mode (Add a human checkpoint). Finish this partial block in.claude/settings.json, preserving existing settings. Use the Worked example's deny rules for both the base file and the.env.*family:{ "permissions": { "allow": ["TODO: the command that runs your tests"], "ask": ["TODO: a command the team approves each time"], "deny": ["TODO: reading .env", "TODO: reading .env.local and the other .env.* files"] } }Replace every
TODObefore saving the final file. Allow only the one command that runs your tests, such asBash(npm test)orBash(pytest), and ask before pushes withBash(git push *).Review and commit the team's rules. Use your repository's terminal. Check that the commit contains the narrow test rule, the ask rule and both deny rules, while preserving the earlier lessons' settings.
In a Claude Code session in your repository, turn on the sandbox with
/sandboxin auto-allow mode. The Config tab shows the resolved sandbox settings (Get started). Your local file outranks the shared one, so a local"enabled": falseoverrides a sharedtrue(Settings precedence):jq .sandbox .claude/settings.local.jsonshows what yours holds. Ask Claude toRun this exact command and show me its output: touch ~/sandbox-probe, and inspect the actual failure. If Claude offers to retry outside the sandbox, decline.Run
git status --shortandgit ls-files .claude/settings.local.jsonin your repository's terminal. Your personal file should appear in neither output. Claude Code keeps it out of git when it creates it; if you created it by hand, add it to.gitignoreyourself (Keep personal settings out of a repository). Leave Manual mode when the exercise is finished: pressShift+Tabuntil the footer showsauto mode on, if auto mode is available (Switch permission modes).
Check
You are done when all of these are true:
- The committed
.claude/settings.jsondenies reading.envand.env.*files and allows one test command:git show HEAD:.claude/settings.json | jq .permissionsshows them. - It asks before a command the team approves each time, such as
Bash(git push *), inpermissions.ask. - The sandbox is on, and your personal file stays out of git:
jq .sandbox .claude/settings.local.jsonshows"enabled": true, andgit ls-files .claude/settings.local.jsonprints nothing. - Self-check (not tested): you inspected the active rules and sandbox configuration, and Claude's attempted write outside the project failed without an unsandboxed retry.
Run npm run check -- i-4 --dir <your repo> from your practice copy, or npm run check -- i-4 in the copy itself.
If the rules item fails, review Your turn step 1, then commit the corrected settings. If the ask item fails, add the rule to permissions.ask, not deny: a deny rule would block the push you want. If the sandbox item fails, revisit Your turn step 3 in the repository the check reads; a local disable wins over a shared enable.
Watch out
- The sandbox wraps shell commands only. Claude's file and web tools follow permission rules; hooks and local MCP servers run outside it with your full access (What runs outside the sandbox). If the sandbox can't start, Claude Code runs commands unsandboxed unless
sandbox.failIfUnavailableistrue. - A
Readdeny rule doesn't stop an arbitrary script from opening files itself (Read and Edit). With the sandbox on, Claude Code also adds yourReaddeny paths to the sandbox'sfilesystem.denyRead.
Go further
- How we built Claude Code auto mode: Anthropic on what auto mode's classifier catches and what it misses.
- Making Claude Code more secure and autonomous with sandboxing: why the sandbox has a filesystem boundary and a network boundary.
- Security: how Claude Code's protections fit together.
Sources: Settings files and precedence · All settings · Configure permissions · Choose a permission mode · Configure auto mode · Configure the sandboxed Bash tool · git status · git ls-files
← Pick the model and effort · Intermediate index · Enforce a rule with a hook → · Topic: Permissions and safety · Stuck on this lesson?