LinuxPublished on July 13, 202612 min read

How to install and configure Fail2ban in 2026

Fail2ban reads the logs of a Linux VPS, spots repeated failed logins and has the firewall ban the offending address. This guide shows how to install Fail2ban and configure it on Debian 12 and 13 and on Ubuntu 22.04 and 24.04. It also covers the whitelist, unbanning and Nginx. Fail2ban is a Linux tool, and the guide Block RDP brute-force attacks covers the same ground on a Windows VPS, with IPBan.

Keep a way back in

A configuration mistake can lock you out of SSH. Prepare a way out before you start.

  • Keep your current SSH session open until you have finished testing.

  • Write down your public IP address if it is static. You can add it to the whitelist later.

  • Never test a ban from your only way into the server.

  • Find out which port SSH really listens on.

sudo sshd -T | grep '^port '

If the command returns, say, port 2222, the SSH jail will need port = 2222 instead of port = ssh.

The console in the client area shows the VPS screen in your browser. It goes through neither SSH nor the VPS firewall, so a ban cannot block it.

  1. In My Services, open the VPS page.

  2. Click the blue Console button at the top right.

  3. The Open the remote console? window appears. Click Open the console.

  4. Log in with the user and password shown in the Password tab.

Open the remote console window with the Open the console button

The guide How to manage your VPS from the client area covers the console in detail.


Understand how Fail2ban works

Fail2ban always follows the same path, from the log to the firewall.

  1. A service such as SSH or Nginx writes every failed login to its log.

  2. A filter spots these failures with regular expressions and picks out the IP address.

  3. A jail counts the failures of each address over a set period.

  4. Past the threshold, an action bans the address in the firewall, with nftables or iptables.

  5. The ban expires on its own. An address that comes back can be banned for longer.

Three settings decide when a ban starts.

SettingRoleValue in this guide
maxretryNumber of failures allowed before the ban5
findtimeWindow in which failures are counted10m
bantimeLength of the first ban1h

With these values, an address that fails five times within 10 minutes is blocked for one hour.

Fail2ban does not replace SSH keys, a well-tuned firewall or security updates. It adds a layer that slows down repeated attempts.


Install Fail2ban on Debian and Ubuntu

Each distribution ships its own version of Fail2ban, with different defaults.

SystemFail2ban versionLog read for SSHDefault firewall
Debian 121.0.2/var/log/auth.log, missing on a fresh installiptables
Debian 131.1.0systemd journalnftables
Ubuntu 22.040.11.2/var/log/auth.logiptables
Ubuntu 24.041.0.2systemd journalnftables
Ubuntu 26.041.1.0systemd journalnftables

Install the package along with nftables and the Python module for systemd.

sudo apt update
sudo apt install -y fail2ban nftables python3-systemd
sudo systemctl enable --now fail2ban

The nftables package provides the nft command, which Fail2ban calls to ban. On Ubuntu 24.04, Fail2ban bans with nftables by default, but that package is not always there. It does not turn on any rule by itself. The python3-systemd package lets Fail2ban read the systemd journal.

On Ubuntu 24.04, the original 1.0.2-3 release crashed with Python 3.12. The fixed release, 1.0.2-3ubuntu0.1, comes from the Ubuntu updates. So always run apt update before installing.

Then display the installed version and check that the service responds.

fail2ban-client --version
sudo fail2ban-client ping
sudo fail2ban-client status

The service should reply Server replied: pong. On Debian and Ubuntu, the package turns on the sshd jail out of the box. Check its status anyway.

sudo fail2ban-client status sshd

If the jail is missing or the service refuses to start, move on to the configuration in the next section.

If the service won't start on Debian 12

Since version 12, Debian no longer installs rsyslog by default. SSH events stay in the systemd journal, and /var/log/auth.log does not exist. Fail2ban 1.0.2 still looks for that file and stops with the error Have not found any log file for sshd jail. Debian 13 already reads the systemd journal. On an upgraded server, or if rsyslog was added, the file may still exist.

Check where SSH writes its logs.

test -f /var/log/auth.log && echo "auth.log OK" || echo "auth.log absent"
sudo journalctl -u ssh --since "10 minutes ago" --no-pager

The jail in the next section reads the systemd journal on all these distributions. That settles this case.


Configure the SSH jail

Do not edit /etc/fail2ban/jail.conf. This file belongs to the package and may change with every update. Your settings go in /etc/fail2ban/jail.local, or in a .local file inside /etc/fail2ban/jail.d/.

Fail2ban reads its files in this order. A setting read later overrides the earlier one.

  1. jail.conf

  2. jail.d/*.conf, including defaults-debian.conf, the Debian and Ubuntu defaults

  3. jail.local

  4. jail.d/*.local

Create the local file.

sudo nano /etc/fail2ban/jail.local

Paste this baseline. It suits a Debian or Ubuntu VPS where SSH logs to the systemd journal.

[DEFAULT]
# Length of the first ban
bantime = 1h

# Count failures over 10 minutes
findtime = 10m
maxretry = 5

# Ban repeat offenders for longer
bantime.increment = true
bantime.factor = 1
bantime.maxtime = 1w

# Whitelist, addresses that are never banned
ignoreip = 127.0.0.1/8 ::1

# Ban with nftables
banaction = nftables
banaction_allports = nftables[type=allports]

[sshd]
enabled = true
# Read the systemd journal
backend = systemd
port = ssh

If SSH listens on another port, replace port = ssh with the number you found at the start.

[sshd]
enabled = true
backend = systemd
# Port found with sshd -T
port = 2222

With backend = systemd, do not add a logpath to the jail. Fail2ban reads the journal directly and ignores that setting.

The two banaction lines make Fail2ban ban through nftables everywhere. Debian 13 and Ubuntu 24.04 and 26.04 already do. Debian 12 and Ubuntu 22.04 call iptables by default, a command that is sometimes missing.

Why use incremental bans?

With bantime.increment = true, Fail2ban keeps a history of bans in its local database. Each repeat offence doubles the ban. With these values, it goes from one hour to two hours, then four, and never beyond a week.

A forgotten password therefore never leads to a permanent ban. This does nothing against a botnet that switches address with every attempt.

Does Fail2ban work with UFW?

Yes. Fail2ban adds its own rules next to those of UFW. With the nftables action, they live in a separate table, f2b-table, which is checked before the UFW rules. UFW decides which ports are open, Fail2ban decides which addresses to block. The guide How to set up the UFW firewall on Ubuntu or Debian handles the first part.


Check that Fail2ban works

Always test the configuration before you apply it.

sudo fail2ban-client -t

If no error shows up, reload Fail2ban and check the SSH jail.

sudo fail2ban-client reload
sudo fail2ban-client status sshd
sudo journalctl -u fail2ban -n 50 --no-pager

In the output of status sshd, the Currently failed line counts current failures. Currently banned counts banned addresses, and Banned IP list lists them.

The -t test checks the syntax. It does not prove that the filter recognises your logs, or that the firewall really blocks. Test the SSH filter against the systemd journal.

sudo fail2ban-regex --print-all-matched systemd-journal sshd

If your server still writes to /var/log/auth.log, test that file instead.

sudo fail2ban-regex --print-all-matched /var/log/auth.log sshd

Test a ban without losing access

Use another internet connection, such as your phone's hotspot. Trigger several failed SSH logins from it. Keep your admin session and the console open.

Then check the result.

sudo fail2ban-client status sshd
sudo fail2ban-client banned

Finally, make sure the firewall holds a Fail2ban rule.

sudo nft list ruleset | grep -iE 'f2b|fail2ban'
sudo iptables -S | grep -i f2b

With the nftables action from this guide, only the first command returns a rule. The other one is for setups that kept the iptables action. Unban the phone's address afterwards, as shown below.


List the IPs banned by Fail2ban

Three commands show the state of the bans.

# Active jails
sudo fail2ban-client status

# IPs banned by the SSH jail
sudo fail2ban-client status sshd

# All banned IPs, jail by jail
sudo fail2ban-client banned

Unban an IP with Fail2ban

These commands lift a ban without touching the configuration. The address 203.0.113.42 is an example.

# Unban an IP from one jail
sudo fail2ban-client set sshd unbanip 203.0.113.42

# Unban an IP from every jail
sudo fail2ban-client unban 203.0.113.42

The next command lifts every ban in every jail. Keep it for cases where you really need it.

sudo fail2ban-client unban --all

Ban an IP manually

To block an address you spotted in the logs right away, add it to the jail.

sudo fail2ban-client set sshd banip 198.51.100.17

If you banned yourself

Five wrong passwords are enough to block your address for an hour. Open the console in the client area, as explained at the start of this guide. Log in, then unban your address with unban. If it is static, add it to the whitelist afterwards.


Add an IP to the Fail2ban whitelist

The ignoreip setting holds the Fail2ban whitelist, the addresses it never bans. It accepts IPv4 and IPv6 addresses, CIDR ranges and DNS names. Extend the line in the [DEFAULT] section of jail.local.

[DEFAULT]
# Your static IP and a trusted range (examples)
ignoreip = 127.0.0.1/8 ::1 203.0.113.42 192.0.2.0/24

Check the syntax, reload Fail2ban, then display the whitelist of the SSH jail.

sudo fail2ban-client -t
sudo fail2ban-client reload
sudo fail2ban-client get sshd ignoreip

Only add your address if it is static and belongs to you. A range that is too wide, such as an ISP's or a public VPN's, also shields the attackers who use it.

To exclude an address right away, without a reload, use addignoreip.

# Ignore an IP until the next reload
sudo fail2ban-client set sshd addignoreip 203.0.113.42

# Remove that exception
sudo fail2ban-client set sshd delignoreip 203.0.113.42

This setting lives in memory only. It is gone after the next reload or restart of Fail2ban. To keep it, add the address to ignoreip.


Protect Nginx with Fail2ban

Fail2ban ships several filters for Nginx. Two jails are the most useful.

  • nginx-http-auth bans after repeated HTTP Basic authentication failures.

  • nginx-limit-req bans the addresses that Nginx rate limiting rejects.

Add them to /etc/fail2ban/jail.local.

[nginx-http-auth]
enabled = true
port = http,https
# Read the Nginx log file
backend = auto

[nginx-limit-req]
enabled = true
port = http,https
backend = auto
maxretry = 10

The backend = auto line makes Fail2ban read the Nginx log file. On Ubuntu 24.04, the default reads the systemd journal for every jail. Without this line, these two jails would see nothing.

The nginx-limit-req jail does not create the rate limit. Nginx must already use limit_req_zone and limit_req, and its rejections must show up in its error log. Check this, test the filter, then check the Nginx configuration.

sudo nginx -T | grep -E 'limit_req(_zone)?'
sudo fail2ban-regex /var/log/nginx/error.log nginx-limit-req
sudo nginx -t

Then reload both services.

sudo systemctl reload nginx
sudo fail2ban-client -t
sudo fail2ban-client reload
sudo fail2ban-client status nginx-limit-req

If the web server is not set up yet, follow the guide Install Nginx and PHP-FPM on a Linux VPS.

Nginx behind Cloudflare or a proxy

Behind Cloudflare, a reverse proxy or a load balancer, the Nginx logs hold the proxy's address. Fail2ban may then ban the proxy instead of the visitor. First restore the real address in the logs, with the Nginx set_real_ip_from and real_ip_header directives.

Even then, the VPS firewall still sees traffic coming from the proxy. A ban set through nftables therefore does not stop that visitor. For Cloudflare, Fail2ban ships cloudflare actions that apply the block at Cloudflare, with an API key.


Troubleshoot Fail2ban when it bans nothing

The same causes come up again and again. The jail is inactive, the backend or the log is wrong, the filter does not match the lines, or the firewall action fails. Check them in this order.

Check the service

sudo systemctl --no-pager --full status fail2ban
sudo fail2ban-client ping
sudo journalctl -u fail2ban -n 100 --no-pager

Check the jail

sudo fail2ban-client status
sudo fail2ban-client status sshd

Check where the logs go

sudo journalctl -u ssh -n 50 --no-pager
sudo tail -n 50 /var/log/auth.log

The tail command fails if /var/log/auth.log does not exist. In that case, keep backend = systemd.

Test the filter

sudo fail2ban-regex systemd-journal sshd

If the counters stay at zero despite real failures, check the logged service, the filter, the backend and the port. Compare the output of sshd -T with the port line of the jail.

Check the firewall action

sudo fail2ban-client get sshd actions
sudo nft list ruleset | grep -iE 'f2b|fail2ban'
sudo iptables -S | grep -i f2b

If the Fail2ban log says that nft or iptables cannot be found, install the missing package or point banaction at the firewall you have. Do not switch actions without knowing which firewall runs on the server.


Harden the rest of the VPS

Fail2ban is only one layer. Combine it with the following measures.

  • Log in with an SSH key and turn off password logins. The guide Set up an SSH key on a Linux VPS walks you through it.

  • Refuse direct SSH logins as root.

  • Only open the ports you need, with UFW or nftables.

  • Install security updates and keep an eye on the logs.

The guide Secure a Linux VPS covers all these steps, from SSH to backups.

Does Fail2ban protect against DDoS attacks?

No. Fail2ban acts on connections that have already reached the server. It slows down brute force, not an attack that floods the network. That kind of attack is filtered upstream, before the VPS. The Security tab of the VPS page shows the Detected attacks card of the DDoS protection.

Should you change the SSH port?

Changing the port cuts down the noise from automated scans. It is not real protection, because a full scan finds the new port. SSH keys, disabling passwords and network filtering come first. If you change the port, update the port line of the jail.


Need help?

Open a ticket from Tickets › Open a Ticket, or from the Contact support tile at the bottom of the VPS page. You can also ask your question on the OuiHeberg Discord.

Was this article helpful?

790 views
Share this article
VPS Linux
Need a Linux VPS?

Root access, your choice of distribution and management from the OuiHeberg client area.

View plans →

Related Articles