Linux & Command Line ADVANCED Linux (Debian/Ubuntu) โฑ๏ธ 5 min read ๐Ÿ“… Updated: 15 Oct 2026

Automating SSH Brute Force Detection: Fail2Ban & AWStats Guide

This guide details how to implement a robust, automated defense system by using Fail2Ban to monitor SSH logs and automatically ban malicious IP addresses, complemented by AWStats for historical analysis.

Gurveer Singh
Gurveer Singh โœ“ Verified Specialist
Founder & Chief Architect ยท hSECURITIES Labs
โšก FAST-TRACK PROTOCOL

Direct Diagnostic & Remediation Summary

  • Install Tools: Update the system and install necessary packages: sudo apt update && sudo apt install fail2ban awstats.
  • Configure Fail2Ban: Create or edit the jail configuration file to monitor SSH logs: sudo nano /etc/fail2ban/jail.local.
๐Ÿ› ๏ธ Tools & Prerequisites: A Linux server (Debian/Ubuntu recommended), root or sudo privileges, and basic familiarity with the command line.

๐Ÿš€ Executive Overview: Automated SSH Security Hardening

This tutorial guides you through establishing a multi-layered defense mechanism against brute-force attacks targeting your SSH service. By integrating Fail2Ban to actively monitor authentication logs and AWStats for historical traffic analysis, we create an automated system that detects suspicious login attempts in real time and automatically blocks offending IP addresses.

โš ๏ธ Problem Overview & Error Symptoms

SSH brute-force attacks involve attackers using automated scripts to rapidly guess valid usernames and passwords against your server's SSH port (default 22). Symptoms include:

  • Excessive failed login attempts visible in the system logs (/var/log/auth.log or similar).
  • Intermittent service disruptions due to repeated connection failures before a legitimate user can connect.
  • A general feeling of vulnerability when managing publicly exposed SSH access.
๐Ÿ” Root Cause & Diagnostic Overview:

The root cause is the lack of rate limiting and automated response mechanisms for failed authentication attempts. The mechanism relies on:

  • Log Monitoring (Fail2Ban): Fail2Ban uses specific log patterns (regex) to watch system logs. When a pattern matching 'authentication failure' occurs repeatedly from the same source IP, it triggers an action.
  • Temporary Firewall Blocking: The action typically involves updating the local firewall (iptables or nftables) to drop all packets from that offending IP for a specified time period (the 'bantime').
  • Historical Analysis (AWStats): AWStats processes web server logs, providing visibility into who accessed your site and when, complementing Fail2Ban's focus on authentication attempts.
STEP 1

Initial Triage & Environment Isolation (Package Installation)

Before configuring anything, ensure the necessary tools are installed and that your SSH service is running correctly. We will use apt for Debian/Ubuntu systems.

sudo apt update
sudo apt install fail2ban awstats

Verify SSH service status:

sudo systemctl status sshd

Ensure the output shows 'active (running)'. If not, troubleshoot your SSH installation first.

STEP 2

Configuring Fail2Ban for SSH Monitoring

Fail2Ban requires a local configuration file to override default settings, ensuring your changes persist across updates. We will create a dedicated jail for SSH.

  • Create the local configuration file: sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local
  • Edit the new local file using nano or vi: sudo nano /etc/fail2ban/jail.local

Inside /etc/fail2ban/jail.local, locate or add the following block and modify parameters as needed:

[sshd]
enabled = true
port    = ssh
filter   = ssh
ban           = 1h  # Ban time: 1 hour
findtime      = 30m # Time window to detect failures within
maxretry      = 5   # Number of attempts before ban

Save the file and exit the editor.

STEP 3

Applying Rules & Testing Fail2Ban

We now restart the service to load the new ruleset. This step activates the monitoring.

  • Restart Fail2Ban service: sudo systemctl restart fail2ban
  • Test if the filter is correctly loaded (optional but recommended): fail2ban-client status sshd

For immediate testing, try to force a few failed logins from a different machine or terminal window. You should see Fail2Ban detect and ban the source IP after exceeding the maxretry threshold.

STEP 4

Integrating AWStats for Web Log Visibility

