diff --git a/README.md b/README.md index 8c41677..cd7d3e6 100644 --- a/README.md +++ b/README.md @@ -34,7 +34,17 @@ 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`. +Available overrides: `HOSTNAME`, `CORES`, `MEMORY`, `DISK`, `BRIDGE`, `STORAGE`, `TEMPLATE_STORAGE`, `WIRECON_REPO`, `WIRECON_REF`, `ROOT_PASSWORD`. + +### Root login + +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: + +``` +ROOT_PASSWORD='hunter2' 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 `. SSH won't work in that case. ## Finishing up (inside the LXC) diff --git a/proxmox/create-lxc.sh b/proxmox/create-lxc.sh index ec85367..5e41dad 100755 --- a/proxmox/create-lxc.sh +++ b/proxmox/create-lxc.sh @@ -40,6 +40,32 @@ 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 + 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 + fi + fi + exec 3<&- + else + log "no ROOT_PASSWORD set and no TTY — root will have no password (pct enter only)" + fi +fi + # --- pick VMID -------------------------------------------------------------- VMID=$(pvesh get /cluster/nextid) log "VMID=$VMID hostname=$HOSTNAME cores=$CORES mem=${MEMORY}MiB disk=${DISK}GiB" @@ -114,6 +140,22 @@ 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 ' + 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 + ' +fi + cat <