Running Codex in a locked-down workspace prevents accidental edits to the wider filesystem while still enabling automation. Some tasks still need a safe place to write generated files, temporary artifacts, or caches outside the primary working directory.
For each codex exec run, Codex applies a writable-path allowlist that limits where file changes are permitted. Adding a directory with --add-dir extends that allowlist for the current run, allowing writes only under the specified path.
The extra directory must exist before starting Codex and should be scoped as narrowly as possible, preferably to a dedicated scratch folder. Granting write access to broad locations such as /home or /etc increases the risk of unintended edits and makes cleanup harder if an automation step misbehaves.
Related: How to set Codex sandbox mode
Related: How to set Codex working directory
Steps to add a writable directory for Codex:
- Create a dedicated scratch directory for Codex output.
$ mkdir -p /tmp/codex-writable
No output indicates the directory already existed or was created successfully.
- Restrict access to the scratch directory.
$ chmod 700 /tmp/codex-writable
Restrictive permissions reduce accidental access to temporary artifacts created during automation.
- Run Codex with an additional writable directory.
$ codex exec --add-dir /tmp/codex-writable "Return OK." OK.
Avoid adding broad paths (for example /tmp, /home, /etc, or a repository root), since automation can overwrite unrelated files under the writable tree.
- Create a marker file in the added directory to confirm write access.
$ codex exec -s workspace-write --add-dir /tmp/codex-writable "Create /tmp/codex-writable/codex-marker." Created `/tmp/codex-writable/codex-marker`.
If a trust or permission error occurs, the path is not writable for the current run.
- Remove the marker file after verification.
$ rm -f /tmp/codex-writable/codex-marker
Mohd Shakir Zakaria is a cloud architect with deep roots in software development and open-source advocacy. Certified in AWS, Red Hat, VMware, ITIL, and Linux, he specializes in designing and managing robust cloud and on-premises infrastructures.
