fail2ban ssh protection

Configure Fail2Ban for SSH on Ubuntu

Fail2Ban can monitor failed SSH login attempts and temporarily block IP addresses that repeatedly fail authentication.

Open the Configuration File

Edit the local Fail2Ban configuration:

sudo nano /etc/fail2ban/jail.local

Configure the SSH Jail

Add or update the [sshd] section:

[sshd]
enabled = true
port = 1992
filter = sshd
logpath = /var/log/auth.log
maxretry = 3
bantime = 3600
findtime = 600

Change 1992 to the SSH port used by the server.

The important part is that settings such as:

enabled = true

must belong to a valid section such as:

[sshd]

Do not leave configuration options outside a section header, as this can prevent Fail2Ban from loading correctly.

What These Settings Mean

  1. enabled = true — enables SSH protection.
  2. port = 1992 — SSH port to monitor.
  3. filter = sshd — uses the built-in SSH detection rules.
  4. logpath = /var/log/auth.log — authentication log to monitor.
  5. maxretry = 3 — bans an IP after 3 failed attempts.
  6. findtime = 600 — counts failures within a 10-minute period.
  7. bantime = 3600 — bans the IP for 1 hour.

Save the Configuration

In Nano:

Ctrl + O
Enter
Ctrl + X

Restart Fail2Ban

Apply the configuration:

sudo systemctl restart fail2ban

Check that the service is running:

sudo systemctl status fail2ban

The service should show:

Active: active (running)

Check the SSH Jail

Check whether the SSH jail is active:

sudo fail2ban-client status sshd

To see all enabled jails:

sudo fail2ban-client status

The SSH status output includes information such as currently failed attempts, currently banned IP addresses, and the total number of bans.

Test the Configuration

Before restarting Fail2Ban after larger configuration changes, the configuration can be checked with:

sudo fail2ban-client -t

If the configuration is valid, Fail2Ban should report that the configuration test was successful.

Contact

Want to get in touch?

If you want to talk about a project, a software or IT role, something you have built, or a system you want to improve, send me a message.

I read the messages myself.

Thank you. Your message has been sent.