Marcio Cunha

How to block SSH brute force attacks with Fail2Ban

Learn step by step how to configure Fail2Ban on Linux to automate the blocking of malicious IP addresses attempting unauthorized SSH access.

Marcio Cunha•3 min
Also available in:PortuguêsEspañol
Summary
  • Fail2Ban monitors operating system log files for repeated authentication failures to identify intruders.
  • Custom jail rules allow you to set strict attempt limits before isolating the address on the network.
  • Direct integration with the system firewall ensures hostile traffic is dropped immediately.
  • The use of security automation tools drastically reduces operational overhead in exposed internet environments.
  • Continuous monitoring of log files validates whether defense policies are performing as expected.

Why your internet-facing SSH server attracts so many invasions

Keeping a server connected to the internet is like leaving a house front door wide open on a busy street. Within minutes of placing a virtual machine or dedicated server online, automated bots from around the world start firing random passwords against the remote access port, a process technically known as a brute force attack. In practice, this means malicious software tests thousands of combinations per minute to guess weak credentials. To mitigate this risk and protect your infrastructure, you can expand your defenses by applying techniques similar to those discussed in our guide on how to block suspicious connections in Linux with iptables and fail2ban, creating an impenetrable barrier against rogue bots.

What Fail2Ban is and how it protects your system

Fail2Ban is a security software written in Python that acts as a tireless watchman for your operating system. It parses system log files, looking for repeated error patterns, such as consecutive login failures on the remote access port. When an IP address exceeds the administrator's tolerated error limit, the program adds a dynamic rule to the system firewall to block any further attempts from that address. To better understand the origin of these events, it is worth exploring how to read Linux syslog in terminal in real time, making it easier to spot suspicious behavior before it causes harm.

Step-by-step guide to installing and configuring Fail2Ban on Linux

Package installation is straightforward on most Debian or Ubuntu-based Linux distributions, requiring only quick terminal commands. Before changing any original developer configuration, it is essential to copy the default file to a customized version so your changes are not lost during package updates. Follow the instructions below to perform the installation and initial setup:

  1. Update your local package list by typing in the terminal:
    sudo apt update
  2. Install the security watchman package with the command:
    sudo apt install fail2ban -y
  3. Create a safety backup copy of the default configuration file by running:
    sudo cp /etc/fail2ban/jail.conf /etc/fail2ban/jail.local

Creating custom rules for the SSH service

With the local configuration file properly created, the next step is to adjust the specific parameters for the remote access channel. Open the newly created file in your preferred text editor to define the ban time and permitted error count. In the block corresponding to the remote access service, you must enable the monitoring directive by changing its parameter from disabled to enabled. Furthermore, establishing an adequate time window for counting errors prevents legitimate users from being accidentally blocked after forgetting their password once or twice.

Testing and validating active security policies

After saving configuration changes, it is essential to restart the security service so the system loads the newly applied rules. The restart command must be executed with administrative privileges in the terminal, followed by a status check to ensure monitoring is running without errors. You can also simulate an intentional login failure from another machine to verify that the corresponding IP address is correctly added to the firewall block list. This practical check ensures the system will respond properly when a real threat attempts to breach your corporate or personal environment. To explore these architectural concepts further in production, see our related technical analysis on How to test thermal fuse in power supply with multimeter.

Final considerations on remote access security

Automating server defense using monitoring tools and dynamic blocking is a fundamental requirement for any modern systems administrator. Although blocking malicious IP addresses significantly reduces the risk of brute force intrusions, combining these strategies with cryptographic keys and disabling direct password logins takes security to another level. Keep your packages up to date, monitor logs frequently, and adopt a proactive stance to ensure the absolute integrity of your computing environments.