interplane-forge
INTERPLANE Forge
Brought by drakestapleton
interplane-forge / infra / hub / sshd-keepalive-apply.sh
| 1 | #!/usr/bin/env bash |
| 2 | # Hub Pi: install hub/sshd-forward-keepalive.conf so dead ssh sessions (and the ports they forward with |
| 3 | # -R) are dropped after about 75 s instead of 2 h 11 min. Run on the hub as root: |
| 4 | # sudo bash <staged infra>/hub/sshd-keepalive-apply.sh <staged infra> |
| 5 | # Steps, stopping at the first failure: preconditions (file not installed yet, Include of |
| 6 | # sshd_config.d present, `sshd -t` passes); install; `sshd -t` (failure removes the file again, nothing |
| 7 | # reloaded); reload ssh; check `sshd -T` is 15/4. A failed reload or a mismatched `sshd -T` removes the |
| 8 | # drop-in and, if the reload already happened, reloads again so the running sshd matches the disk. |
| 9 | # Undo: hub/sshd-keepalive-rollback.sh. |
| 10 | set -euo pipefail |
| 11 | INFRA=$(cd "${1:?usage: sshd-keepalive-apply.sh <infra dir>}" && pwd) |
| 12 | SRC=$INFRA/hub/sshd-forward-keepalive.conf |
| 13 | DST=${SSHD_DROPIN:-/etc/ssh/sshd_config.d/40-forward-keepalive.conf} |
| 14 | die() { echo "sshd-keepalive-apply: FAIL: $*" >&2; exit 1; } |
| 15 | [ "$(id -u)" = 0 ] || die "run as root (sudo)" |
| 16 | [ -f "$SRC" ] || die "missing $SRC" |
| 17 | [ ! -e "$DST" ] || die "$DST exists (already applied?)" |
| 18 | grep -qE '^Include /etc/ssh/sshd_config.d/\*\.conf' /etc/ssh/sshd_config || die "sshd_config does not include sshd_config.d/*.conf" |
| 19 | sshd -t || die "sshd -t fails before any change" |
| 20 | before=$(sshd -T 2>/dev/null | grep -E '^clientalive(interval|countmax) ' | sort | tr '\n' ' ') |
| 21 | echo "sshd-keepalive-apply: before: $before" |
| 22 | install -m 644 "$SRC" "$DST" |
| 23 | if ! sshd -t; then rm -f -- "$DST"; die "sshd -t fails with the drop-in; removed it, nothing reloaded"; fi |
| 24 | if ! ${SSHD_RELOAD:-systemctl reload ssh}; then |
| 25 | rm -f -- "$DST" |
| 26 | die "ssh reload failed; removed the drop-in (running sshd unchanged if the reload did not apply)" |
| 27 | fi |
| 28 | after=$(sshd -T 2>/dev/null | grep -E '^clientalive(interval|countmax) ' | sort | tr '\n' ' ' || true) |
| 29 | echo "sshd-keepalive-apply: after: $after" |
| 30 | if [ "$after" != "clientalivecountmax 4 clientaliveinterval 15 " ]; then |
| 31 | rm -f -- "$DST" |
| 32 | if ! ${SSHD_RELOAD:-systemctl reload ssh}; then |
| 33 | die "sshd -T after install: '$after'; drop-in removed but reload-back failed" |
| 34 | fi |
| 35 | die "sshd -T after install: '$after' (drop-in removed, ssh reloaded)" |
| 36 | fi |
| 37 | echo "sshd-keepalive-apply: APPLIED $DST (new sessions drop after 4 unanswered asks at 15 s, about 75 s)" |