diff --git a/setup_harden_debian_ubuntu.sh b/setup_harden_debian_ubuntu.sh index eff2257..80aeceb 100644 --- a/setup_harden_debian_ubuntu.sh +++ b/setup_harden_debian_ubuntu.sh @@ -1,7 +1,7 @@ #!/bin/bash # Debian 12 and Ubuntu Server Hardening Interactive Script -# Version: 3.9 | 2025-06-26 +# Version: 3.10 | 2025-06-27 # Compatible with: Debian 12 (Bookworm), Ubuntu 20.04 LTS, 22.04 LTS, 24.04 LTS # # Description: @@ -79,7 +79,7 @@ print_header() { echo -e "${CYAN}╔═════════════════════════════════════════════════════════════════╗${NC}" echo -e "${CYAN}║ ║${NC}" echo -e "${CYAN}║ DEBIAN/UBUNTU SERVER SETUP AND HARDENING SCRIPT ║${NC}" - echo -e "${CYAN}║ v3.9 | 2025-06-26 ║${NC}" + echo -e "${CYAN}║ v3.10 | 2025-06-26 ║${NC}" echo -e "${CYAN}╚═════════════════════════════════════════════════════════════════╝${NC}" echo } @@ -229,6 +229,7 @@ check_system() { if [[ -f /etc/os-release ]]; then source /etc/os-release + ID=$ID # Populate global ID variable if [[ $ID == "debian" && $VERSION_ID == "12" ]] || \ [[ $ID == "ubuntu" && $VERSION_ID =~ ^(20.04|22.04|24.04)$ ]]; then print_success "Compatible OS detected: $PRETTY_NAME" @@ -490,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." @@ -570,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 <¼ to > tee "$NEW_DOCKER_CONFIG" > /dev/null <