How to Install and Configure Fail2ban for SSH and Nginx on Ubuntu

This guide shows how to install fail2ban ubuntu ssh protection plus Nginx jails, so repeated failed logins earn an automatic ban instead of endless retries. Fail2ban watches log files, counts failures from each address, and adds a firewall ban when an address crosses the limit. It runs quietly, costs almost no resources, and turns the daily brute-force noise in your logs into blocked addresses.

Fail2ban is a complement to good login hygiene, not a replacement for it. It does not fix password login, and it does not patch anything. Pair it with key-only login, a firewall, and updates. Used that way, it removes the background hammering that fills logs and wastes connection slots, while real protection still comes from keys and closed ports.

Install Fail2ban Ubuntu SSH: Setup Steps

Install Fail2ban from the system packages, then confirm the service runs. On Ubuntu, the default setup ships with an SSH jail defined but sometimes inactive until you enable it in your own config. Never edit the shipped defaults file directly, since package updates overwrite it. Instead, create your own local config file (jail.local) with only the settings you change, which survives updates and stays readable.

sudo apt update
sudo apt install fail2ban
sudo systemctl enable fail2ban
sudo systemctl start fail2ban

After install, check the service status and list active jails. The SSH jail should appear once enabled. If it does not, your local config likely missed the enable flag, which is the most common first-run mistake. Our Fail2ban setup guide for SSH brute-force protection covers jail settings and ban times in more depth if you want to tune beyond the defaults.

Configure the SSH Jail

In your local config, enable the SSH jail, set the maximum retries low, such as five failures, and set a ban time of an hour or more for repeat offenders. Short find times catch fast guessing scripts, while longer ban times keep persistent bots out for the rest of the day. Keep the ignore list short: add only your own static addresses, and never add a whole shared network you do not control, since one bad actor on it would bypass the jail for everyone else.

A fail2ban sshd configuration lives or dies on log paths. The jail must read the same authentication log the SSH service writes to, or it bans nobody while looking healthy. After enabling the jail, confirm it shows a log path and a currently-failed count, then test with a few failed logins from an address you control. You should see the failure count rise and, past the limit, the address banned. Test bans from a second address, never from your only admin connection, so a misconfiguration cannot lock you out.

Add an Nginx Jail

SSH is the first jail, but web attacks deserve one too. A fail2ban nginx jail watches the Nginx error log for repeated bad requests, such as password guessing against login pages, scanning for known vulnerable paths, and bot patterns that hammer forms. Enable the Nginx jail that matches your setup: one pattern covers bad bots, another covers repeated authentication failures on protected pages. Start with one jail, watch its bans for a week, and add more only when the log shows a pattern worth banning.

For WordPress sites, the login page draws its own attacks, and Fail2ban pairs with plugin-level protections. Our guide to disable XML-RPC to stop WordPress login brute-force attacks removes one abused login path entirely, which cuts the attempts Fail2ban must handle. For the wider WordPress hardening picture, our WordPress security basics for VPS owners puts Fail2ban next to updates, permissions, and login limits.

Check Bans, Unban, and Tune

Checking status is a daily habit worth building. The client command shows each jail, its banned addresses, and its failure counts. Unbanning your own address after a testing mistake takes one command with the jail name and the address, so keep that command in your runbook. Review banned addresses weekly: a ban list full of one country or one network tells you where the pressure comes from, and a ban on your own office range tells you a client is misconfigured and retrying.

sudo fail2ban-client status sshd
sudo fail2ban-client set sshd unbanip 203.0.113.10

That example address is a documentation address, not a real ban; substitute the real address when you unban. Tuning means adjusting retries, find time, and ban time to your traffic. Too strict, and traveling admins with flaky connections earn bans. Too loose, and bots get hundreds of guesses before the ban lands. Five retries within ten minutes with an hour ban is a sane starting point for SSH, tightened only if logs show bots adapting.

Fail2ban and the Firewall Together

Fail2ban bans through the firewall, so the two tools must agree. Confirm the ban action matches your firewall setup: a ban action written for one firewall backend does nothing useful on another. On an Ubuntu server running UFW, use the matching action and confirm a test ban actually appears in the firewall rules. Our guide to set up a firewall with UFW on Ubuntu Server 24.04 covers the base rules Fail2ban builds on, and the order matters: firewall first, Fail2ban second, so bans land on a working rule set.

Also confirm Fail2ban starts at boot and survives restarts. A server that reboots without Fail2ban hands bots a free window until someone notices, and that window often lasts until the next log review. Enable the service, test a reboot during a maintenance window, and confirm the jails return active.

Limits Worth Knowing

Fail2ban cannot stop distributed attacks that spread guesses across thousands of addresses, since each address stays under the limit. It cannot protect logins that succeed, so a leaked key or password bypasses it entirely. It also cannot judge content, so it never replaces a web application firewall for SQL injection or cross-site scripting attempts. Treat it as one layer: keys for authentication, firewall for ports, Fail2ban for repeat guessing, updates for known flaws. No single layer carries the server.

The Quiet Log Test

A working Fail2ban setup shows itself in a quieter authentication log: bursts of failures end in a ban line, then silence from that address. Check the log a week after setup and compare with the week before. Fewer repeated attempts, a sane ban list, and no bans on your own addresses mean the setup works. Keep it running, review it monthly, and let it do the boring blocking while you work on the layers that need judgment.

Ditulis oleh

Firdaus Safari Putra

Founder, PT FirKaizen Digital Solutions

Firdaus Safari Putra adalah founder PT FirKaizen Digital Solutions. Ia membangun dan mengoperasikan infrastruktur server di balik panduan ini, dan menulis berdasarkan apa yang benar-benar ia jalankan, uji, dan ukur — bukan teori.