# SSH keys

> Save public keys to your account and choose one for each rental.

Source: https://legion.ambient.xyz/docs/renting/ssh-keys

Each rental has one SSH public key, which can log in as root. You choose it when you rent, and you can change it later.

## Make a key

If you don't have one, run:

```sh
ssh-keygen -t ed25519
```

This writes a private key, `~/.ssh/id_ed25519`, and a public key, `~/.ssh/id_ed25519.pub`. Only the public key ever goes to Legion.

## Save keys to your account

Open **Settings**, then **SSH keys**. Paste one line from a `.pub` file, give it a name and press **Save key**.

- Saved keys are yours, in this organization, in every browser you sign in from. Only you can see them.
- Legion accepts `ssh-ed25519`, `ssh-rsa` (2048 bits or more), `ecdsa-sha2-nistp256`, `ecdsa-sha2-nistp384`, `ecdsa-sha2-nistp521`, `sk-ssh-ed25519@openssh.com` and `sk-ecdsa-sha2-nistp256@openssh.com`.
- A key is one line, up to 8192 characters.
- If you paste a private key by mistake, Legion refuses it before it's sent.

## Choose a key when you rent

The rent form lists your saved keys. None is chosen for you: pick one each time. You can also paste a different key, and save it as you rent.

Removing a saved key from your account doesn't affect rentals that already use it. To take access away from a key, change the rental's key.

## Change or revoke the key of a rental

A rental has one key, and you can replace it while the rental exists, running or stopped. Do it when you lose a laptop, rotate your keys, or when someone who held the private key leaves your organization.

1. Open the rental and go to its **Connect** tab. Under **SSH key** you see the fingerprint of the key it has now.
2. Press **Change SSH key**, then choose one of your saved keys or paste a different public key. You can save a pasted key to your account.
3. Press **Review**. The page shows the current and the new fingerprint and what happens next. Press **Replace SSH key** to confirm.

The rental isn't restarted and your files stay. The SSH gateway accepts only the new key from the host's next sync, at most 30 seconds later, and closes the open SSH sessions that used the old key within about 10 seconds after that: 40 seconds at the most in all. From then on the old private key can't connect. The container's `authorized_keys` file is rewritten in place. [The web terminal](https://legion.ambient.xyz/docs/renting/connecting#the-web-terminal) signs in with a certificate, not the rental's key, so it isn't affected.

Only an owner or a member can change a rental's key. Pasting the same key again changes nothing. A private key is refused, as everywhere else.

A rental that is being deleted, is deleted or has failed can't take a new key. If the rental's host runs an older Legion agent that can't change a key while the container runs, the page says so: stop the rental, change the key, then start it again.

The same change is available through the API: [updateRental](https://legion.ambient.xyz/docs/api/rentals/update-rental) with `ssh_public_key`. The key itself is never sent back, only `ssh_key_fingerprint`, the fingerprint `ssh-keygen -l` prints.
