> ## Documentation Index
> Fetch the complete documentation index at: https://docs.archil.com/llms.txt
> Use this file to discover all available pages before exploring further.

# SSH and port forwarding

> SSH into sandboxes, copy files with scp and rsync, connect your editor, and reach private ports from localhost.

The [`archil` CLI](https://docs.archil.com/reference/archil-npm-cli) connects your machine to a sandbox without exposing anything to the
internet. SSH is real OpenSSH, so `scp`, `rsync`, port forwarding, and editor remotes like VS Code and Cursor all work.
Private ports can be served on localhost for your browser or local tools.

## SSH

```bash theme={null}
npx archil sandbox ssh dev
npx archil sandbox ssh dev -- uname -a                     # run one command
npx archil sandbox ssh dev -- -L 5432:localhost:5432       # any ssh option
```

Put `--` before arguments that start with a dash, so they go to `ssh`.

The connection goes through the Archil API instead of an open port. ssh's `ProxyCommand` runs
`archil sandbox ssh-proxy`, which:

1. resumes the sandbox if it's paused, or starts it if it's stopped;
2. installs `openssh-server` the first time if the image doesn't include it (with `apt`, `apk`, or `dnf`);
3. authorizes the CLI's own key, `~/.ssh/archil_ed25519`, for `root`;
4. records the sandbox's host key, fetched over the API, so you never see a trust-on-first-use prompt;
5. runs `sshd` and relays the session over the sandbox's process API.

### scp, rsync, and editors

Add the `*.archil` hosts to your SSH config once:

```bash theme={null}
npx archil sandbox ssh-config --install
```

This writes `~/.ssh/archil_config` and includes it from the top of `~/.ssh/config`. After that, any sandbox is
reachable as `<name>.archil`:

```bash theme={null}
ssh dev.archil
scp ./data.csv dev.archil:/root/
rsync -a ./src/ dev.archil:/root/src/
```

In VS Code or Cursor, run **Remote-SSH: Connect to Host...** and enter `dev.archil`.

> [!NOTE]
>
> The config runs the CLI by its full path, so install it globally first (`npm install -g archil`) rather than
> through `npx`. Sign in with `archil login` so the config can use your saved profile. An API key that only exists
> in `ARCHIL_API_KEY` is never written to disk, and editors usually don't have that variable.

`rsync` must also be installed in the sandbox. [Desktops](https://docs.archil.com/compute/sandboxes/desktops.md) include it.

## Forward a port to localhost

`archil sandbox forward` serves a sandbox port on `127.0.0.1` until you press Ctrl-C:

```bash theme={null}
npx archil sandbox forward dev 3000                       # http://127.0.0.1:3000
npx archil sandbox forward dev 8888 --local-port 9999 --open
```

The port stays private. The forward authenticates with a short-lived
[port token](https://docs.archil.com/compute/sandboxes/introduction.md#hosting-network-services) that it renews automatically, and it keeps the
sandbox awake while it runs. It only answers requests addressed to `127.0.0.1` or `localhost`, so web pages can't
reach the forward through DNS rebinding. Forwarding carries HTTP and WebSocket traffic. For other protocols, such as a
database, use `ssh -L`.

From code, use `forwardPort` from `disk/tunnel` (Node.js only):

```typescript theme={null}
import { forwardPort } from "disk/tunnel";

const app = await forwardPort(sandbox, 3000);
const response = await fetch(`${app.url}/health`);
await app.close(); // stops listening, ends the keepalive, revokes the token
```

To make a port reachable from anywhere instead, [expose it](https://docs.archil.com/compute/sandboxes/introduction.md#hosting-network-services).


This documentation is built and hosted on [Mintlify](https://mintlify.com), a developer documentation platform.