Skip to content

SecurityFor beginnersDuration around 10 min.

How to set up fail2ban: protect SSH on Ubuntu against password guessing

fail2ban monitors the login attempts on your server and bans addresses that try an incorrect password too often. Following this guide, it protects SSH access with custom values for ban duration and failed attempts, and you will know how to check and lift a ban.

Tested on with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS

At a glance

  • On Ubuntu, the jail for SSH is already active after installation; without a custom file, fail2ban bans for ten minutes after five failed attempts within ten minutes.
  • Custom values belong in the jail.local file โ€” a package update overwrites jail.conf.
  • Enter your own static address in ignoreip so you do not lock yourself out.

Prerequisites

  • A server with Ubuntu 24.04 LTS or 26.04 LTS, a user with sudo privileges and an SSH connection โ€” such as a VPS, a Cloud Server or a Dedicated Server.
  • A second way onto the server in case you lock yourself out: for a VPS, this is the KVM console in the customer area.

Step 1: install fail2ban

Install fail2ban package
sudo apt update
sudo apt install -y fail2ban

The Ubuntu package includes the /etc/fail2ban/jail.d/defaults-debian.conf file. It enables the sshd jail, reads the logins from the systemd journal and bans via nftables. Without further specification, the defaults from jail.conf apply: five failed attempts within ten minutes result in a ten-minute ban.

Step 2: define custom values in jail.local

Never modify jail.conf yourself โ€” a package update replaces the file. Your values go in jail.local; fail2ban reads them after the defaults and lets them take precedence.

Create file
sudo nano /etc/fail2ban/jail.local
/etc/fail2ban/jail.local
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
ignoreip = 127.0.0.1/8 ::1
 
[sshd]
enabled = true
backend = systemd
  • bantime: how long an address remains banned, here one hour.
  • findtime and maxretry: five failed attempts within ten minutes trigger the ban.
  • ignoreip: addresses that are never banned. Append your own static address separated by a space.

Step 3: check configuration and restart fail2ban

fail2ban-client -t reads all files and reports typos before the service loads them. Afterwards, you enable the service permanently and restart it so it reads the new file.

Check, enable, restart
sudo fail2ban-client -t
sudo systemctl enable --now fail2ban
sudo systemctl restart fail2ban
Output of the check
OK: configuration test is successful

Step 4: view status and banned addresses

Query status
sudo fail2ban-client status
sudo fail2ban-client status sshd

The second command shows how many failed attempts were counted and which addresses are currently banned. This is what it looked like in the test after six incorrect logins from 203.0.113.7; the third line under "Filter" names the read source and depends on your system:

Output of fail2ban-client status sshd
Status for the jail: sshd
|- Filter
|  |- Currently failed:	1
|  |- Total failed:	6
|  `- โ€ฆ
`- Actions
   |- Currently banned:	1
   |- Total banned:	1
   `- Banned IP list:	203.0.113.7

Step 5: unban an address

You can lift a ban manually before it expires; the second command shows the current ban duration in seconds.

Unban and check ban duration
sudo fail2ban-client set sshd unbanip 203.0.113.7
sudo fail2ban-client get sshd bantime

fail2ban does not replace SSH keys

fail2ban slows down guessing, but does not prevent it. The actual protection comes from logging in only with SSH keys and disabled passwords. After that, fail2ban mainly keeps the logs clean.

Typical errors

  • The service does not start: Usually a typo in jail.local. fail2ban-client -t names the line; the service log names the reason.
  • You have banned yourself: Sign in via the server console, lift the ban with unbanip and enter your address in ignoreip.
  • "WARNING 'allowipv6' not defined" on Ubuntu 24.04: A notice from Fail2Ban 1.0.2 without consequences; the check still reports success.
  • Nothing is counted: The jail reads the systemd journal. sudo journalctl -u ssh --since today shows whether login attempts arrive there.
Read fail2ban log
sudo journalctl -u fail2ban --since today

We tested all commands in this guide on 2 December 2026 โ€” with Ubuntu 24.04 LTS and Ubuntu 26.04 LTS. If a step changes at the manufacturer, we update it here along with the date.

Official documentation

Frequently asked questions about fail2ban

What does fail2ban do?

fail2ban monitors logs of services like SSH, counts failed logins per address and bans an address for a specified time in the firewall as soon as it fails too often.

Is fail2ban already active on Ubuntu after installation?

Yes. The package enables the jail for SSH via the defaults-debian.conf file. Without custom values, it bans for ten minutes after five failed attempts within ten minutes.

Why jail.local and not jail.conf?

jail.conf belongs to the package and is replaced during an update. fail2ban reads jail.local afterwards, so your values override the defaults and survive an update.

Does fail2ban also protect services other than SSH?

Yes. jail.conf contains prepared jails for web servers, mail servers and other services. You enable one of them by creating a section with its name and enabled = true in jail.local.

Is fail2ban compatible with ufw?

Yes. The Ubuntu package blocks via its own rules in nftables; the ufw rules remain unaffected and continue to apply.

Further reading

More tutorials

  • How to create an SSH key and add it to your server

    SecurityFor beginnersabout 10 min

    An SSH key allows you to sign in without a password, and no one can guess your password. This tutorial generates a key on your machine, adds it to the server, and finally disables password authentication.

  • How to install Docker on Ubuntu: Docker Engine and Compose

    ContainerFor beginnersabout 10 min

    By the end of this tutorial, Docker Engine from the official Docker repository will be running on your Ubuntu server, along with the Compose plugin and a first service. You need a server with Ubuntu 24.04 LTS or 26.04 LTS and a user with sudo privileges.

  • How to set up Nginx as a reverse proxy with HTTPS (Letโ€™s Encrypt)

    Web serverFor beginnersabout 20 min

    Following this tutorial, Nginx will accept requests to your domain, pass them to an application on the same server, and serve everything over HTTPS with a Letโ€™s Encrypt certificate. This is suitable for Node.js, Python, Go, or a container listening on a local port.

A server with SSH access

You access VPS and Cloud Servers via SSH with your key; you set up fail2ban on them as described here.