From 381c141c4e3bd359de6d13c01d2d28e911b05fa2 Mon Sep 17 00:00:00 2001 From: buildplan <170122315+buildplan@users.noreply.github.com> Date: Fri, 27 Jun 2025 00:54:10 +0100 Subject: [PATCH] Update setup_harden_debian_ubuntu.sh --- setup_harden_debian_ubuntu.sh | 75 ++++++++++++++++++++--------------- 1 file changed, 42 insertions(+), 33 deletions(-) diff --git a/setup_harden_debian_ubuntu.sh b/setup_harden_debian_ubuntu.sh index 60dcc53..5fc00b7 100644 --- a/setup_harden_debian_ubuntu.sh +++ b/setup_harden_debian_ubuntu.sh @@ -491,22 +491,21 @@ configure_ssh() { exit 1 fi - # Detect SSH service name and handle socket activation - if [[ $ID == "ubuntu" ]] && { systemctl is-enabled ssh.service >/dev/null 2>&1 || systemctl is-active ssh.service >/dev/null 2>&1; }; then + # Detect SSH service name, preserve socket activation on Ubuntu if active + if [[ $ID == "ubuntu" ]] && systemctl is-active ssh.socket >/dev/null 2>&1; then + SSH_SERVICE="ssh.socket" + print_info "Using SSH socket activation: $SSH_SERVICE" + elif [[ $ID == "ubuntu" ]] && { systemctl is-enabled ssh.service >/dev/null 2>&1 || systemctl is-active ssh.service >/dev/null 2>&1; }; then SSH_SERVICE="ssh.service" elif systemctl is-enabled sshd.service >/dev/null 2>&1 || systemctl is-active sshd.service >/dev/null 2>&1; then SSH_SERVICE="sshd.service" elif ps aux | grep -q "[s]shd"; then - print_warning "SSH daemon running but no standard service detected. Checking for socket activation..." - if systemctl is-active ssh.socket >/dev/null 2>&1; then - print_info "Disabling ssh.socket to enable ssh.service..." - systemctl disable --now ssh.socket - fi - SSH_SERVICE="ssh.service" + print_warning "SSH daemon running but no standard service detected." + SSH_SERVICE="ssh.service" # Default for Debian if ! systemctl enable --now "$SSH_SERVICE" >/dev/null 2>&1; then print_error "Failed to enable and start $SSH_SERVICE. Attempting manual start..." if ! /usr/sbin/sshd; then - print_error "Failed to start SSH daemon manually. Please check openssh-server installation." + print_error "Failed to start SSH daemon manually." exit 1 fi print_success "SSH daemon started manually." @@ -571,10 +570,20 @@ configure_ssh() { SSHD_BACKUP_FILE="$BACKUP_DIR/sshd_config.backup_$(date +%Y%m%d_%H%M%S)" cp /etc/ssh/sshd_config "$SSHD_BACKUP_FILE" - # Check if SSH config needs updating + # Use systemd drop-in for port override, preserving Ubuntu's socket activation + NEW_SSH_CONFIG=$(mktemp) + tee "$NEW_SSH_CONFIG" > /dev/null < /dev/null <