# Connect with SSH

SSH lets you control your server from a terminal. You need a key pair: a private key that stays on your computer, and a public key that you add to the console.

## 1. Create a key

On macOS or Linux, open Terminal. On Windows 10 or later, open PowerShell, where `ssh` is built in. Run:

```bash
ssh-keygen -t ed25519 -C "you@example.com"
```

Press Enter to accept the default location and set a passphrase. Show the public key:

```bash
cat ~/.ssh/id_ed25519.pub
```

On Windows PowerShell, use `Get-Content $env:USERPROFILE\.ssh\id_ed25519.pub`.

## 2. Add the public key

1. Go to **SSH keys** in the console and select **Add key**.
2. Enter a name, for example "Work laptop".
3. Paste the whole line. It starts with `ssh-ed25519`, `ssh-rsa` or `ecdsa-sha2`.
4. Select **Add key**.

Never paste the private key (the file without `.pub`). The form refuses it. It also refuses keys that are too weak. Use an Ed25519 key.

To change the name later, use the tag button on the key's row (or `apidly ssh-keys rename ID NAME`). Only the name changes; servers that already carry the key are not touched. Members rename only their own keys.

## 3. Choose the key when you deploy

Select the key on the **Configure** step. A key added after a server exists does not change that server. If you delete a key, servers that are already running keep it until they are restarted or redeployed.

## 4. Connect

Open the server, then the **Connect** tab, and select **Copy command**. It looks like this:

```bash
ssh root@HOST -p PORT
```

The tab also shows the host, port and user (`root`). If your private key is not in the default place, add `-i ~/.ssh/your_key`.

To connect with `ssh my-gpu` instead of a host and port, let the CLI write your servers into `~/.ssh/config`. See [Connect with plain ssh](/docs/tutorial-ssh-config).

### From VS Code

1. Install the **Remote - SSH** extension.
2. Choose **Open SSH Configuration File** and add the entry that the **Connect** tab shows (host, port, user and key path).
3. Pick the host in the Remote Explorer.

## What you should see

A shell prompt on your server. Check the GPU with `nvidia-smi`.

## Common problems

See [Troubleshooting](/docs/troubleshooting) for "Permission denied", timeouts and "host key changed". To copy files, see [Moving files](/docs/moving-files).

## File tools on a stopped server

Open a stopped server and select the **Files** tab to look at its disk without starting it. Three actions are available: list a folder (`ls`), see the size of a folder (`du`) and delete a path (`rm`). This is not a terminal and no other commands run there. To run commands on a running server, use SSH. While the server runs, the tab only explains this and shows the SSH command to copy.

- Paths must be absolute, such as `/workspace/data`, without spaces or quotes. Deleting asks you to type the path back and cannot be undone. `/` and top-level folders cannot be deleted.
- Each action waits up to 60 seconds for the output, and you can stop waiting at any time. Output is cut to its last 64 KB. Secret values and platform details are removed from it. Results stay in the tab until you leave the page and can be downloaded.
- You can run 20 actions per minute. Owners, admins and the member who launched the server can use it.
- The activity log keeps the first 200 characters of each command and who ran it, never the output.

The same is available through the API: `POST /api/v1/servers/{id}/exec` with `{ "command": "ls -la /workspace" }` and a key with the `servers` scope. A running server answers `409 exec_needs_stopped`.

**Commands on a running server.** `apidly exec ID -- COMMAND [ARGS...]` checks the server first. While it runs, the CLI starts your own local `ssh` with the connection details from the API (add `-i KEYFILE` for a specific key, `--print` to only print the command) and runs the command as you, with your keys; nothing is executed by our servers. Every argument is quoted for the remote shell, so use `-- sh -c 'cmd1 | cmd2'` for pipes. The exit code of the command becomes the exit code of `apidly exec`. With `--json` the output is captured and returned as `{ via, exitCode, output, stderr }`. While the server is stopped, `apidly exec` falls back to the API file tools above (`ls`, `du`, `rm` only).

## Recreate the container

In **Settings** on the server, **Recreate container** recreates the server from a freshly pulled copy of the same image, with the same variables and start script, on the same host and GPU. **It erases every file on the server, including everything in `/workspace`**, and cannot be undone. Attached [volumes](/docs/storage-and-data) and cloud copies are not affected, so copy off what you need first. The SSH and app ports change: connect again with the new details on the server page. A hard reboot, by contrast, keeps the files. The image tag, variables, start script, disk and GPU of a running server cannot be changed in place; deploy a new server for that.
