Security is useless without proper monitoring. This guide covers fail2ban protection, connection optimization, and comprehensive log analysis.
Part 1: Fail2Ban Protection
Fail2ban monitors log files and automatically blocks IP addresses that show malicious behavior.
Install Fail2Ban
# Ubuntu/Debiansudo apt install fail2ban
# RHEL/CentOSsudo yum install epel-releasesudo yum install fail2banConfigure Fail2Ban
Create a local configuration file:
sudo nano /etc/fail2ban/jail.local[DEFAULT]# Ban IP for 1 hour after 3 failed attempts within 10 minutesbantime = 3600findtime = 600maxretry = 3destemail = [email protected]sendername = Fail2Banaction = %(action_mwl)s
[sshd]enabled = trueport = 2222 # Match your SSH portfilter = sshdlogpath = /var/log/auth.log # Debian/Ubuntu# logpath = /var/log/secure # RHEL/CentOSmaxretry = 3bantime = 3600Start Fail2Ban
sudo systemctl enable fail2bansudo systemctl start fail2banMonitor Fail2Ban
# Check statussudo fail2ban-client status sshd
# View banned IPssudo fail2ban-client get sshd banned
# Unban an IPsudo fail2ban-client set sshd unbanip 192.168.1.100Part 2: SSH Connection Management
Create SSH Config for Easy Access
On your local machine, create ~/.ssh/config:
Host production-server HostName server_ip Port 2222 User deployer IdentityFile ~/.ssh/id_prod_server ServerAliveInterval 60 ServerAliveCountMax 3
Host staging-server HostName staging_ip Port 2222 User deployer IdentityFile ~/.ssh/id_staging_server ProxyJump bastion-host # Jump through bastionNow you can connect simply with:
ssh production-serverSSH Agent for Key Management
Load keys into SSH agent to avoid re-entering passphrases:
# Start agenteval "$(ssh-agent -s)"
# Add keysssh-add ~/.ssh/id_prod_server
# List loaded keysssh-add -lPart 3: Monitoring and Logging
Enable Detailed SSH Logging
In /etc/ssh/sshd_config:
LogLevel VERBOSEMonitor Authentication Logs
# Real-time monitoring (Ubuntu/Debian)sudo tail -f /var/log/auth.log
# Real-time monitoring (RHEL/CentOS)sudo tail -f /var/log/secure
# Search for failed attemptssudo grep "Failed password" /var/log/auth.log | tail -20
# Successful loginssudo grep "Accepted publickey" /var/log/auth.log | tail -20Create Monitoring Script
Save this as /usr/local/bin/ssh-monitor.sh:
#!/bin/bash# SSH Security Monitoring Script
LOG_FILE="/var/log/auth.log" # Change for RHEL: /var/log/secureREPORT_FILE="/var/log/ssh-security-report.txt"
echo "SSH Security Report - $(date)" > $REPORT_FILEecho "================================" >> $REPORT_FILEecho "" >> $REPORT_FILE
echo "Failed Login Attempts:" >> $REPORT_FILEgrep "Failed password" $LOG_FILE | awk '{print $1, $2, $3, $11}' | sort | uniq -c | sort -nr | head -20 >> $REPORT_FILEecho "" >> $REPORT_FILE
echo "Successful Logins:" >> $REPORT_FILEgrep "Accepted publickey" $LOG_FILE | awk '{print $1, $2, $3, $9, $11}' | tail -20 >> $REPORT_FILEecho "" >> $REPORT_FILE
echo "Active SSH Sessions:" >> $REPORT_FILEwho >> $REPORT_FILEecho "" >> $REPORT_FILE
echo "Current Fail2Ban Bans:" >> $REPORT_FILEfail2ban-client status sshd 2>/dev/null >> $REPORT_FILE
cat $REPORT_FILEMake it executable and run daily:
sudo chmod +x /usr/local/bin/ssh-monitor.sh
# Add to crontabecho "0 9 * * * /usr/local/bin/ssh-monitor.sh | mail -s 'SSH Security Report' [email protected]" | sudo crontab -Next Steps
With monitoring in place, review the Troubleshooting guide for common issues, compliance requirements, and ongoing maintenance best practices.