Files
ShellBook/templates/workspace/notebooks/ssh-key.md
T
Florian Schmidt 91ac1df8a5 feat(templates): add starter runbook workspace (#5)
Document the notebook format and provide SSH-key, hardening-check, and package-install examples with inventory and credential exclusions.
2026-11-16 00:04:45 +01:00

57 lines
2.0 KiB
Markdown

---
title: New server SSH key
params:
host: { type: host, required: true, label: Target server }
user: { type: name, required: true, default: admin, label: Login user }
keyname: { type: name, default: "id_ed25519_{{host}}" }
---
# New server SSH key
Creates a key pair for **{{user}}** on **{{host}}** and authorizes it.
The host needs OpenSSH and passwordless sudo. The private key stays local to
ShellBook until downloaded; only the public key is uploaded.
```sh {check host="{{host}}" id=user-exists title="User exists"}
id {{user}}
```
```sh {check host="{{host}}" id=password-login-disabled title="Password login disabled" sudo}
sshd -T | grep -qx 'passwordauthentication no'
```
```sh {run on=local id=keygen title="Generate key pair" artifacts="{{keyname}},{{keyname}}.pub"}
ssh-keygen -q -t ed25519 -N '' -C {{user}}@{{host}} -f {{keyname}}
```
Each execution generates a fresh pair in its own workdir. Rerunning this step
intentionally creates a new key; it does not overwrite a key from an earlier run.
```sh {run host="{{host}}" id=authorize title="Authorize key" sudo confirm upload="{{keyname}}.pub:/tmp/shellbook-new-key.pub"}
home=$(getent passwd {{user}} | cut -d: -f6)
test -n "$home"
group=$(id -gn {{user}})
install -d -m 700 -o {{user}} -g "$group" "$home/.ssh"
touch "$home/.ssh/authorized_keys"
key=$(cat /tmp/shellbook-new-key.pub)
if ! grep -qxF -- "$key" "$home/.ssh/authorized_keys"; then
printf '%s\n' "$key" >> "$home/.ssh/authorized_keys"
fi
chown {{user}}:"$group" "$home/.ssh/authorized_keys"
chmod 600 "$home/.ssh/authorized_keys"
rm -f /tmp/shellbook-new-key.pub
```
:::after{step=keygen}
Download the private key once and save it securely:
- **Linux:** `~/.ssh/{{keyname}}`, then `chmod 600 ~/.ssh/{{keyname}}`.
- **Windows:** `C:\Users\<you>\.ssh\{{keyname}}`, accessible only to your user.
Keep the public artifact available for the authorization step. Never commit the private key.
:::
:::after{step=authorize failed}
Inspect the output and confirm the account and sudo access before retrying.
:::