While Fail2Ban handles authentication, AWStats analyzes web access logs (e.g., /var/log/apache2/access.log). This step ensures historical visibility into attack patterns affecting the web layer.

  • Generate the initial stats report for your web directory: sudo awstats setup /var/www/html/logs/access.log
  • View a summary of top visitors and IPs: awstats index.html bulk

Note: AWStats typically requires cron jobs to run periodically (e.g., daily) to process new log files automatically.

TROUBLESHOOTING

Common Pitfalls & Advanced Diagnostics

If the system isn't working, check these common failure points:

  • Firewall Conflict: If you use a custom firewall setup (like UFW), ensure that Fail2Ban is allowed to manipulate iptables rules. Sometimes manual firewall rules override Fail2Ban's actions.
  • Log Path Mismatch: The most common error. Double-check the logpath directive in your jail configuration file; it must point exactly to where SSH logs are written (e.g., /var/log/auth.log).
  • Service Dependency: Ensure that Fail2Ban has permission to read the log files. If running as a non-root user, permissions might be restricted.

Advanced Check: Review the journal logs for failures:

sudo journalctl -u fail2ban.service --since "1 hour ago"
FINAL STEP

Verification, Results & Long-Term Stability Validation

To confirm the system is fully operational, perform a controlled test:

  1. Simulated Attack: From an external machine (or another terminal), attempt to SSH into the server using incorrect credentials 6 times in quick succession.
  2. Verification Check: Wait slightly longer than the bantime specified (e.g., 1 hour + buffer). Then, attempt to connect again from that same external IP. The connection should be rejected by the firewall/SSH daemon immediately.
  3. Cleanup: If the block is successful, manually unban the IP for testing purposes using sudo fail2ban-client set sshd unbanip YOUR_OFFENDING_IP.

For long-term stability, schedule a daily cron job to run AWStats cleanup and report generation.

Resolution Complete: You have successfully implemented automated defense against SSH brute force attacks using Fail2Ban for real-time blocking and AWStats for historical log auditing. Regularly review the /var/log/fail2ban.log file to monitor successful bans and ensure your IP whitelisting remains accurate.
COMMAND LINE / TERMINAL
sudo apt update && sudo apt install fail2ban awstats
sudo nano /etc/fail2ban/jail.local
sudo systemctl restart fail2ban
fail2ban-client status sshd

Frequently Asked Questions

Does Fail2Ban work if I change my SSH port from 22?

Yes, but you must update the <code>port</code> directive in your jail.local file to match your custom SSH port (e.g., <code>port = 2222</code>).

How do I whitelist my personal IP address so I am never banned?

Add an explicit exclusion section at the top of your jail.local file using <code>[DEFAULT] ignoreip = 127.0.0.1/8 ::1 YOUR_STATIC_HOME_IP/32</code>.

What if AWStats reports data but nothing seems to be happening?

Ensure that the web server logs (e.g., Apache or Nginx) are actively writing entries into the log file path specified in your <code>awstats setup</code> command, and that the cron job for AWStats is running successfully.

Engineering Assistance

Need help implementing this solution?

Our cybersecurity and infrastructure engineers can audit, harden, or configure your systems directly.

Request Technical Assistance โ†’
Gurveer Singh
Gurveer Singh โœ“ Verified Author
Founder & CEO at hSECURITIES | Cybersecurity Educator | Security Researcher | Ethical Hacking Enthusiast | Technology Mentor
Meet the Founder โ†’

Gurveer Singh Saggu is the Founder and CEO of hSECURITIES, a cybersecurity and technology education platform focused on practical learning, digital security awareness, hardware diagnostics, Linux administration, and real-world cybersecurity training.

With a strong passion for technology, ethical hacking, and security education, he has spent years helping students, professionals, and organizations understand modern cyber threats and develop practical technical skills. Through hSECURITIES, he creates hands-on learning resources, technical guides, security awareness content, and community-driven initiatives that make cybersecurity more accessible.

His mission is to bridge the gap between theoretical knowledge and practical implementation by providing step-by-step learning experiences, technical troubleshooting resources, and security-focused educational platforms.

Community Discussion & Confirmations (0)

Technical questions, hardware findings, and verified fix reports.

No reader questions posted yet. Have you tried this solution or experienced an edge case? Post below.

Ask a Question or Share Solution