# Run Codex CLI in a VM

> Run OpenAI's Codex CLI in a flockvm VM, preinstalled on the Agent image: sign in without a browser and run it without approvals in a disposable microVM.

- URL: https://flockvm.com/docs/agents/codex
- Section: Coding agents
- Last updated: 2026-11-01

The Codex CLI runs well in a flockvm VM: the VM is the external sandbox OpenAI has in mind for its most permissive mode, and nothing the agent does reaches your laptop. This guide signs it in over SSH and starts a task that keeps running after you disconnect.

## Before you start

- A flockvm VM with the **Agent** image, the default in the **New VM** form. If you don't have one yet, follow the first steps of [Run Claude Code in a VM](https://flockvm.com/docs/agents/claude-code#set-up-the-vm). The [free trial](https://flockvm.com/docs/billing/trial) is enough: `small` and `default` both run Codex.
- A ChatGPT plan that includes Codex, or an OpenAI API key.

The Agent image has Codex, Node.js 26, Git, tmux and Docker, and you log in as `user`, which has passwordless `sudo`. The examples below use port `22001`: use your VM's port, shown in the **Connect** section of its page.

## Check Codex

```bash title="As user, inside the VM"
codex --version
codex mcp list
```

Codex is installed system-wide at the version of the image's build. `codex mcp list` shows the `playwright` MCP server, set up in `~/.codex/config.toml`: Codex can open pages in a headless Chromium and take screenshots. To update Codex, run `sudo npm i -g @openai/codex@latest`.

> **Note: On the Minimal image.** The Minimal image has no Codex. Create a non-root `agent` user as in [the Claude Code guide](https://flockvm.com/docs/agents/claude-code#on-the-minimal-image), install Codex as root with `npm install -g @openai/codex`, then log in as `agent` and read `agent` wherever this page says `user`.

## Sign in without a browser

A VM has no browser. Pick one of three routes; each leaves a credential in the VM that anything the agent runs can read, so prefer one you can revoke.

**API key:**

`codex login --with-api-key` reads the key from standard input. Send it from a file on your laptop, so it never lands in a shell history on either side:

```bash title="On your laptop"
ssh -p 22001 user@HOST 'codex login --with-api-key' < ~/openai-key.txt
```

Usage is billed at OpenAI's API rates. Use a project key with a spend limit, and revoke it when the work is done.

**Device code:**

In the VM, run `codex login` and see whether your Codex version offers a way to sign in from another device: Codex then shows a URL and a code, which you open and enter on your laptop. `codex login --help` lists the options your version has.

```bash title="As user, inside the VM"
codex login --help
```

If your version or your account does not offer it, use one of the other two routes.

**Copy auth.json:**

Sign in on your laptop with `codex login`, then copy the login cache to the VM:

```bash title="On your laptop"
ssh -p 22001 user@HOST 'install -d -m 700 ~/.codex'
scp -P 22001 ~/.codex/auth.json user@HOST:.codex/auth.json
ssh -p 22001 user@HOST 'chmod 600 ~/.codex/auth.json'
```

OpenAI's advice: treat `~/.codex/auth.json` like a password, it contains access tokens.

Ten VMs signed in to one ChatGPT account draw from the same plan allowance.

## Clone your repository

Use a deploy key for the repository, as in the [Claude Code guide](https://flockvm.com/docs/agents/claude-code#clone-your-repository):

```bash title="As user, inside the VM"
ssh-keygen -t ed25519 -N '' -C "flockvm $(hostname)" -f ~/.ssh/id_ed25519
cat ~/.ssh/id_ed25519.pub     # add it under Settings → Deploy keys on GitHub
git clone git@github.com:your-org/your-repo.git
```

## Choose the approval and sandbox mode

Codex has its own sandbox and approval prompts. In a disposable VM, you can turn both off, or keep its sandbox and only drop the prompts:

| Flags                                                         | What Codex does                                                                                                                                     | When to use it in a VM                                                        |
| ------------------------------------------------------------- | --------------------------------------------------------------------------------------------------------------------------------------------------- | ----------------------------------------------------------------------------- |
| `--dangerously-bypass-approvals-and-sandbox` (alias `--yolo`) | Runs every command without approvals and without its sandbox                                                                                        | The VM is the sandbox; the agent needs the network, Docker or system packages |
| `--sandbox workspace-write --ask-for-approval never`          | No prompts, but writes stay inside the project, `.git` and `.codex` stay read-only, and the network stays off unless you enable it in `config.toml` | You want a second layer inside the VM                                         |

OpenAI describes the first flag as intended solely for environments that are externally sandboxed. A flockvm VM is one: a bad command breaks the VM, not your machine. It does not stop the agent from sending out what it can read, since the VM has outbound Internet access. More in [running coding agents with skip-permissions safely](https://flockvm.com/learn/dangerously-skip-permissions-safely).

> **Note:** Codex's flags change between versions. Run `codex --help` and `codex exec --help` in the VM to see the ones your version accepts.

## Run an interactive session

Start Codex inside tmux, so it survives a dropped SSH connection:

```bash title="As user, inside the VM"
tmux new -s codex
cd ~/your-repo
codex --dangerously-bypass-approvals-and-sandbox
```

Detach with `Ctrl-b` then `d`. Reattach from your laptop:

```bash
ssh -t -p 22001 user@HOST tmux attach -t codex
```

## Run a one-shot task

`codex exec` runs one task without the interactive UI. `-o` writes the agent's last message to a file:

```bash title="As user, inside the VM"
cd ~/your-repo
git switch -c codex/pagination
tmux new -d -s task 'codex exec --dangerously-bypass-approvals-and-sandbox -o ~/result.md "Add pagination to GET /projects and make the tests pass" 2>&1 | tee ~/task.log'
```

Follow it with `tail -f ~/task.log`, then read the summary and push:

```bash title="As user, inside the VM"
cat ~/result.md
git push -u origin HEAD
```

`--ephemeral` skips saving the session files to disk, useful for throwaway runs.

## Pause, resume, delete

While you review the result, click **Pause** on the VM page: memory and processes are saved, billing stops. **Resume** brings back the same tmux session. When you are done, **Delete** the VM: its disk goes, and so does the copy of your Codex credential. Revoke the key or sign out the session if the agent ran code you don't trust.

![The VM actions: Pause, Stop, Delete](https://flockvm.com/docs/screenshots/vm-actions.webp "Pause keeps memory and processes; billing stops while the VM is paused.")

A `small` VM costs $0.016 an hour, a `default` one $0.032, billed per second. Model usage counts against your ChatGPT plan or your OpenAI API bill. See [Pricing](https://flockvm.com/docs/billing/pricing).

Rolling Codex out to a team, with a key and config per environment, locked settings and Codex in GitHub Actions on flockvm runners, is the [Codex CLI for enterprise teams playbook](https://flockvm.com/playbooks/codex-cli-enterprise).

## Next steps

- [Parallel agents](https://flockvm.com/docs/agents/parallel-agents): One VM per Codex task, best-of-N, and cleanup.
- [Long-running sessions](https://flockvm.com/docs/agents/tmux): tmux, logging, and what survives a pause.
- [Codex parallel tasks](https://flockvm.com/learn/codex-parallel-tasks): Codex Cloud, local worktrees and one VM per task, compared.
- [SSH](https://flockvm.com/docs/vms/ssh): Connect, copy files, and set up an SSH config.
