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

# SSH Setup

> Set up passwordless SSH from this machine to a Lager Box

Install this machine's Lager SSH key on a Lager Box so that `lager` commands and
`lager ssh` work without a password. Run it once per box, enter the box password
when prompted, and subsequent commands authenticate with the key.

This wraps the `ssh-keygen` / `ssh-copy-id` dance into a single command. You can
fix a `Permission denied (publickey,password)` error without the key path and
without the `ssh-copy-id` incantation.

> [!NOTE]
>
> Introduced in **lager 0.27.1** as `lager authorize`; renamed to
> `lager ssh-setup` because the old name read like authentication once
> `lager login` (gateway sign-in) arrived. The old spelling still works
> with a deprecation warning.

## Syntax

```bash theme={null}
lager ssh-setup [OPTIONS]
```

## Options

| Option | Description |
| - | - |
| `--box BOX` | Lager Box name or IP address (uses the default box if omitted) |

***

## Usage

```bash theme={null}
# Authorize a specific Lager Box
lager ssh-setup --box my-lager-box

# Authorize the default Lager Box
lager ssh-setup
```

You are prompted for the box password **once** (by `ssh-copy-id`). After the key
is installed, no further password prompts appear for that box.

***

## How It Works

1. **Resolves** the box name to an IP and looks up its SSH user (see
   [SSH username resolution](https://docs.lagerdata.com/source/reference/cli/ssh.md#username-resolution)).
2. **Generates** the key pair `~/.ssh/lager_box` (and `lager_box.pub`) if it does
   not already exist.
3. **Skips the copy if already authorized** — it asks the box whether the
   `lager_box` public key is already in `~/.ssh/authorized_keys`. It asks with
   `lager_box` first, then with your default keys. If the key is present, it
   goes straight to step 6 (the command is idempotent).
4. **Copies** the public key to the box with `ssh-copy-id -f` (one password prompt).
   If the box accepts only key authentication and one of your keys reaches it,
   the copy uses that key and asks for no password.
5. **Verifies** that the key is now in `~/.ssh/authorized_keys`. It reports an
   error only when the box confirms that the key is absent. If the check cannot
   reach the box, the command continues.
6. **Registers** the public key on the box as
   `/etc/lager/authorized_keys.d/lager-box-<user>-<host>.pub`, over the key it
   just installed, so no extra prompt.

Step 6 is what makes the key durable rather than merely present. `ssh-copy-id`
appends to `~/.ssh/authorized_keys` outside every marker block. `start_box.sh`
preserves such a line against its own rebuild. It cannot preserve that line
against another key manager that rebuilds the file from its own source. Such a
manager drops every line outside its own markers, and `start_box.sh` then
re-creates its block from the key directory alone. A key registered in the key
directory comes back; one that was only appended does not.

The write is attempted unprivileged first, then with `sudo -n`. On a box Lager
alone manages, the key directory is writable by the box user and no sudo is
involved. On a box hardened by another key manager, the directory is root-owned
deliberately. A writable key directory will let any user on the box authorize
any key. The `sudo -n` fallback covers that case wherever the box user has a
NOPASSWD grant broad enough.

If both attempts fail, the command warns and still reports success (the key
works; only its durability is lost) and prints the grant to add. **Lager does
not install this grant.** A fleet that scopes sudo tightly is the system that
made the directory root-owned. That fleet is therefore the right place to decide
who can write there. Add the grant through that fleet's own provisioning:

```
<box-user> ALL=(ALL) NOPASSWD: /usr/bin/tee /etc/lager/authorized_keys.d/lager-box-*.pub
```

A sudoers wildcard does not match `/`, so the grant cannot reach outside the
directory. The grant does not restrict which key is written, because the
content is the box user's to choose. On a box where that user is already
root-equivalent, that changes nothing. On a box where the user is not
root-equivalent, read the key before you add the grant.

> [!WARNING]
>
> Do not widen the key directory's permissions instead. A writable
> `/etc/lager/authorized_keys.d` lets any user on the box authorize any key, which
> is exactly what a hardened box closed.

The key lives at `~/.ssh/lager_box`. That filename is not one of SSH's default
identities. So `lager ssh` passes `-i ~/.ssh/lager_box` explicitly when the key
exists (since lager 0.28.1), and an authorized box connects without a password.
`-i` replaces SSH's default identity list rather than adding to it. So
`lager ssh` also names each of your own default keys that exists, after
`lager_box`. Those keys are `id_rsa`, `id_ecdsa`, `id_ed25519`, and their `-sk`
forms. A box you authorized with your own key therefore still connects, even
when `lager_box` is not authorized there.

***

## Examples

```bash theme={null}
# First-time setup for a new box
lager boxes add --name my-lager-box --ip 100.x.y.z --user <ssh-user>
lager ssh-setup --box my-lager-box

# Re-running against an already-authorized box is safe (no-op)
lager ssh-setup --box my-lager-box
# <ssh-user>@100.x.y.z is already authorized — no password needed.
```

***

## Troubleshooting

| Issue | Cause | Fix |
| - | - | - |
| `ssh-copy-id was not found` | OpenSSH client tools are not installed | Install the OpenSSH client, or append the key manually (see below) |
| `ssh-copy-id ... failed` | Wrong password, or the box rejected the connection | Confirm the box user and password with your admin, then retry |
| Key copied but auth still fails | The box's `sshd_config` may disallow `publickey` auth | Test with `ssh -i ~/.ssh/lager_box <user>@<box>` and check `sshd_config` |
| `ssh-copy-id reported success but the key is not in the box's authorized_keys.` | The copy did not reach `~/.ssh/authorized_keys`, for example because of a full disk or a read-only home directory | Check the disk and the home directory on the box, then append the key manually (see below) |

If `ssh-copy-id` is unavailable, append the public key to the box manually:

```bash theme={null}
cat ~/.ssh/lager_box.pub | ssh <user>@<box> 'mkdir -p ~/.ssh && cat >> ~/.ssh/authorized_keys'
```

***

## See Also

* [SSH](https://docs.lagerdata.com/source/reference/cli/ssh.md) — open an interactive shell on a Lager Box
* [Boxes](https://docs.lagerdata.com/source/reference/cli/boxes.md) — register box names, IPs, and SSH users
* [Setting Up a Lager Box](https://docs.lagerdata.com/source/getting-started/setting-up-a-lager-box.md) — turn an Ubuntu machine into a Lager Box


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