# Remote hosts

> Run sandboxes on another Linux machine, driven over ssh by a key that cannot read their secrets or change their policy.

A remote host is an ordinary msb-manager install on another Linux machine
with KVM. `msbctl` there behaves exactly as it does here, and you drive it over
ssh. Its sandboxes have no folder on your laptop: they are
[cloned from a repo](repo-sandboxes.md), and they keep
running, and start after a reboot, with the laptop switched off.

## What the setup protects against

The threat is code running **as you on the laptop**: an agent run outside any
sandbox, or a compromised dependency during local development. It can read
every file you can, use every key in your ssh-agent, and reuse any open ssh
connection. So the key the laptop uses every day must not be able to read a
token stored on the host, or point one at a host of its choosing, and a plain
shell does both (`cat ~/.config/msb/secrets/global.env`).

That key therefore gets a **forced command**, `msbctl host serve`: it runs
msbctl commands from an allowlist and nothing else, and it runs them with
`MSB_RESTRICTED=1`, so nothing it runs can approve a change to a sandbox's
egress rules or secret bindings, even at a prompt (code on the laptop can
answer a prompt as easily as you can). Policy changes are made on the host
itself, or with a second, admin key that the laptop's agent doesn't hold: a
FIDO2 `sk-` key, which needs a touch.

The restricted key can still *use* what it can reach: `exec` in a sandbox
whose GitHub token can push. That's inherent in a key that manages sandboxes,
and it's bounded by each token's scope, so give each sandbox a fine-grained,
per-repo, expiring token.

## Setting up a host

On the host, as a dedicated user with no sudo and a 0700 home (msb needs
`/dev/kvm` and nothing else):

```console
$ curl -fsSL https://github.com/microsandbox/microsandbox/releases/latest/download/install.sh | sh
$ curl -fsSL https://github.com/runoverlabs/sandbox-manager/releases/latest/download/get.sh | sh
$ msbctl host authorize "ssh-ed25519 AAAA... you@laptop"     # the laptop's everyday key
$ msbctl host authorize "sk-ssh-ed25519@openssh.com AAAA... you@laptop" --admin   # optional
$ msbctl host autostart install
$ msbctl doctor
```

`host authorize` writes the `authorized_keys` line:

```text
restrict,pty,command="/home/msb/.local/bin/msbctl host serve" ssh-ed25519 AAAA... you@laptop
```

`--admin` writes the key without a forced command, and asks first unless it's
an `sk-` key, since a plain key in the laptop's agent is exactly what the
forced command is there to contain.

`host autostart install` writes a systemd user unit that runs
`msbctl start --autostart` at boot. A user unit runs with nobody logged in only
with lingering on, once, as root: `loginctl enable-linger msb`.

