create-lxc: replace password flow with passwordless SSH key injection
Always authorize the Proxmox host's root pubkey (generate an ed25519 pair if missing) so `ssh root@<lxc>` from the host works immediately. Extra keys via SSH_PUBKEY (literal or path). Relies on Alpine's default PermitRootLogin=prohibit-password — no sshd_config edits, no password ever set; pct enter stays as fallback.
This commit is contained in:
1 parent
d6e846c7a0
commit
1ab70a6f9a
2 files changed
+41
-40
No files matched your search
@@ -34,19 +34,22 @@ HOSTNAME=tunbox MEMORY=512 STORAGE=fast-nvme \
|
||||
bash -c "$(curl -fsSL https://git.d4nn13.de/d4nn13/wirecon/raw/branch/main/proxmox/create-lxc.sh)"
|
||||
```
|
||||
|
||||
Available overrides: `HOSTNAME`, `CORES`, `MEMORY`, `DISK`, `BRIDGE`, `STORAGE`, `TEMPLATE_STORAGE`, `WIRECON_REPO`, `WIRECON_REF`, `ROOT_PASSWORD`, `WIRECON_WEB_AUTOSTART` (yes/no).
|
||||
Available overrides: `HOSTNAME`, `CORES`, `MEMORY`, `DISK`, `BRIDGE`, `STORAGE`, `TEMPLATE_STORAGE`, `WIRECON_REPO`, `WIRECON_REF`, `SSH_PUBKEY`, `WIRECON_WEB_AUTOSTART` (yes/no).
|
||||
|
||||
The script also asks (interactively, default **yes**) whether to enable the web UI on boot. Edit configs in-browser right after provisioning instead of `vi`'ing over `pct enter`.
|
||||
|
||||
### Root login
|
||||
### Shell access (passwordless, key-only)
|
||||
|
||||
The script prompts for a root password (reads from `/dev/tty`, works under `curl | bash`) and, if one is given, installs `openssh`, flips `PermitRootLogin yes`, and enables `sshd`. You can also preset it:
|
||||
No password is ever set. The script installs `openssh`, enables `sshd`, and authorizes root SSH keys:
|
||||
|
||||
- **Always added:** the Proxmox host's `/root/.ssh/id_ed25519.pub`. The script generates this keypair if it doesn't exist, so `ssh root@<lxc-ip>` from the Proxmox host works immediately.
|
||||
- **Optionally added:** whatever you pass in `SSH_PUBKEY` — either a literal key string or a path to a file containing one or more keys. Example:
|
||||
|
||||
```
|
||||
ROOT_PASSWORD='hunter2' bash -c "$(curl -fsSL …/create-lxc.sh)"
|
||||
SSH_PUBKEY=~/.ssh/id_ed25519.pub bash -c "$(curl -fsSL …/create-lxc.sh)"
|
||||
```
|
||||
|
||||
Leave it empty / skip the prompt and the container has **no root password** — you enter it from the Proxmox host with `pct enter <VMID>`. SSH won't work in that case.
|
||||
Alpine's default `PermitRootLogin prohibit-password` stays in place — root is reachable with a key, password auth is refused. `pct enter <VMID>` from the Proxmox host remains available as a fallback.
|
||||
|
||||
## Finishing up (inside the LXC)
|
||||
|
||||
|
||||
+33
-35
@@ -40,29 +40,27 @@ fi
|
||||
|
||||
log() { printf '[create-lxc] %s\n' "$*"; }
|
||||
|
||||
# --- root password ----------------------------------------------------------
|
||||
# Accept ROOT_PASSWORD from env; otherwise prompt on the controlling TTY
|
||||
# (works even when invoked via `curl … | bash` because we read from /dev/tty).
|
||||
# If neither is available, fall back to the no-password default — `pct enter`
|
||||
# from the Proxmox host is still possible.
|
||||
if [[ -z "${ROOT_PASSWORD:-}" ]]; then
|
||||
if [[ -r /dev/tty && -w /dev/tty ]]; then
|
||||
exec 3</dev/tty
|
||||
printf 'root password for container (empty to skip): ' >/dev/tty
|
||||
IFS= read -rs ROOT_PASSWORD <&3
|
||||
printf '\n' >/dev/tty
|
||||
if [[ -n "$ROOT_PASSWORD" ]]; then
|
||||
printf 'confirm: ' >/dev/tty
|
||||
IFS= read -rs ROOT_PASSWORD_CONFIRM <&3
|
||||
printf '\n' >/dev/tty
|
||||
if [[ "$ROOT_PASSWORD" != "$ROOT_PASSWORD_CONFIRM" ]]; then
|
||||
echo "passwords do not match" >&2
|
||||
exit 1
|
||||
# --- SSH key injection ------------------------------------------------------
|
||||
# Passwordless access. Always authorize the Proxmox host's root key (create
|
||||
# an ed25519 pair if none exists) so `ssh root@<lxc>` from *this* host works
|
||||
# out of the box. Additional keys via SSH_PUBKEY (one key per line, or a
|
||||
# path to a file containing keys).
|
||||
PUBKEYS=""
|
||||
|
||||
HOST_KEY="${HOST_KEY:-/root/.ssh/id_ed25519}"
|
||||
if [[ ! -f "$HOST_KEY" ]]; then
|
||||
log "generating Proxmox root SSH key at $HOST_KEY (none existed)"
|
||||
install -d -m 0700 /root/.ssh
|
||||
ssh-keygen -t ed25519 -N "" -f "$HOST_KEY" -C "root@$(hostname)" >/dev/null
|
||||
fi
|
||||
fi
|
||||
exec 3<&-
|
||||
PUBKEYS="$(cat "${HOST_KEY}.pub")"
|
||||
|
||||
if [[ -n "${SSH_PUBKEY:-}" ]]; then
|
||||
# treat as path if it looks like one, otherwise inline value
|
||||
if [[ -f "$SSH_PUBKEY" ]]; then
|
||||
PUBKEYS+=$'\n'"$(cat "$SSH_PUBKEY")"
|
||||
else
|
||||
log "no ROOT_PASSWORD set and no TTY — root will have no password (pct enter only)"
|
||||
PUBKEYS+=$'\n'"$SSH_PUBKEY"
|
||||
fi
|
||||
fi
|
||||
|
||||
@@ -156,21 +154,20 @@ pct exec "$VMID" -- sh -c 'apk add --no-cache bash curl ca-certificates >/dev/nu
|
||||
log "running bootstrap from $BOOTSTRAP_URL"
|
||||
pct exec "$VMID" -- sh -c "curl -fsSL '$BOOTSTRAP_URL' | WIRECON_REPO='$WIRECON_REPO' WIRECON_REF='$WIRECON_REF' bash"
|
||||
|
||||
# --- optional: set root password + enable sshd -----------------------------
|
||||
if [[ -n "${ROOT_PASSWORD:-}" ]]; then
|
||||
log "setting root password"
|
||||
# Pipe via stdin so the secret never appears in argv / ps.
|
||||
printf 'root:%s\n' "$ROOT_PASSWORD" | pct exec "$VMID" -- chpasswd
|
||||
|
||||
log "installing + enabling sshd (PermitRootLogin yes)"
|
||||
pct exec "$VMID" -- sh -c '
|
||||
# --- install sshd + authorize keys (passwordless) ---------------------------
|
||||
log "installing sshd + authorizing $(printf '%s\n' "$PUBKEYS" | grep -c '^ssh-') key(s)"
|
||||
# Alpine's default sshd_config is `PermitRootLogin prohibit-password` which
|
||||
# allows root login with keys and rejects passwords — exactly what we want.
|
||||
# No sshd_config edits needed.
|
||||
printf '%s\n' "$PUBKEYS" | pct exec "$VMID" -- sh -c '
|
||||
apk add --no-cache openssh >/dev/null
|
||||
sed -i "s/^#\?PermitRootLogin.*/PermitRootLogin yes/" /etc/ssh/sshd_config
|
||||
grep -q "^PermitRootLogin yes" /etc/ssh/sshd_config || echo "PermitRootLogin yes" >> /etc/ssh/sshd_config
|
||||
rc-update add sshd default
|
||||
rc-service sshd restart
|
||||
install -d -m 0700 /root/.ssh
|
||||
cat >> /root/.ssh/authorized_keys
|
||||
sort -u /root/.ssh/authorized_keys -o /root/.ssh/authorized_keys
|
||||
chmod 0600 /root/.ssh/authorized_keys
|
||||
rc-update add sshd default >/dev/null
|
||||
rc-service sshd restart >/dev/null
|
||||
'
|
||||
fi
|
||||
|
||||
# --- optional: start web UI -------------------------------------------------
|
||||
if [[ "$WIRECON_WEB_AUTOSTART" == "yes" ]]; then
|
||||
@@ -185,10 +182,11 @@ fi
|
||||
cat <<EOF
|
||||
|
||||
[create-lxc] container $VMID ($HOSTNAME) provisioned.
|
||||
[create-lxc] shell: ssh root@${ip:-<lxc-ip>} (passwordless, key-only)
|
||||
${WEB_URL:+[create-lxc] web UI: $WEB_URL}
|
||||
[create-lxc] finish configuration:
|
||||
- edit /etc/wireguard/wg0.conf and /etc/wirecon/wirecon.conf
|
||||
(via the web UI above, or: pct enter $VMID && vi …)
|
||||
(via the web UI above, or over ssh / pct enter)
|
||||
- then start the tunnel services:
|
||||
rc-update add wg-quick.wg0 default && rc-service wg-quick.wg0 start
|
||||
rc-update add wirecon-watchdog default && rc-service wirecon-watchdog start
|
||||
|
||||
Reference in new issue
Block a user