fix(install): sshd hardening no longer aborts when ssh.service is inactive

harden_sshd ended with an unconditional 'systemctl reload ssh'. On WSL (and any
host where sshd was never started) the unit is inactive, systemctl exits with
'ssh.service is not active, cannot reload.' and set -e killed the whole install
right after the drop-in was validated.

- reload only when 'systemctl is-active --quiet ssh'; else say the drop-in is in
  place and applies at the next sshd start
- try-reload-or-restart instead of reload: exits 0 if sshd stopped in between
- create /run/sshd before 'sshd -t': on Ubuntu 24.04 the privsep dir only exists
  while ssh.service runs, so the validation failed on an inactive host and the
  valid drop-in was removed as 'rejected'
This commit is contained in:
bchanot committed 2026-10-12 01:14:40 +02:00
1 parent e82af805fa
commit 64f06bbbbc
1 file changed
+10 -4
+10 -4
View File
@@ -220,19 +220,25 @@ install_unattended_upgrades() {
# sshd hardening drop-in (PermitRootLogin, MaxAuthTries, LoginGraceTime): only
# settings that cannot lock anyone out; authentication methods stay untouched.
# Validated with sshd -t before the reload. A rejected file is removed rather than
# left in place, so sshd keeps starting on the next boot; the install goes on and
# the warning tells you.
# Validated with sshd -t (/run/sshd is created first so the check works without a
# running sshd); the reload happens only when sshd is running. A rejected file is
# removed rather than left in place, so sshd keeps starting on the next boot; the
# install goes on and the warning tells you.
harden_sshd() {
echo "Deploying sshd hardening drop-in"
sudo install -D -m 0644 "$SCRIPT_DIR/etc/ssh/sshd_config.d/20-hardening.conf" \
/etc/ssh/sshd_config.d/20-hardening.conf
sudo install -d -m 0755 /run/sshd
if ! sudo sshd -t; then
sudo rm -f /etc/ssh/sshd_config.d/20-hardening.conf
echo "sshd rejected 20-hardening.conf — removed, sshd config unchanged" >&2
return 0
fi
sudo systemctl reload ssh
if systemctl is-active --quiet ssh; then
sudo systemctl try-reload-or-restart ssh
else
echo "ssh.service not running — drop-in in place, applies at the next sshd start"
fi
}
# Yes/no prompt for the optional system changes. $2 is the default taken on Enter: