Fail2ban is a service that reads logs and temporarily blocks IP addresses with repeated failed login attempts, for example during SSH password brute-forcing. In this article, you'll install it, enable SSH protection, verify that it works, and learn how to unblock an IP if something goes wrong.

What Fail2ban Is and How It Works

Fail2ban monitors log files (or the systemd journal) and looks for lines that indicate failed login attempts. If the same IP racks up too many such attempts within a short period, the program adds a rule to the firewall, and the address temporarily loses access to the server.

The chain looks like this:

  1. A service (SSH, for example) logs a failed login attempt.

  2. A filter — a set of regular expressions — finds such lines and extracts the IP.

  3. A jail defines the rule for a specific service: which log to read, how many attempts are allowed, and for how long to ban.

  4. An action is triggered when a jail fires: most often, adding a rule to the firewall (iptables, nftables, firewalld, etc.).

It's important to understand the limits: Fail2ban doesn't replace SSH key authentication or system updates. It reduces log noise and the load from password brute-forcing, but it doesn't make your server invulnerable.

What You'll Need Before You Start

  • A VPS with root access or a user with sudo privileges.

  • A modern distribution: Ubuntu, Debian, AlmaLinux, Rocky Linux, or similar.

  • A working firewall (ufw, firewalld, nftables, or iptables).

  • A second terminal window or access to the console in your VPS control panel. This will come in handy if you accidentally lock yourself out.

This guide assumes a server where you have full root access, such as on Leneex VPS plans. The commands will also work on any other Linux VPS.

Installing Fail2ban

Ubuntu and Debian

sudo apt update
sudo apt install fail2ban

On most versions, the service starts right after installation. Let's enable autostart and start it explicitly:

sudo systemctl enable --now fail2ban

AlmaLinux, Rocky Linux, and Other RHEL-Compatible Systems

Fail2ban is in the EPEL repository, so first enable it:

sudo dnf install epel-release
sudo dnf install fail2ban
sudo systemctl enable --now fail2ban

Checking the Status

sudo systemctl status fail2ban

The output should show active (running). If the service didn't start, see the "Common Issues" section below.

Configuring via jail.local

The main Fail2ban config is /etc/fail2ban/jail.conf, but you shouldn't edit it directly: a package update might overwrite the file. Instead, create /etc/fail2ban/jail.local. Settings in it override the values from jail.conf, while everything else keeps its default.

sudo nano /etc/fail2ban/jail.local

A basic [DEFAULT] section:

[DEFAULT]
ignoreip = 127.0.0.1/8 ::1 203.0.113.10
bantime  = 1h
findtime = 10m
maxretry = 5
backend  = auto

Replace 203.0.113.10 with your own static IP address. This protects you from accidentally locking yourself out.

What each parameter means:

Parameter

What It Does

Example Value

ignoreip

List of addresses and networks that are never banned

127.0.0.1/8 ::1 203.0.113.10

bantime

How long an IP stays blocked

1h (one hour), 1d (one day)

findtime

The time window over which failed attempts are counted

10m

maxretry

How many failed attempts are allowed within findtime

5

backend

Where to read events from: log files or the systemd journal

auto, systemd

The values above are a reasonable starting point, not the only correct option. If you only log in via keys on your server and still see a lot of brute-forcing, you can increase bantime to a full day. If you frequently mistype your password, it's better to raise maxretry. Check the official Fail2ban documentation for your version for the exact default values.

Regarding backend: on newer systems that don't have a /var/log/auth.log file (for example, a fresh Debian install without rsyslog), set backend = systemd, otherwise Fail2ban simply won't see login attempts.

Protecting SSH (sshd Jail)

Add a section for SSH to the same jail.local:

[sshd]
enabled = true
port    = ssh
maxretry = 4

If SSH runs on a non-standard port, specify its number, otherwise the ban won't work:

[sshd]
enabled = true
port    = 2222

Apply the settings:

sudo systemctl restart fail2ban

Before restarting, make sure your IP is listed in ignoreip and that you have a second SSH session open nearby. This way you can quickly roll back the config if something goes wrong.

How to Verify Fail2ban Is Working

Check the overall status and the list of active jails:

sudo fail2ban-client status

The output will look something like this:

Status
|- Number of jail:      1
`- Jail list:   sshd

Check a specific jail:

sudo fail2ban-client status sshd

The response will include counters for failed attempts and a list of banned IPs.

Watch the log in real time:

sudo tail -f /var/log/fail2ban.log

When a ban triggers, you'll see Found and Ban lines in the log.

Safe test: from another device or a test server (not the IP you use to manage the machine), enter an incorrect password several times. Once maxretry is exceeded, connections from that address should stop going through.

If you don't have a test machine, you can check the ban mechanism manually by specifying an address that definitely isn't yours:

sudo fail2ban-client set sshd banip 203.0.113.50
sudo fail2ban-client set sshd unbanip 203.0.113.50

Don't test the protection using your own IP until you've confirmed it's in ignoreip and you have access to the console in your VPS control panel.

How to Unblock an IP and Manage Bans

Task

Command

View banned IPs

sudo fail2ban-client status sshd

Ban an IP manually

sudo fail2ban-client set sshd banip 203.0.113.50

Unblock an IP

sudo fail2ban-client set sshd unbanip 203.0.113.50

Unblock all addresses

sudo fail2ban-client unban --all

Reload configuration

sudo fail2ban-client reload

If you've locked yourself out, SSH access is no longer available. Open the web console or VNC console in your VPS control panel, log in from there, and run unbanip for your address. Then add the IP to ignoreip.

Additional Jails: Nginx and Other Services

Fail2ban ships with a set of ready-made filters for Nginx, Apache, Postfix, Dovecot, and other services. For example, to enable protection against password brute-forcing on an Nginx page with basic authentication, add this to jail.local:

[nginx-http-auth]
enabled = true
port    = http,https
logpath = %(nginx_error_log)s

After making changes, run sudo systemctl restart fail2ban again. The list of available filters is located in the /etc/fail2ban/filter.d/ directory. For each new jail, first make sure the log path and port are specified correctly.

Common Issues

The Service Won't Start

Possible Cause: Syntax error in jail.local

Solution: Check the config with sudo fail2ban-client -t and review journalctl -u fail2ban

Fail2ban Doesn't See Login Attempts

Possible Cause: Missing log file or wrong backend

Solution: Set backend = systemd or check the logpath

No Ban Despite a Non-Standard SSH Port

Possible Cause: The jail still specifies port ssh

Solution: Enter the actual port number in the port parameter

IP Is Banned, But the Attacks Continue

Possible Cause: Conflict with the firewall or wrong action

Solution: Check which firewall is in use and choose the appropriate banaction

You've Locked Yourself Out

Possible Cause: Your IP isn't in ignoreip

Solution: Log in through the VPS control panel console and run unbanip

Conclusion

Fail2ban is one layer of defense, not the only one. It handles password brute-forcing and bot noise well, but it only works properly alongside SSH keys, regular updates, and a properly configured firewall. You can cross-check the overall security picture with "Hosting Security Checklist: 10 Criteria".

If you want to practice all these steps without risking a production project, a separate VPS with root access is a good fit: it's a safe place to experiment with jail.local without worrying about locking yourself out of a live server.

FAQ

What is Fail2ban and what is it used for?

It's a service that analyzes logs and blocks IP addresses with repeated failed login attempts. It's used to protect SSH and other services from password brute-forcing.

Does Fail2ban protect against DDoS?

No. Fail2ban works with logs and reacts to specific repeated events, so it doesn't mitigate volumetric attacks. DDoS protection requires other tools.

How do I unblock an IP in Fail2ban?

Run sudo fail2ban-client set sshd unbanip ADDRESS, substituting the name of the relevant jail and the IP. If you don't have SSH access, log in through the console in your VPS control panel.

What's the difference between bantime, findtime, and maxretry?

maxretry is the number of allowed failed attempts, findtime is the period over which they're counted, and bantime is how long the block lasts once the limit is exceeded.

Do I need Fail2ban if SSH already uses key-based login?

Key-based login already significantly reduces the risk of password guessing. Fail2ban still cuts down on log noise and bot load, so it remains useful as an extra layer of defense.