`doctor` says whether the host can run sandboxes (KVM, msb, disk, the
registry; see [Commands](commands.md#msbctl-doctor)) and exits 1 when it
can't. Its last part, also available as `host check`, reports what it can see as heuristics: whether the user can
become root, the modes of the home and secrets directories, where secret values
are stored ([file or keyring](credentials.md#keeping-the-values-in-the-keyring)), core dumps, swap,
whether the disk holding the home sits on dm-crypt, whether every
`authorized_keys` line is restricted or an `sk-` key, lingering and the
autostart unit, and the `msb`, `msbctl` and `/dev/kvm` it finds. Full-disk
encryption is a prerequisite msb-manager can't provide; `host check` only says
whether it found one.

## On the laptop

The quickest way is the menu: bare `msbctl` → **remote hosts** → **add a remote
host**, then the ssh destination. Or:

```console
$ msbctl remote add sbx-01 msb@sbx-01.tailnet.ts.net
```

Either way, at a terminal it goes straight on to `msbctl remote setup sbx-01`,
which does the host's half from here while your key still has a shell there.
It looks at the host and asks before each step:

- install msb at the version this machine runs, with that release's own
  installer (without msb here, the latest), and offer to replace a different
  version already there, warning that sandboxes it made may not start under
  another;
- install msbctl at this machine's version (a checkout installs the latest
  release);
- add the user to the `kvm` group if it can't open `/dev/kvm` (with `sudo -n`,
  if that works without a password);
- install autostart, and turn lingering on the same way;
- copy your git identities (without signing keys: a sandbox there has no
  ssh-agent to sign with) and send the Claude tokens you pick, over ssh's stdin.
  Your defaults stay the defaults there. A host starts with neither, and
  without them its sandboxes can't commit or log Claude in;
- restrict one of your keys to `msbctl host serve`. The default is *not now*.
  If the key you pick is the one you log in with, it warns that you lose its
  shell, so keep another way in first: a password, the console, or an admin
  key.

Then it runs `remote test`. Run `remote setup` again whenever you like: it
asks only about what's still missing, and once your key runs `host serve`
it just tests.

Without a terminal, `remote add` records `[remotes.sbx-01]` in your
`config.toml`, prints the `msbctl host authorize` line to run on the host
(listing the keys in your agent and `~/.ssh`), and tests the connection.
`msbctl remote test sbx-01` runs the
host's `doctor` again from here and warns if a check failed there, if your key
isn't the restricted one, or if the two msbctl versions differ. `remote ls` lists them with the last
test's result, `remote rm` forgets one (the host is untouched).

Then `msbctl add` (or the menu's **register a new project**) asks where the
sandbox should run, and on a remote asks for the repo and its name and hands
the rest to the host's own wizard. Or skip the question with `--remote`. A
sandbox there is `sbx-01:name`, for every verb:

```console
$ msbctl add --remote sbx-01 --repo acme/webshop -y
$ pass show github/webshop | msbctl secret set sbx-01:webshop GH_TOKEN --stdin
$ msbctl start sbx-01:webshop
$ msbctl shell sbx-01:webshop
$ msbctl exec sbx-01:webshop -- claude-agent-acp      # from an editor: a clean stdio pipe
$ msbctl ls                                           # every host, with a HOST column
```

Each runs `ssh sbx-01 msbctl <the same command>` with that one name made bare,
and the exit status is the host's (ssh's own failures are 255). ssh gets a tty
only when both your stdin and stdout are one, so `exec` from a pipe stays a
byte stream. One connection per host is kept open for ten minutes
(`ControlMaster`), so the next command doesn't pay for the handshake; a reused
connection can still only run what `host serve` allows. A name without a colon
is local, as before. A laptop that only drives remotes needs no msb and no KVM
at all.

`ls` asks every remote in parallel (batch mode, a five-second connect timeout)
and caches each answer. A host that doesn't answer gets one `unreachable` line,
with its sandboxes from the last answer marked `stale`, and isn't tried again
for a minute, so the picker doesn't wait on it at every reload; `ls --refresh`
tries it now. In `--porcelain` and `--picker` output a remote sandbox is
`host:name`, so the picker's keys work on it unchanged. With no remotes
configured, `ls` is exactly what it was.

The same works with plain ssh, quoting the command as one argument (`host
serve` splits it, the host's shell never sees it):

```console
$ ssh -t msb@host -- 'msbctl shell webshop'
```

### What the restricted key may run

| Command | Allowed |
| --- | --- |
| `ls`, `status`, `show`, `config`, `doctor`, `start` (and `--autostart`), `stop`, `reclaim`, `shell`, `exec`, `update`, `resize`, `rebuild`, `purge`, `rename`, `--version` | yes |
| `self-update` | with no `--version`, or one not older than the running release (an older one is the way back to a weaker `host serve`) |
| `cache` | `ls`, `clear` |
| `observe` | `report`, `off` (`on` turns egress filtering off) |
| `secret` | `ls`, `set`, `rm` (`add` binds a value to a host) |
| `setup` | only `--claude-token NAME --stdin` |
| `add` | only `--repo` (a folder `add` mounts a host path) |
| `policy` | `show` |
| `host` | `check` |
| `allow`, `mount`, `port`, `keys`, `move`, `entry`, `edit`, the menu | no |

Every sandbox must be named: `.` and an omitted name mean the host's current
folder, which means nothing to the caller. `exec` and `shell` pass on only the
`msb exec` flags known to act inside the guest (`-e`, `-w`, `-u`, `-t`,
`--no-tty`, `--no-stdin`, `--timeout`, `--rlimit`, `-q`, `--stream`). A refusal
is one line on stderr, `msbctl host serve: refused: <reason>`, and exit
status 126. A `rebuild` whose repo policy changed refuses too: approve it on
the host with `msbctl policy <name> update`.
