# Instance access

A terminal on an instance from the console or the CLI, and keys added to a running instance, with port 22 closed.

## How it works

Every instance launched since 2026-11-16 runs **Deutsche Cloud Connect** unless it was launched with
`connect: false`:

- The machine keeps **one outbound SSH connection** to Deutsche Cloud (`ews-connect.service`, the stock
  OpenSSH client). It logs in with a key it made itself at first boot (`/etc/ews/connect/id_ed25519`);
  Deutsche Cloud only ever sees the public half. Nothing new listens on the machine.
- To open a terminal or change keys, Deutsche Cloud goes back through that connection to the machine's
  own `127.0.0.1:22` and logs in to **your sshd** with an **OpenSSH certificate valid for 2 minutes**,
  signed by your organization's user CA. The machine trusts that CA through
  `TrustedUserCAKeys /etc/ssh/ews-user-ca.pub` (in `/etc/ssh/sshd_config.d/50-ews-connect.conf`).
  The certificate only works from the tunnel's end on the machine (`source-address 127.0.0.1,::1`).
- Your security group can keep **port 22 closed**: the connection lands on loopback.
- **Glass box**: the machine's own log shows every login, with the session and the person:

  ```
  sshd[2331]: Accepted publickey for root from 127.0.0.1 port 55166 ssh2: ED25519-CERT SHA256:… ID ews:ses-38dbc7f9a8322c68:jane.doe (serial …) CA ED25519 SHA256:…
  ```

Everything Deutsche Cloud installed is listed at the top of `/usr/local/sbin/ews-connect-install`.

## Open a terminal

Console: **Compute → Instances → your instance → Connect → Open terminal** (as `root`, or as the
image's default user when it has one). Copy with Ctrl+Shift+C (⌘C), paste with Ctrl+Shift+V (⌘V).
If your connection drops, the shell waits 60 s and the page reconnects, replaying the last output.

CLI:

```bash
dcloud ec2 connect i-0123abcd            # as root
dcloud ec2 connect i-0123abcd --user ubuntu
```

Limits: a session ends after 20 minutes without input or output, and after 8 hours in any case.

API: `POST /ec2/api/instances/{id}/sessions {"loginUser":"root"}` answers a single-use `ticket`
(60 s) and `websocketUrl`. Open the WebSocket with the subprotocols `ews.terminal` and
`ews.ticket.<ticket>`; binary frames are the terminal's bytes, text frames
`{"type":"resize","cols":120,"rows":40}`. The server sends `{"type":"exit","code":0}` when the shell
exits.

## Add a key to a running instance

Console: **Connect → Add key**, choose a key pair (Compute → Key pairs), choose `root` and/or the
default user. CLI / API:

```bash
dcloud ec2 instances add-key i-0123abcd --key-pair-id <uuid> [--users root]
dcloud ec2 instances remove-key i-0123abcd <uuid>
dcloud ec2 instances connect-status i-0123abcd    # connected? who can log in? keys it added
```

The line is appended to `~/.ssh/authorized_keys` and ends in `ews-key:<key pair id>`, so removing it
takes out exactly that line and nothing you added yourself. No reboot. To use the key from your own
SSH client, the instance's security group must allow port 22 from your address; the browser
terminal does not need that.

## Run a command

To run one command without a terminal, for example a deploy step from CI, use Run Command. It goes
through the same tunnel and the same 2-minute certificate, and returns the exit code and output:

```bash
dcloud ec2 instances run-command i-0123abcd --command 'systemctl restart app'
```

API: `POST /ec2/api/instances/{id}/commands {"command":"…"}` answers a `commandId`;
`GET /ec2/api/instances/{id}/commands/{commandId}` gives its `status`, `exitCode`, `stdout` and
`stderr`. A command runs at most once. An error answer means it never ran and may be sent again; a
command whose start the machine did not confirm is answered anyway and ends as `lost`, its outcome
unknown. Details, exit codes and a GitHub Actions example:
[Run a command on an instance](/docs/ec2/guide/run-a-command).

## Who may do what (IAM)

| Action                  | What it allows                                                        |
| ----------------------- | --------------------------------------------------------------------- |
| `ec2:StartSession`      | Open a terminal on the instance (every start and denial is audited)   |
| `ec2:AddInstanceKey`    | Add a key pair to the running instance (also `ec2:DescribeKeyPairs`)  |
| `ec2:RemoveInstanceKey` | Remove a key Connect added                                            |
| `ec2:DescribeSessions`  | See the sessions: who, when, as whom, how long, bytes                 |
| `ec2:TerminateSession`  | End someone else's session (ending your own needs nothing)            |
| `ec2:RunCommand`        | Run a command (Run Command), read and cancel the commands you started |
| `ec2:DescribeCommands`  | Read every command run on the instance and its output                 |

All are checked on the instance's resource name, so a policy can allow a terminal on some instances
only. `EC2ReadOnly` includes none of the writes, and not `ec2:DescribeCommands` either: command
output can hold anything the machine printed. `ec2:RunCommand` is in no managed policy except
`AdministratorAccess`, `Ec2FullAccess` included: attach it explicitly.

## What is recorded

For every session: who opened it, from which address, on which instance, as which user, when it
started and ended, how many bytes went each way, and why it ended. **Never what was typed or
shown.** The machine's sshd log has its own record of every login.

## Machines without Connect

| State on the Connect tab | Meaning                                                                   | What to do                                                    |
| ------------------------ | ------------------------------------------------------------------------- | ------------------------------------------------------------- |
| Setting up               | The machine is booting; it connects within about a minute                 | Wait                                                          |
| Not connected            | The machine is down, or someone ran `systemctl disable ews-connect` on it | Start it, or run the enable command on it                     |
| Off                      | Launched with `connect: false`                                            | Run the enable command on it if you want Connect              |
| Not installed            | Launched before 2026-11-16                                                | Run the enable command over your existing access, or relaunch |

The enable command (also shown on the Connect tab), run as root on the machine:

```bash
curl -fsSL https://console.platform.deutschecloud.app/api/instance/v1/connect/install.sh | sudo sh
```

It uses the machine's instance identity, so it works on any instance launched since 2026-11-13. An
older machine has no identity: add keys by hand, or relaunch it. Deutsche Cloud never turns Connect back on by
itself. To remove it completely: `sudo sh /usr/local/sbin/ews-connect-install uninstall`.

Requirements on the machine: systemd, curl, OpenSSH client and server, outbound TCP to the console's
SSH port (2222). `PermitRootLogin no` in your sshd configuration stops root terminals and key
changes (use the default user).
