# Run Claude Code in a VM

> Run Claude Code in a flockvm VM: it is preinstalled on the Agent image. Sign it in, clone your repository and let it work without permission prompts.

- URL: https://flockvm.com/docs/agents/claude-code
- Section: Coding agents
- Last updated: 2026-11-10

A flockvm VM gives Claude Code a machine of its own: `sudo`, Docker, a browser, its own ports, and a kernel that is not your laptop's. On the Agent image, the default, Claude Code is already installed. This guide takes you from a new VM to a Claude Code session that keeps working after you close your terminal.

## Before you start

- A flockvm organization that can create VMs, its [free trial](https://flockvm.com/docs/billing/trial) included, and an SSH key on your laptop. See [Quickstart](https://flockvm.com/docs/get-started/quickstart) and [SSH keys](https://flockvm.com/docs/vms/ssh-keys).
- A way to sign Claude Code in: a Claude plan that includes Claude Code, or an Anthropic API key from the Claude Console.

The Agent image (`ubuntu-26.04-agent`) has Claude Code, Node.js 26, Git, tmux, Docker and Playwright, and a `user` account to run the agent as. Claude Code needs 4 GB of RAM or more: the `small` size (2 vCPU, 4 GB) is the minimum, and `default` (4 vCPU, 8 GB) leaves room for your builds and tests. See [Sizes](https://flockvm.com/docs/vms/sizes) and [Images](https://flockvm.com/docs/vms/images#agent-image).

## Set up the VM

1. **Create a VM**

   In the [dashboard](https://flockvm.com/dashboard), click **New VM**. Pick a **Size**, keep the **Agent** image selected, then give the VM a name such as `claude-api-fix` and choose your SSH key. Click **Launch VM**.

   ![The New VM form](https://flockvm.com/docs/screenshots/new-vm.webp "Pick a size, an image and an SSH key, then Launch VM.")

   The VM goes from **starting** to **running** as soon as SSH answers.

   From a terminal, the [CLI](https://flockvm.com/docs/cli) does the same, with your saved SSH key and the Agent image by default:

   ```bash title="On your laptop"
   flockvm vm create claude-api-fix --size default --wait
   ```

2. **SSH in as user**

   Copy the command from the **Connect** section of the VM page. It looks like this, with your VM's port and host (`HOST` below stands for the host shown there, in every command of this page):

   ```bash
   ssh -p 22001 user@HOST
   ```

   ![The Connect section of a VM page with the SSH command](https://flockvm.com/docs/screenshots/vm-ssh.webp "Each VM has its own SSH port on the host.")

   With the CLI, `flockvm vm ssh claude-api-fix` runs that command for you.

   `user` has passwordless `sudo` and is in the `docker` group. It is not root, so Claude Code accepts `--dangerously-skip-permissions`.

3. **Check Claude Code**

   Nothing to install:

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

   `claude mcp list` shows the `playwright` MCP server, already set up: see [Check pages in a browser](#check-pages-in-a-browser).

### On the Minimal image

The Minimal image (`ubuntu-26.04`) has Node.js 22 but no Claude Code, and you log in as `root`. Create a user for the agent, then install Claude Code:

```bash title="As root, inside a Minimal VM"
useradd -m -s /bin/bash agent
install -d -m 700 -o agent -g agent /home/agent/.ssh
install -m 600 -o agent -g agent /root/.ssh/authorized_keys /home/agent/.ssh/authorized_keys
usermod -aG docker agent
echo 'agent ALL=(ALL) NOPASSWD:ALL' > /etc/sudoers.d/agent   # optional: lets the agent install packages
chmod 440 /etc/sudoers.d/agent
npm install -g @anthropic-ai/claude-code
```

Then SSH in with `ssh -p 22001 agent@HOST`, and read `agent` wherever this page says `user`.

## Sign Claude Code in

Log in as `user`:

```bash
ssh -p 22001 user@HOST
```

Pick one of two ways. Both sign Claude Code in from inside the VM, and both leave a credential there, readable by anything the agent runs: use one you can revoke. Don't copy a token from your laptop into the VM.

**Log in inside the VM:**

For a Claude plan (Pro, Max, Ultra, Team or Enterprise) or a Claude Console account. Start `claude` in the VM and run `/login` if it does not ask you to log in on its own:

```bash title="As user, inside the VM"
claude
```

The VM has no browser: press `c` to copy the login URL, open it in your laptop's browser and sign in. If the browser then shows a code, paste it into the terminal at the `Paste code here if prompted` prompt. Claude Code shows `Login successful`.

The login is saved in `~/.claude/.credentials.json` (mode `0600`). Run `/logout` in the VM when you are done with it.

**Organization API key:**

For an organization billed through the Claude Console. Create a key there, ideally in a workspace with a spend limit, and keep it for VMs. Then, in the VM:

```bash title="As user, inside the VM"
install -m 600 /dev/null ~/.claude.env
vi ~/.claude.env     # add one line: export ANTHROPIC_API_KEY=sk-ant-...
echo '[ -f ~/.claude.env ] && . ~/.claude.env' >> ~/.bashrc
. ~/.claude.env
```

Usage is billed per token by Anthropic. In an interactive session, Claude Code asks once whether to use the key; `claude -p` always uses it.

To skip this step in every new VM, turn on **Agent credentials** in your organization's [environment](https://flockvm.com/docs/vms/environments#agent-credentials): new VMs then get `ANTHROPIC_API_KEY` for `root` at first boot.

> **Tip: Keep keys out of your shell history.** Typing `export ANTHROPIC_API_KEY=…` or `echo sk-ant-… >> file` on the command line saves the key in `~/.bash_history`. Pasting it into `~/.claude.env` with an editor avoids that.

## Clone your repository

Give the VM its own credential for Git rather than your personal SSH key. A deploy key per repository is the narrowest:

```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 the printed key to your repository on GitHub (**Settings → Deploy keys**, with write access if the agent should push), then clone:

```bash title="As user, inside the VM"
git clone git@github.com:your-org/your-repo.git
cd your-repo
git config user.name "Claude (flockvm)"
git config user.email "you@example.com"
```

> **Warning: Avoid ssh -A.** Forwarding your laptop's SSH agent (`ssh -A`) lets anything in the VM use all of your keys while you are connected. A deploy key limits the agent to one repository, and you can delete it when you delete the VM.

## Run without permission prompts

`--dangerously-skip-permissions` lets Claude Code run every command without asking. On your laptop, a bad command can reach your home directory. In a flockvm VM, the worst case is a broken VM you delete and recreate. What it does not limit: the VM has outbound Internet access, so the agent can still send out what it can read, including the credential you gave it. More in [running --dangerously-skip-permissions safely](https://flockvm.com/learn/dangerously-skip-permissions-safely).

Start it inside tmux, so the session survives a dropped connection:

```bash title="As user, inside the VM"
tmux new -s claude
cd ~/your-repo
claude --dangerously-skip-permissions
```

Detach with `Ctrl-b` then `d`. Claude Code keeps working. Reattach later with:

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

Inside tmux, Claude Code uses `Ctrl-b` too: press it twice to send it to Claude Code. The tmux settings for Shift+Enter, notifications and scrolling are in [Claude Code with tmux](https://flockvm.com/learn/claude-code-tmux).

### One-shot tasks

For a task you don't need to watch, run Claude Code in print mode and log the output. `claude -p` skips the folder trust dialog:

```bash title="As user, inside the VM"
cd ~/your-repo
tmux new -d -s task 'claude -p --dangerously-skip-permissions "Fix the failing tests in src/api, then commit on a new branch" 2>&1 | tee ~/task.log'
tail -f ~/task.log
```

More patterns for long sessions in [Long-running sessions with tmux](https://flockvm.com/docs/agents/tmux). To run several Claude Code agents at once, and how many your plan and machine can take, see [how to run Claude Code agents in parallel](https://flockvm.com/learn/run-claude-code-agents-in-parallel).

## Check pages in a browser

On the Agent image, Claude Code has an MCP server named `playwright`, set up in `~/.claude.json`. It drives a headless Chromium in the VM, so Claude Code can open the app it is working on, click through it and take screenshots. Ask for it in the prompt, for example "start the dev server, open <http://localhost:3000> with the playwright tools and check the login page before and after your change".

For screenshots from the command line, or to compare two of them, see [Screenshots with Playwright](https://flockvm.com/docs/vms/images#screenshots-with-playwright).

## Let Claude Code create its own VMs

The steps above run Claude Code inside a VM. The other way round, Claude Code on your laptop can manage flockvm VMs through the [flockvm MCP server](https://flockvm.com/docs/mcp): create a VM for a task, give you its SSH command, pause it, delete it.

Give Claude Code a dedicated API key with an expiry and the **Create and use VMs** preset, not the one you log in with: the key lets it create billed VMs, and with that preset delete only the VMs it created. See [API keys and scopes](https://flockvm.com/docs/api/api-keys). Create it in [Settings → API keys](https://flockvm.com/dashboard/settings/api-keys), then:

```bash title="On your laptop"
npm install -g flockvm
read -rs FLOCKVM_API_KEY && export FLOCKVM_API_KEY   # paste the dedicated key, hidden, kept out of shell history
flockvm mcp add claude
```

This writes the key into Claude Code's private configuration (`~/.claude.json`), for this project (`local` scope, the default); add `--scope user` for every project. `--scope project` writes the shared `.mcp.json`, which is usually committed, so it needs `--key-from-env`: the file then holds `Bearer ${FLOCKVM_API_KEY}`, never the key, and Claude Code reads the key from `FLOCKVM_API_KEY` when it starts.

```bash title="On your laptop"
flockvm mcp add claude --scope project --key-from-env
```

Then ask it, for example, "create a small flockvm VM named review-pr-42 with my saved SSH key and give me its SSH command". See [Connect a client](https://flockvm.com/docs/mcp/clients#claude-code) and [Security](https://flockvm.com/docs/mcp/overview#security).

## Keep Claude Code up to date

On the Agent image, Claude Code is installed system-wide, owned by root, at the version of the image's build, and its auto-updater is off. Update it with npm:

```bash title="As user, inside the VM"
sudo npm i -g @anthropic-ai/claude-code@latest
claude --version
```

## Get the work out

Push the agent's branch, or copy files back to your laptop:

**git:**

```bash title="git"
# inside the VM
git push -u origin HEAD
```

**scp:**

```bash title="scp"
# on your laptop
scp -P 22001 user@HOST:your-repo/report.md .
```

**rsync:**

```bash title="rsync"
# on your laptop
rsync -az -e 'ssh -p 22001' user@HOST:your-repo/dist/ ./dist/
```

Note the capital `-P` for the port with `scp`.

## Pause when it waits for you

A VM bills by the second while it runs. When the agent is done or waiting for your review, click **Pause** on the VM page: the VM's memory is written to disk, the RAM is freed and billing stops. **Resume** brings back the same processes, so your tmux session and Claude Code are where you left them.

![The VM actions: Pause, Stop, Delete](https://flockvm.com/docs/screenshots/vm-actions.webp "Pause keeps memory and processes. Stop keeps the disk only.")

With the CLI: `flockvm vm pause claude-api-fix`, then `flockvm vm resume claude-api-fix`.

When you no longer need the VM, **Delete** it: the disk and a paused VM's saved memory go too, and so does the credential stored in it. If the agent ran untrusted code, delete its API key in the Claude Console.

| Size                     | Per hour | A 2-hour session |
| ------------------------ | -------- | ---------------- |
| `small` (2 vCPU, 4 GB)   | $0.016   | $0.032           |
| `default` (4 vCPU, 8 GB) | $0.032   | $0.064           |
| `large` (8 vCPU, 16 GB)  | $0.064   | $0.128           |

Paused and stopped VMs are not billed for now. Model tokens are a separate bill, paid to Anthropic. See [Pricing](https://flockvm.com/docs/billing/pricing).

## Troubleshooting

**Claude Code refuses to start as root**

You are logged in as `root`. SSH in as `user` on the Agent image (`ssh -p 22001 user@HOST`, with your VM's port), or as the user you created on Minimal, and start `claude` again.

**claude: command not found**

On the Minimal image, Claude Code is not installed: see [On the Minimal image](#on-the-minimal-image). The native installer installs Claude Code for the user who runs it, so running it as root does not give `agent` a `claude` command: install with `npm install -g @anthropic-ai/claude-code` as root, or run the native installer as `agent`.

**Claude Code does not update itself**

On the Agent image its auto-updater is off, because the install belongs to root. Run `sudo npm i -g @anthropic-ai/claude-code@latest`.

**Claude Code stopped at the trust dialog**

Each new directory asks once whether you trust it. Attach to the tmux session and answer it, or use `claude -p`, which skips it.

**git clone asks for a password or says Permission denied (publickey)**

The deploy key is missing on the repository, or it is read-only and the agent tried to push. Check **Settings → Deploy keys** on GitHub.

## Next steps

- [Parallel agents](https://flockvm.com/docs/agents/parallel-agents): One VM per agent, fanned out over SSH.
- [Long-running sessions](https://flockvm.com/docs/agents/tmux): tmux, logs, and what survives a pause.
- [Claude Code in a VM](https://flockvm.com/integrations/claude-code): Why a VM, other routes, and what happens to credentials.
- [Lifecycle](https://flockvm.com/docs/vms/lifecycle): Start, stop, pause, resume and delete.
