Sandbox mode limits which files the Codex CLI can read or write while executing commands, reducing the chance of accidental edits and unintended data exposure.

When running a task through codex exec, tool calls and shell commands execute under a sandbox policy selected with the -s option. Common modes include read-only (no filesystem writes), workspace-write (writes allowed within the current workspace), and danger-full-access (broad access beyond the workspace); the default for codex exec is read-only.

Keep the workspace directory narrow and predictable before enabling write access, since workspace-write can modify any file under that root. Reserve danger-full-access for short, deliberate operations and avoid running it from directories that contain secrets (for example /home/user/.ssh) or critical system paths (for example /etc), because it can overwrite configuration or leak credentials. You can also persist a default sandbox in /~/.codex/config.toml via sandbox_mode.

Steps to set Codex sandbox mode:

  1. Change to the directory that should be treated as the Codex workspace.
    $ cd ~/work/project
  2. Confirm the workspace boundary by printing the current directory path.
    $ pwd
    /home/user/work/project
  3. List the top-level files to confirm the correct workspace is selected.
    $ ls -1
    README.md
    src
    tests
  4. Run Codex in read-only mode for a single command.
    $ codex exec -s read-only "Return OK."
    OK

    The read-only sandbox prevents creating, deleting, or modifying files.

  5. Attempt a workspace write in read-only mode to confirm changes are blocked.
    $ codex exec -s read-only "Create a file named codex-sandbox-test.txt containing OK."
    I can't write files in this environment because the sandbox is read-only.
    If you want to create it yourself, run:
    printf "OK" > /home/user/work/project/codex-sandbox-test.txt
  6. Verify the test file was not created in the workspace.
    $ ls -l codex-sandbox-test.txt
    ls: cannot access 'codex-sandbox-test.txt': No such file or directory
  7. Run Codex in workspace-write mode when edits are required.
    $ codex exec -s workspace-write "Return OK."
    OK

    The workspace-write sandbox allows writes under the current workspace directory.

  8. Create a test file inside the workspace using workspace-write mode.
    $ codex exec -s workspace-write "Create a file named codex-sandbox-test.txt containing OK."
    Created `codex-sandbox-test.txt` with `OK` in /home/user/work/project.
  9. Verify the test file exists in the workspace.
    $ ls -l codex-sandbox-test.txt
    -rw-r--r-- 1 user user 2 Jan 19 11:39 codex-sandbox-test.txt
  10. Verify the test file content matches the request.
    $ cat codex-sandbox-test.txt
    OK
  11. Remove the test file after verification.
    $ rm -f codex-sandbox-test.txt
  12. Run Codex in danger-full-access mode only when access outside the workspace is required.
    $ codex exec -s danger-full-access "Return OK."
    OK

    The danger-full-access sandbox can read and write outside the workspace, including sensitive paths like /etc and /home.

  13. Prefer returning to read-only mode after completing the privileged task.
    $ codex exec -s read-only "Return OK."
    OK