# SSH keys for outbound Git

> For the complete documentation index, see [llms.txt](https://docs.ringleader.dev/llms.txt).


> An SSHKey lets programs in a workstation sign with a private key that stays on your computer.


An SSHKey lets programs in a workstation sign with a private key that stays on your computer.
Git over SSH then works with no key on the workstation. A `git clone` of a private repository, a
service, or a shell on the workstation authenticates with your key. HTTPS with credential injection is the safer choice for Git, and the
next section says why.

## Cost of an SSH key

For Git, we recommend HTTPS with a key that Ringleader adds to each request. Use either the
GitHub preset with a token in a Secret, as
[Inject a key on a cloud workstation](https://docs.ringleader.dev/security/credentials/injected-credentials/#inject-a-key-on-a-cloud-workstation)
shows, or a short-lived token from a credential provider, as
[Connect workstations to GitHub with the Ringleader GitHub App](https://docs.ringleader.dev/guides/connect-github-app/)
shows.

<div class="callout callout-warning">
  <p class="callout-title">Every program can sign</p>
  <div class="callout-body">
    While the key is forwarded, every program in the workstation can sign with it. An injected
credential never enters the workstation.
  </div>
</div>


An SSHKey is the convenient option, with two more costs:

- An SSHKey with no selector is forwarded to every workstation you own.
- The key works only while your computer is connected to the workstation. An agent working
  overnight on a cloud workstation loses it once your laptop sleeps.

## Keys for outbound connections

An SSHKey is about the keys a workstation uses to reach other systems, such as GitHub or a
package registry. Who may SSH into the workstation is a separate rule: ownership plus
[Grants](https://docs.ringleader.dev/reference/resources/grant/).

A share gives a person SSH access, which covers `rl shell`, `rl tmux` and `ssh`. It doesn't
forward their own SSH keys into the workstation, unless the SSHKey on their computer names the
workstation's owner in `selector.matchOwners`, as
[Selection](https://docs.ringleader.dev/reference/resources/sshkey/#selection) describes.

## Forwarded SSH agent

An SSHKey names the path of a private key on your computer, and an optional label selector.
The Ringleader background service forwards every matching key into the workstation as an SSH
agent that it holds in its own memory. The agent is available over the service's connection at
a fixed socket on the workstation.

The private keys live only in the background service's memory, or, for a key with
`source: agent`, in your own `ssh-agent`. Ringleader never writes them to the workstation's
disk, and the agent socket works only while the service holds its connection to the
workstation.

## SSH agent in every session

Ringleader sets `SSH_AUTH_SOCK` for every SSH session at the level of the workstation's `sshd`,
before `sshd` starts your shell. So the forwarded keys reach every session, whatever login
shell the workstation uses. That includes an interactive `rl shell`, a remote command, a
script, a coding agent on the workstation, and `scp` and `rsync`. A command such as
`rl shell my-git-box -- git -C /home/dev/repo pull` signs with your key, and no login
shell is needed.

Setting the variable this way is best effort. It needs the workstation to grant Ringleader's
delivery `sudo` rule, and an `sshd` whose configuration includes `/etc/ssh/sshd_config.d/`.
Both hold on every image Ringleader ships, so check only on a custom image or a login identity
without that rule.

`rl troubleshoot my-git-box` answers it in two places:

- An `ambient agent` line in the controller view names which shells lost the agent and which
  precondition stopped the delivery. It appears only when something is missing. A workstation
  that your background service has never connected to also shows no such line, so its absence
  doesn't prove the delivery worked.
- An `ssh-agent-env` probe under the system probes confirms the working case. It reads
  `SSH_AUTH_SOCK` and both files over the same non-login, non-interactive session that your
  commands use, and reports whether `sshd` reads `/etc/ssh/sshd_config.d/` at all:

  ```text
    ssh-agent-env :
        SSH_AUTH_SOCK=/home/dev/.ringleader/ssh-auth-sock
        profile.d fragment: present
        sshd drop-in: present
        sshd Include sshd_config.d: yes
  ```

For a yes or no, `rl shell my-git-box -- printenv SSH_AUTH_SOCK` shows the variable
directly.

## Selection of workstations

[Selection](https://docs.ringleader.dev/reference/resources/sshkey/#selection) says how a selector matches. A
[node-bound workstation](https://docs.ringleader.dev/workstations/basics/#node-bound-workstations)
keeps the labels that your computer's SSH keys select it by in `spec.hostAccess.labels`.

## Host keys for outbound SSH

Forwarding a key doesn't make the workstation trust the Git server. For that, see
[Host keys for outbound SSH](https://docs.ringleader.dev/workstations/packages-tools-settings/code-dotfiles-trusted-folders/#host-keys-for-outbound-ssh).

## Forward your SSH key

To forward a key into every `tier: dev` workstation, apply an SSHKey with a selector:

```yaml
apiVersion: workstations.ringleader.dev/v1
kind: SSHKey
metadata:
  name: github
  namespace: local
spec:
  path: ~/.ssh/id_ed25519
  selector:
    matchLabels:
      tier: dev
```

Or create the same SSHKey from the command line:

```bash
rl sshkey create github --path ~/.ssh/id_ed25519
```

Then clone from the workstation's shell:

```bash
git clone git@github.com:your-org/your-app.git
```

To clone from a server the workstation doesn't trust yet, add its host key first, as
[Trust a Git server's host key](https://docs.ringleader.dev/workstations/packages-tools-settings/code-dotfiles-trusted-folders/#trust-a-git-servers-host-key)
shows.

## Keep the key in your ssh-agent

By default, the background service reads the private key file and holds the key, so the file
must be readable without a passphrase. For a key with a passphrase, or one on a hardware
token, set `source: agent`. The key then stays in the `ssh-agent` on your computer, and the
service passes each signing request from the workstation to it:

```yaml
spec:
  path: ~/.ssh/id_ed25519_sk
  source: agent
```

Or from the command line:

```bash
rl sshkey create github --path ~/.ssh/id_ed25519_sk --source agent
```

With `source: agent`, `path` still names the private key, and the background service reads
`<path>.pub` to know which agent key to use. Keep `<path>.pub` next to the private key. The
service skips an agent key whose `<path>.pub` it can't read.

Load the key into your agent with `ssh-add`, or a workstation can't use it. The service
reaches your agent through the `SSH_AUTH_SOCK` it started with, so a service started without
one can't use a `source: agent` key. When that leaves a workstation with no key,
`rl troubleshoot` says so. Restart the service from a shell that has an agent, or use
`source: file`.

## Next steps

- [SSHKey](https://docs.ringleader.dev/reference/resources/sshkey/) lists the fields, the `rl sshkey` commands and
  the flags of `create`.
- [Host keys for outbound SSH](https://docs.ringleader.dev/workstations/packages-tools-settings/code-dotfiles-trusted-folders/#host-keys-for-outbound-ssh)
  makes the workstation trust a Git server.
- [Give a workstation access to GitHub](https://docs.ringleader.dev/guides/github-access/)
  gives the safer way to reach GitHub, with a token that no program on the workstation can read.

