Fail2ban

On this page 7

Fail2ban is an open-source log-based intrusion prevention daemon for Linux and other Unix-like systems. It watches service logs for repeated signs of abuse, such as failed SSH logins or rejected web authentication, and temporarily blocks the offending IP addresses by adding rules to the host firewall.

In Linux security, Fail2ban is a classic layer of SSH hardening and service protection. It does not fix weak passwords or vulnerable software, but it sharply reduces the noise and success rate of automated brute-force attacks against any service that writes useful log entries.

How Fail2ban detects and blocks attackers

Fail2ban works as a loop of read, match, count and act. A Python daemon follows log files or the systemd journal, applies regular expressions to each new line, extracts the client address from matching lines and counts failures per address. When an address exceeds a threshold within a time window, Fail2ban runs a ban action, usually inserting a firewall rule, and later runs an unban action when the ban expires.

The configuration is built from three kinds of objects. A filter defines what a failure looks like in the logs. An action defines what to do about it. A jail binds a filter to one or more actions and to a log source, with its own thresholds. Typical jails exist for sshd, Postfix, Dovecot, Apache, nginx and many other services.

The table below lists the core components and where they live on a typical installation.

ComponentRoleTypical location
JailCombines a filter, actions and thresholdsjail.local or jail.d/*.local
FilterRegular expressions for failure linesfilter.d/*.conf
ActionCommands for ban and unbanaction.d/*.conf
BackendMethod of reading logssystemd journal, pyinotify or polling
fail2ban-serverDaemon that runs jailsStarted by the init system
fail2ban-clientCommand-line control and statusUsed by administrators and scripts
Core Fail2ban components

Key parameters and typical values

Three parameters define the behavior of every jail: maxretry, findtime and bantime. An address is banned when it produces maxretry failures within findtime seconds, and the ban lasts for bantime. Upstream defaults are commonly five failures within ten minutes leading to a ten-minute ban, but distributions and administrators often choose stricter values.

ParameterMeaningCommon rangeEffect of a higher value
maxretryFailures allowed before a ban3 to 10More tolerant of user mistakes
findtimeWindow for counting failures10 minutes to 1 dayCatches slower attacks
bantimeDuration of the ban10 minutes to several daysLonger protection, more lockout risk
ignoreipAddresses never bannedLocalhost and admin networksPrevents self-lockout
bantime.incrementGrows the ban for repeat offendersFactor or exponential growthPunishes persistent sources
banactionFirewall backend usednftables, iptables, firewalld, ufwMust match the host firewall
Main jail parameters

The special recidive jail reads Fail2ban’s own log and bans addresses that were banned repeatedly, for example several times in a day, for a much longer period such as a week. This combination of short initial bans and long repeat bans balances user forgiveness against persistent attackers.

Fail2ban compared with other protective controls

Fail2ban is a reactive, host-level control. It complements, but never replaces, preventive measures such as key-only SSH authentication and network filtering.

ControlHow it protectsRelationship to Fail2ban
SSH key authenticationRemoves password guessing entirelyMakes brute force futile; Fail2ban reduces noise
Firewall allowlistingExposes services only to known networksStronger where feasible
Rate limiting in the service or firewallLimits connection rates per addressWorks without log parsing
PAM account lockoutLocks the targeted accountProtects accounts, not the host
Web application firewallInspects HTTP requestsCovers attacks that leave no log failures
Network intrusion preventionAnalyzes traffic patternsBroader scope, more complexity
Fail2ban and related defenses

A useful distinction is account-centric versus source-centric protection. PAM lockout punishes the targeted user, which an attacker can abuse to lock out legitimate accounts. Fail2ban punishes the source address, which keeps users working but is weaker against attacks spread across many addresses.

Configuring Fail2ban safely

Safe configuration starts with never editing the shipped jail.conf. Local changes go into jail.local or files in jail.d, which survive package upgrades. Each jail should be enabled explicitly, with thresholds suited to the service it protects.

  • Add administrator networks to ignoreip before enabling strict jails, so a mistyped password does not lock out the team.
  • Match the backend to the logging setup: on systems that log only to the journal, use the systemd backend.
  • Choose the right banaction for the active firewall, such as nftables on modern distributions.
  • Test filters with fail2ban-regex against real log samples before relying on them.
  • Check jail status with fail2ban-client status sshd and unban with fail2ban-client set sshd unbanip.

When services sit behind a reverse proxy or load balancer, logs may show the proxy address instead of the client. In that case the service must log the real client address, and bans must be applied where the traffic actually enters, otherwise Fail2ban may ban the proxy itself.

Limitations and common mistakes

Fail2ban can only react to what appears in the logs, after the attempt has already happened. It offers little against distributed attacks from thousands of addresses, each staying below the threshold, and it is not a defense against volumetric denial of service.

SymptomLikely causeFix
No bans despite attacksWrong log path or backendPoint the jail to the actual log source
Filter never matchesLog format changed after an updateUpdate the filter and retest with fail2ban-regex
Administrators locked outMissing ignoreip entriesAdd trusted networks and use console access to recover
Bans have no effectAction does not match the firewall in useSelect the correct banaction
IPv6 attackers ignoredOld version or IPv4-only actionUse a recent release with IPv6 support
Typical Fail2ban problems

The idea in brief

Fail2ban is like a doorman who keeps a notebook. Every time someone tries a wrong key at the door, the doorman writes down who it was. After a few failed tries in a short time, that person is not allowed near the door for a while.

Honest visitors who mistype once are not affected. Automated burglars who try thousands of keys are turned away quickly, which keeps the building quieter and safer.

A practical scenario

A small company runs a public Linux server for its customer portal. The authentication log shows thousands of failed SSH logins every day from automated scanners, which fills the disk with log entries and hides genuine events.

The administrator first switches SSH to key-only authentication. Then Fail2ban is installed with an sshd jail using the systemd backend and the nftables banaction: three failures within ten minutes lead to a one-hour ban, the office network is listed in ignoreip, and the recidive jail bans repeat offenders for a week.

Within a day, failed attempts in the logs drop to a small fraction of their previous volume, and remaining entries are easier to review. The server was already safe thanks to keys, but Fail2ban removes most of the noise and adds another layer of protection.