EN · $ USD
Security, Backups and Cloud Storage

How to Install Fail2ban and Stop Brute-Force Attacks

7 min read Updated: Sunucu Atlas technical team

In short

Fail2ban watches your logs and temporarily blocks, through the firewall, any IP that racks up too many failed logins in a short time. On Ubuntu you install it with apt install fail2ban and configure it in /etc/fail2ban/jail.local. Enable the sshd, nginx-http-auth, postfix-sasl and dovecot jails, set findtime, maxretry and bantime, and brute-force attempts are stopped automatically.

In this guide
  1. How Does Fail2ban Work? Key Concepts
  2. Installing Fail2ban Step by Step
  3. jail.local: Global Settings and Ban Times
  4. Configuring the SSH Jail
  5. Configuring Jails for Nginx
  6. Jails for Mail Services (Postfix and Dovecot)
  7. recidive: Long Bans for Repeat Offenders
  8. Managing Banned IPs
  9. Common Mistakes
  10. Which Server Should You Run It On?
  11. Next Steps

Fail2ban is an open-source tool that monitors server logs and uses a firewall rule to block, for a set period, any IP address that makes too many failed login attempts in a short time. On an Ubuntu or Debian VDS you install it with apt install fail2ban, configure it in /etc/fail2ban/jail.local, and it automatically stops brute-force attacks through separate "jails" for SSH, Nginx and your mail services.

This guide walks you through installation, jail configuration for SSH, Nginx, Postfix and Dovecot, incremental ban times, writing your own filter, and managing banned IPs. The commands are written for Ubuntu 22.04 and 24.04 and work the same on Debian.

How Does Fail2ban Work? Key Concepts

You only need to understand four concepts to configure Fail2ban correctly:

  • Jail: A set of rules that protects one service. It defines which log to read, which filter to use and what to do when a violation is detected. Example: [sshd].
  • Filter: Regular expressions (failregex) in /etc/fail2ban/filter.d/ that match failed attempts in log lines.
  • Action: The command that runs when a violation is detected, usually blocking the IP with nftables, iptables or UFW.
  • findtime, maxretry, bantime: The three parameters of the rule "an IP that fails maxretry times within findtime is blocked for bantime."

Fail2ban does not replace a strong password; it is an extra layer. The real protection for SSH is key-based login, which we cover separately in our guide on creating an SSH key and disabling password login.

