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

# SSH

> Reach a workspace with ssh, sftp, scp and sshfs.

The Mirage server can listen for SSH next to HTTP. The SSH username names the
workspace, every line runs in Mirage's shell, and every file operation goes
through the workspace's mounts.

## Set up

**1. Install the SSH library**

> [tab] Python
>
> ```bash theme={null}
> pip install 'mirage-ai[ssh]'
> ```

> [tab] TypeScript
>
> ```bash theme={null}
> npm install -g ssh2
> ```

**2. Pick a port**

```bash theme={null}
mirage daemon config set ssh_port 2222
```

SSH stays off until a port is set.

**3. Authorize your key**

```bash theme={null}
mkdir -p ~/.mirage/ssh
cat ~/.ssh/id_ed25519.pub >> ~/.mirage/ssh/authorized_keys
```

Only public keys log in, and the file is read on every login.

**4. Restart the server**

```bash theme={null}
mirage daemon restart
mirage workspace create workspace.yaml --id demo
```

> [accordion] Settings
>
> An environment variable wins over `~/.mirage/config.toml`, which wins over the
> default.
>
> | Key | Env var | Default |
> | - | - | - |
> | `ssh_port` | `MIRAGE_SSH_PORT` | unset (off) |
> | `ssh_host` | `MIRAGE_SSH_HOST` | `127.0.0.1` |
> | `ssh_host_key_file` | `MIRAGE_SSH_HOST_KEY_FILE` | `~/.mirage/ssh/host_ed25519_key` |
> | `ssh_authorized_keys` | `MIRAGE_SSH_AUTHORIZED_KEYS` | `~/.mirage/ssh/authorized_keys` |
>
> The server makes its host key on first start and keeps it, so your
> `known_hosts` entry stays valid.

## Connect

| Command | Does |
| - | - |
| `ssh -p 2222 demo@127.0.0.1` | Open a shell in workspace `demo`. |
| `ssh -p 2222 demo@127.0.0.1 'ls /data \| wc -l'` | Run one line. |
| `ssh -T -p 2222 demo@127.0.0.1 < setup.sh` | Run a script. |
| `sftp -P 2222 demo@127.0.0.1` | Transfer files. |
| `scp -P 2222 notes.txt demo@127.0.0.1:/data/` | Copy a file in. |
| `sshfs -p 2222 -o direct_io demo@127.0.0.1:/ ~/mnt/demo` | Show the workspace as a local folder. |

Each run gets a fresh session, so a `cd` or `export` never leaks between them.
Ctrl-C cancels the running line, and `ssh host cmd` exits with the line's status.
To mount a folder on the machine running Mirage instead, use [FUSE](https://docs.mirage.strukto.ai/home/setup/fuse.md).

### Over HTTPS

A hosted server carries SSH over its HTTPS port, so it needs no SSH port or key.
`mirage ssh-proxy` relays it with the CLI's token:

```text ~/.ssh/config theme={null}
Host mirage
  ProxyCommand mirage ssh-proxy %r
```

Then `ssh demo@mirage`, `sftp demo@mirage`, `scp notes.txt demo@mirage:/data/`
and `sshfs -o direct_io demo@mirage:/ ~/mnt/demo` all work. The CLI sends its
token, your [`mirage login`](https://docs.mirage.strukto.ai/home/access/cli#log-in) on a hosted server, and the
login runs under the workspace's default profile.

## Auth (optional)

In `local` and `token` mode, an authorized key opens every workspace under the
workspace's default profile. Options in front of a key in `authorized_keys`
narrow it:

```text ~/.mirage/ssh/authorized_keys theme={null}
mirage-profile="guarded" ssh-ed25519 AAAA... agent-a
mirage-account="alice",mirage-profile="guarded" ssh-ed25519 AAAA... alice-agent
ssh-ed25519 AAAA... me
```

| Option | Does |
| - | - |
| `mirage-profile` | Runs every login of the key under this [session profile](https://docs.mirage.strukto.ai/home/configuration/session.md). The server reads it, so a key cannot pick looser rules. |
| `mirage-account` | Opens only the workspaces this [account](https://docs.mirage.strukto.ai/home/access/http.md#accounts) created. Another account's workspace answers like a missing one. |

On a [`jwt`](https://docs.mirage.strukto.ai/home/access/http#jwt) server, each login reaches only its
account's workspaces:

| Way in | How |
| - | - |
| [Over HTTPS](#over-https) | `mirage login`, then connect through `mirage ssh-proxy`. Your JWT is the account; no key needed. |
| SSH port | The operator adds `mirage-account="you"` to your key. A key without it opens nothing. |

> [accordion] Account rules in detail
>
> * The account holds for shell, exec, `sftp` and `scp` alike.
> * In `local` and `token` mode, a key without `mirage-account` opens every
>   workspace.
> * An option that is empty or given twice opens nothing.

> [accordion] How sessions behave
>
> * Each `ssh`, `sftp` or `scp` run gets a fresh session, closed when it ends.
> * The session's profile and the mount modes apply as in any shell: a
>   read-only mount refuses `sftp put`.
> * Output streams as the line prints it.
> * Ctrl-C cancels the running line and sets `$?` to 130. A dropped connection
>   cancels it too.
> * The server sends a keepalive every 15 seconds and closes a connection that
>   misses three, so a half-open one cancels its line as well.
> * Nothing is forwarded: no ports, no agent, no X11.


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