#!/bin/bash # Define the SSH configuration file path SSH_CONFIG="/etc/ssh/sshd_config" # Define the backup file path SSH_CONFIG_BACKUP="/etc/ssh/sshd_config.bak" # Restore the backup first if [ -f "$SSH_CONFIG_BACKUP" ]; then cp "$SSH_CONFIG_BACKUP" "$SSH_CONFIG" else echo "Backup not found. Trying to fix the existing sshd_config file." fi # Remove the incorrect line with \n if present sed -i '/Match User vdx\\n/d' "$SSH_CONFIG" # Add the correct line echo -e "Match User vdx\n PasswordAuthentication no" >> "$SSH_CONFIG" # Test SSH configuration if sshd -t; then echo "SSH configuration test passed." # Restart SSH service if systemctl is-active --quiet sshd; then systemctl restart sshd echo "SSH service restarted successfully." systemctl status ssh else systemctl restart ssh echo "SSH service restarted successfully." systemctl status ssh fi else echo "SSH configuration test failed. Manual intervention required." systemctl start ssh systemctl status ssh fi