How to Run an AI Coding Agent in a Sandbox VM

Run Claude Code, Codex or any coding agent in its own Linux VM, with its own kernel, firewall and credentials, so your app and laptop stay untouched.

On this page
  1. Why coding agents need a sandbox
  2. Container or VM: which sandbox to use
  3. Before you start
  4. Step 1: Create the VM
  5. Step 2: Close the ports the agent does not need
  6. Step 3: Connect to the VM
  7. Step 4: Install the agent and your code
  8. Step 5: Give the agent limited access
  9. Step 6: Keep the agent’s work in Git
  10. Summary
  11. Frequently asked questions
  12. Is a VM safer than a Docker container for an AI agent?
  13. Can I leave the agent running when I close my laptop?
  14. Which agents does this work with?
  15. Does the agent get access to my other Hostless apps?

Give your AI coding agent its own virtual machine (VM). The agent can then install packages, run tests and change files without touching your laptop or your app. This guide shows you how to set one up.

Who this is for: developers who use a coding agent, such as Claude Code or Codex, and can connect to a Linux server over SSH.

What it covers: why a VM makes a good sandbox, how to create one, close the ports it does not need, give the agent limited access, and keep its work in Git.

What it does not cover: choosing an agent, or letting an agent deploy to production.

Why coding agents need a sandbox

Coding agents now run commands on your behalf. They install dependencies, edit files and start processes. Trust in that work is still low. In the Stack Overflow 2026 Developer Survey, only 6.6% of developers said they trust AI with important work decisions.

A sandbox lets you use the agent fully while that trust grows. The agent works inside a boundary, and your own machine and your app stay outside it.

Container or VM: which sandbox to use

Containers and VMs both isolate an agent. They draw the boundary in different places.

Container VM
Kernel Shares the host’s kernel Runs its own kernel under hardware virtualization
Setup Fast to start on your laptop Runs on a separate machine you reach over SSH
Network Uses the host’s network rules Has its own firewall rules
Good for Short tasks you watch Long tasks you leave running, and code you did not write

A container is a good choice when you watch the agent work on your laptop. A VM is the stronger choice when the agent runs for hours, or when it runs code you have not read. A VM also keeps running when you close your laptop.

Before you start

You need:

  1. A Hostless Cloud account with access to virtual machines. To get access, use the virtual machines page.
  2. An SSH key on your computer.
  3. The install command for your agent’s command line tool.

This guide uses a Hostless VM. The same steps work on any Linux VM that you can reach over SSH.

Step 1: Create the VM

  1. In the dashboard, open Virtual machines and select Create.
  2. Enter a name, such as agent-sandbox.
  3. Select a region and an image: Ubuntu 22.04 LTS or Debian 12.
  4. Select the smallest size. Most agents need no more.
  5. Add your SSH key.
  6. Select Create.

The Create virtual machine form with a name, region, image, size and SSH key filled in

The VM shows its IP address when it starts.

Step 2: Close the ports the agent does not need

New VMs accept traffic on TCP ports 22, 80 and 443. An agent that edits code and runs tests does not serve web traffic. It needs only port 22 for SSH.

  1. Wait until the VM shows Running, then open its Firewall settings.
  2. Remove the rules for ports 80 and 443.
  3. Limit port 22 to your own IP address.

Firewall rules for the VM, with only port 22 open to one IP address

Step 3: Connect to the VM

Connect as the hostless user. This user can run admin commands with sudo.

ssh hostless@YOUR_VM_IP

You can also use the browser terminal in the dashboard. It stays available to project editors even when port 22 is closed.

Step 4: Install the agent and your code

  1. Update the package list. The Ubuntu image already includes Git.

    sudo apt update
  2. Clone the repository that the agent will work on:

    git clone https://github.com/you/your-repo.git
    cd your-repo

    The VM’s browser terminal after the package update and a git clone

  3. Install your agent’s command line tool, using its own instructions.

  4. Start the agent in the repository folder.

The agent now works only inside this VM.

Step 5: Give the agent limited access

The agent can use any credential you put on the VM. Give it only what its task needs.

  1. Create a token that can read and push to only one repository.
  2. Store the token on the VM.
  3. Keep production environment variables and keys off the VM.

Step 6: Keep the agent’s work in Git

The agent’s work lives in the repository on the VM. Push it to a branch, so you can review it anywhere and start the next session on a clean machine.

  1. Ask the agent to commit its changes to a new branch, such as agent/task-name.

  2. Push the branch with the token from Step 5:

    git push origin agent/task-name
  3. Review the branch and open a pull request from your own computer.

  4. When you want a clean machine, create a new VM and clone the branch there.

Summary

  1. Run the agent on a small VM, away from your laptop and your app’s server.
  2. Close every port except SSH, and limit SSH to your IP address.
  3. Give the agent a token for one repository only.
  4. Push its work to a branch, so you can review it and move to a clean VM at any time.

Frequently asked questions

Is a VM safer than a Docker container for an AI agent?

A VM draws a stronger boundary. It runs its own kernel under hardware virtualization, so code inside it does not share a kernel with your other workloads. A container shares the host’s kernel.

Can I leave the agent running when I close my laptop?

Yes. The agent runs on the VM, so it keeps working after you disconnect. Use a tool such as tmux to keep the session open.

Which agents does this work with?

Any agent with a command line tool that runs on Linux, such as Claude Code, Codex or Aider.

Does the agent get access to my other Hostless apps?

The agent sees only the credentials you put on the VM. Keep tokens for your other apps off it. For the strongest boundary, create the sandbox VM in its own project, away from your apps and databases.