# SSH and VS Code

Use your own `ssh`, `scp`, `rsync`, port forwards and VS Code Remote-SSH with a kosa8 Cloud sandbox. Nothing listens on port 22: SSH is carried over the sandbox's [terminal WebSocket](https://docs.kosa8.com/guides/cloud-terminal-files/), which your API key already authenticates.

## Connect

```
kosa8 cloud ssh sbx_…                              # a shell
kosa8 cloud ssh sbx_… -- -L 3000:localhost:3000    # and forward a port
kosa8 cloud ssh sbx_… -- uname -a                  # run one command
```

Everything after `--` goes to `ssh`. If this machine has no `ssh` client, `kosa8 cloud ssh` opens the plain terminal instead (`kosa8 cloud terminal`).

## ssh kosa8-ID, scp and VS Code

```
kosa8 cloud ssh-config --write     # adds a block to ~/.ssh/config (a later --write replaces it)
ssh kosa8-sbx_…
scp ./data.csv kosa8-sbx_…:/workspace/
rsync -a ./src/ kosa8-sbx_…:/workspace/src/
```

Without `--write`, `kosa8 cloud ssh-config` prints the stanza for you to paste. With no ID it matches every `kosa8-sbx_…` host; with IDs it prints one stanza each:

```
Host kosa8-sbx_*
  ProxyCommand /usr/local/bin/kosa8 cloud ssh --stdio %n
  User root
  IdentityFile ~/.kosa8/ssh/id_ed25519
  IdentitiesOnly yes
  HostKeyAlias %n
  UserKnownHostsFile ~/.kosa8/ssh/known_hosts
  StrictHostKeyChecking accept-new
  ServerAliveInterval 30
```

**VS Code:** install the Remote - SSH extension, run `kosa8 cloud ssh-config --write`, then *Remote-SSH: Connect to Host…* and type `kosa8-sbx_…`. Open `/workspace`. VS Code downloads its server into the sandbox, so the sandbox needs egress to `update.code.visualstudio.com`; if your [egress rules](https://docs.kosa8.com/guides/cloud-egress/) block it, set `"remote.SSH.localServerDownload": "always"` so VS Code downloads it here and copies it over. Cursor and other editors built on VS Code work the same way.

## How it works

1. `kosa8 cloud ssh --stdio ID`, the ProxyCommand, asks the API for a terminal (`POST /v1/sandboxes/{id}/terminal`, scope `sandboxes:write`) running a short setup script instead of a shell. A sleeping sandbox is woken first.

2. The script puts the terminal in raw mode (`stty raw -echo`), so every byte passes unchanged. It finds `sshd`, installing `openssh-server` with the image's package manager if there is none. It adds this machine's kosa8 key to `~/.ssh/authorized_keys`, then runs `sshd -i`, which speaks SSH on the terminal itself. Password login is off.

3. The CLI sends nothing until the script says sshd is ready. Before then, what the sandbox prints (package installs, errors) goes to your terminal as diagnostics.

- **Your key** is kosa8's own, `~/.kosa8/ssh/id_ed25519`, made on first use. Delete the `~/.kosa8/ssh` directory to make a new one.

- **The sandbox's host key** is meant to be kept in `/workspace/.kosa8/ssh`, so it stays the same across connections, sleep and wake, and is checked against `~/.kosa8/ssh/known_hosts`. **Known issue:** on kosa8 Cloud today `/workspace/.kosa8` is a file the host keeps, so the key is made afresh on every connection and the second connection to a sandbox fails with `Host key verification failed`. Until that is fixed, remove the sandbox's line from `~/.kosa8/ssh/known_hosts` before you connect again (the first connection to each sandbox works).

- Each connection is one terminal, recorded in the audit log as `sandbox.terminal_open`. What you type is not recorded.

## Limits

- Like every terminal, each SSH connection runs in its own container from the sandbox's image, with `/workspace` mounted. Work in `/workspace`. In a persistent sandbox, packages installed over SSH outside `/workspace` are not seen by `exec`.

- An image without `sshd` installs it on every connection, which takes a few seconds and needs egress to the image's package mirror. Add `openssh-server` to your image to skip that. The image needs `stty` and `sh`, which nearly every Linux image has.

- You log in as `root`, the user commands run as.

- The terminal URL lasts five minutes, but only until you connect. After that, a session lasts as long as you stay connected.

---
Source: https://docs.kosa8.com/guides/cloud-ssh/
