interplane.Get Omega
Read in your language

interplane-forge

INTERPLANE Forge

Brought by drakestapleton

branch main Branches Tags History

interplane-forge / infra / hub / sshd-keepalive-apply.sh

37 lines · 2297 bytes Raw Blame History
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.
10set -euo pipefail
11INFRA=$(cd "${1:?usage: sshd-keepalive-apply.sh <infra dir>}" && pwd)
12SRC=$INFRA/hub/sshd-forward-keepalive.conf
13DST=${SSHD_DROPIN:-/etc/ssh/sshd_config.d/40-forward-keepalive.conf}
14die() { 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?)"
18grep -qE '^Include /etc/ssh/sshd_config.d/\*\.conf' /etc/ssh/sshd_config || die "sshd_config does not include sshd_config.d/*.conf"
19sshd -t || die "sshd -t fails before any change"
20before=$(sshd -T 2>/dev/null | grep -E '^clientalive(interval|countmax) ' | sort | tr '\n' ' ')
21echo "sshd-keepalive-apply: before: $before"
22install -m 644 "$SRC" "$DST"
23if ! sshd -t; then rm -f -- "$DST"; die "sshd -t fails with the drop-in; removed it, nothing reloaded"; fi
24if ! ${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)"
27fi
28after=$(sshd -T 2>/dev/null | grep -E '^clientalive(interval|countmax) ' | sort | tr '\n' ' ' || true)
29echo "sshd-keepalive-apply: after: $after"
30if [ "$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)"
36fi
37echo "sshd-keepalive-apply: APPLIED $DST (new sessions drop after 4 unanswered asks at 15 s, about 75 s)"