Installing Fail2ban Step by Step

  1. Update the package list and install Fail2ban. The python3-systemd package lets it read logs from the systemd journal:
    sudo apt update
    sudo apt install fail2ban python3-systemd -y
  2. Enable the service so it starts at boot:
    sudo systemctl enable --now fail2ban
    sudo systemctl status fail2ban
  3. Leave the main config file jail.conf untouched; package updates overwrite it. Create jail.local instead. Fail2ban reads jail.conf first, then jail.d/*.conf and jail.local; the value read last wins.

jail.local: Global Settings and Ban Times

Open the file with sudo nano /etc/fail2ban/jail.local and start with the base configuration below. Don't forget to add your own static IP address to the ignoreip line; otherwise a few mistyped passwords can lock you out.

[DEFAULT]
# Addresses that are never banned (add your own IP)
ignoreip = 127.0.0.1/8 ::1 203.0.113.10

# 5 failures within 10 minutes = 1 hour ban
findtime = 10m
maxretry = 5
bantime  = 1h

# Ban time grows exponentially for repeat offenders
bantime.increment = true
bantime.factor    = 2
bantime.maxtime   = 1w

# If you use UFW, apply bans through UFW
banaction = ufw

With bantime.increment enabled, each new ban for the same IP is a multiple of the previous one, so persistent bots stay locked out for periods that stretch from hours to days. If you don't use UFW, delete the banaction line and Fail2ban will use your distribution's default action (nftables or iptables). If you haven't set up UFW yet, see our guide on setting up a Linux firewall with UFW.

Recommended ban time values

Scenariofindtimemaxretrybantime
SSH (password login off, keys only)10m31h + incremental
SSH (password login on)10m31d + incremental
Nginx basic auth / login page10m51h
Mail (SMTP AUTH, IMAP)10m56h
recidive (repeat offenders)1d31w

Treat these values as a starting point. If your office sits behind a single NAT IP and many users connect from the same address, raise maxretry a little.

Configuring the SSH Jail

On Ubuntu the sshd jail is usually enabled by default after installing Fail2ban, but it is best to spell out the settings explicitly. If you changed your SSH port, be sure to update the port line; otherwise the ban rule blocks the wrong port.

[sshd]
enabled  = true
port     = ssh
# If you changed the port: port = 2222
backend  = systemd
maxretry = 3
bantime  = 1d

backend = systemd reads sshd entries straight from the journal. On recent Ubuntu releases, using the journal instead of /var/log/auth.log gives more reliable results.

Configuring Jails for Nginx

Fail2ban ships with ready-made filters for Nginx. These three are the most useful in practice:

  • nginx-http-auth: Wrong-password attempts against HTTP basic auth (for example, an admin panel protected with it).
  • nginx-botsearch: Bots probing for paths that don't exist, such as /phpmyadmin or /wp-admin.
  • nginx-limit-req: Excess requests rejected by the Nginx limit_req module.
[nginx-http-auth]
enabled = true

[nginx-botsearch]
enabled  = true
maxretry = 10

[nginx-limit-req]
enabled  = true
maxretry = 10
findtime = 1m

Logs are read automatically from the distribution default, /var/log/nginx/. If you defined a separate log file for your site, add a logpath = /var/log/nginx/site-error.log line to the relevant jail.

A custom filter for the WordPress login page

In WordPress, a failed login returns HTTP 200 and a successful one returns 302. A simple filter that uses this difference catches wp-login.php brute-force attempts. Create the file /etc/fail2ban/filter.d/nginx-wplogin.conf:

[Definition]
failregex = ^<HOST> -.*"POST /wp-login\.php HTTP/[0-9.]+" 200
ignoreregex =

Then add it to jail.local:

[nginx-wplogin]
enabled  = true
port     = http,https
filter   = nginx-wplogin
logpath  = /var/log/nginx/access.log
maxretry = 5
findtime = 10m

Before you enable the filter, test it against your real logs; the output shows how many lines match:

sudo fail2ban-regex /var/log/nginx/access.log /etc/fail2ban/filter.d/nginx-wplogin.conf

Jails for Mail Services (Postfix and Dovecot)

If you run your own mail server, SMTP AUTH and IMAP password guessing is one of the most common attacks you will see. Enabling the built-in jails for Postfix and Dovecot is all it takes:

[postfix-sasl]
enabled  = true
maxretry = 5
bantime  = 6h

[dovecot]
enabled  = true
maxretry = 5
bantime  = 6h

On Ubuntu servers, rsyslog writes mail logs to /var/log/mail.log, and Fail2ban's default paths point to that file. Docker-based mail stacks such as mailcow include their own blocking mechanism, so you don't need these jails for those setups.

recidive: Long Bans for Repeat Offenders

The recidive jail reads Fail2ban's own log and blocks an IP on all ports for one week if it was banned repeatedly within a day:

[recidive]
enabled  = true
logpath  = /var/log/fail2ban.log
banaction = %(banaction_allports)s
findtime = 1d
maxretry = 3
bantime  = 1w

When you are done with all settings, test the configuration and reload the service:

sudo fail2ban-client -t
sudo systemctl reload fail2ban

Managing Banned IPs

Every command you need day to day lives under fail2ban-client:

TaskCommand
List active jailssudo fail2ban-client status
Show banned IPs in a jailsudo fail2ban-client status sshd
Unban a single IPsudo fail2ban-client set sshd unbanip 198.51.100.7
Remove an IP from all jailssudo fail2ban-client unban 198.51.100.7
Ban an IP manuallysudo fail2ban-client set sshd banip 198.51.100.7
Remove all banssudo fail2ban-client unban --all

To see who was banned and when, use sudo tail -f /var/log/fail2ban.log or sudo grep "Ban " /var/log/fail2ban.log | tail -20.

If you lock yourself out

If your own IP gets banned, connect from a different network (for example, your phone's mobile data) and run the unbanip command. If you have no SSH access at all, log in through the VNC/console access your provider offers. To keep it from happening again, add your IP to the ignoreip line.

Common Mistakes

  • Editing jail.conf: Your changes are lost on the next update. Always use jail.local.
  • Wrong port: If SSH runs on 2222 but the jail blocks 22, the attacker is never blocked.
  • Not seeing the real IP behind a reverse proxy: If a proxy or CDN sits in front of Nginx, the logs record the proxy's IP. Without a real_ip_header setting, Fail2ban bans the proxy and your site goes down for everyone.
  • Skipping tests: Don't assume a filter works until fail2ban-regex shows it actually matches.

Which Server Should You Run It On?

Installing Fail2ban requires root access and permission to manage firewall rules, so it runs on your own VDS, not on shared hosting. Fail2ban is very lightweight; on its own it needs only a few tens of MB of memory, so even the smallest plan is enough. What really matters is the web, database or mail workload you will run on the server.

For PHP sites and control panels where single-core performance counts, the Ryzen Premium VDS plans with the AMD Ryzen 9 5900X are a good starting point. For mail servers, multiple sites and background services that need more RAM at a lower cost, consider the Intel Xeon-based Intel Premium VDS options. Both series include KVM virtualization, root access and layered DDoS protection: Fail2ban handles application-level brute-force attempts, while DDoS protection absorbs high-volume attacks at the network level.

Next Steps

After installing Fail2ban, do the following in order: disable SSH password login, open only the ports you need with UFW, enable the recidive jail, and check the fail2ban-client status output once a week. If you want to apply these steps on a clean server from scratch, pick one of Sunucu Atlas's Istanbul-based Ryzen VDS plans and follow along with our guide to the first 10 security steps on a new Ubuntu server. If you get stuck during setup, you can open a support ticket.

Frequently asked questions

Does Fail2ban still work after I change the SSH port?

Yes, but you must update the port line in the [sshd] section of jail.local to the new port. Otherwise the ban rule blocks the old port and the attacker is not stopped.

What should I do if Fail2ban bans my own IP address?

Connect from a different network or through your provider's console access and run fail2ban-client set sshd unbanip IP. To prevent it from happening again, add your IP to the ignoreip line.

Should I edit jail.conf or jail.local?

Always edit jail.local. Package updates overwrite jail.conf, while jail.local is preserved and overrides the same settings.

Does Fail2ban stop DDoS attacks?

No. Fail2ban works from logs, so it is effective against application-level attempts such as brute-force logins and scanning. High-volume network attacks must be mitigated at the network level before they reach the server.

Can Fail2ban be used together with UFW?

Yes. If you set banaction = ufw in jail.local, bans are added as UFW rules and you can see them with sudo ufw status.

How many resources does Fail2ban use?

Very few; on most servers it runs with a few tens of MB of memory. CPU usage can rise when it watches very large log files, so it helps to keep log sizes under control with logrotate.

For this guide

Recommended plans

Fail2ban needs root access and control over the firewall, so a Ryzen or Intel Premium VDS with KVM virtualization and layered DDoS protection is the right foundation if you want to manage brute-force protection yourself.

SA-R9-1

RYZEN9 5900X
Recommended

2 GB RAM

$3.93/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk30 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-2

RYZEN9 5900X

4 GB RAM

$5.90/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk40 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

SA-R9-3

RYZEN9 5900X

6 GB RAM

$8.85/mo

+VAT

  • Processor2 vCPU
  • ModelAMD Ryzen 9 5900X
  • Disk50 GB NVMe
  • LocationIstanbul
  • Port: 1G Port
  • Traffic: Fair use
  • Virtualization: KVM
  • Multi-layer DDoS protection

Ryzen Premium VDS — tüm paketler Intel Premium VDS — tüm paketler

Need help with setup?

Send questions about your service in a support ticket, and our technical team will walk you through it step by step.