create-lxc: prompt for root password, enable sshd when set

Accepts ROOT_PASSWORD from env or reads from /dev/tty (works under
curl|bash). When set, pipes it through `chpasswd` via stdin (secret
stays out of argv) and installs+enables openssh with PermitRootLogin
yes. Skipping leaves the prior no-password behavior intact so pct
enter remains the fallback.
This commit is contained in:
d4nn13 committed 2026-04-19 19:11:01 +02:00
1 parent 4e7e0f34ba
commit eaca8305ae
2 files changed
+53 -1

No files matched your search

+11 -1
View File
@@ -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 <VMID>`. SSH won't work in that case.
## Finishing up (inside the LXC)
+42
View File
@@ -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
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
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 <<EOF
[create-lxc] container $VMID ($HOSTNAME) provisioned.