fail2ban or CrowdSec: block SSH brute force on a Linux server
A public SSH server gets login attempts from bots around the clock. fail2ban and CrowdSec read the logs and block addresses that fail again and again in the firewall for a while. They help most on an SSH server that accepts passwords.
Do you need it?
| Setup | Blocking |
|---|---|
| SSH accepts passwords | Needed. Better still, switch to keys (key-only login); until then, blocking repeated failures slows guessing down. |
| SSH accepts keys only | Optional. Guessing cannot succeed, so blocking mainly keeps the logs short and saves a little CPU. |
| Other services with logins (mail, a web admin, a VPN with passwords) | Needed for those services. Both tools have filters for common software. |
OpenSSH 9.8 and later also have a built-in penalty (PerSourcePenalties, on by default): an address that
fails to log in again and again is refused for a while. Debian 13, Rocky Linux 9.9 and 10.3, and Alpine 3.24
have it; Debian 12 and Ubuntu 24.04 ship older OpenSSH releases without it. Check with:
sudo sshd -T | grep persourcepenalties
fail2ban
Every common distribution packages fail2ban (RHEL, Rocky and Alma through EPEL). It acts on the local server only.
Install
sudo apt install fail2ban python3-systemd # Debian, Ubuntu
sudo dnf install epel-release
sudo dnf install fail2ban-server fail2ban-firewalld python3-systemd # RHEL, Rocky, Alma
sudo apk add fail2ban # Alpine (then rc-update add fail2ban)
On RHEL itself, epel-release is not in the repositories; install it with
sudo dnf install https://dl.fedoraproject.org/pub/epel/epel-release-latest-9.noarch.rpm (latest-8 on
RHEL 8).
Package defaults as of November 2026:
| Release | fail2ban | SSH jail on after install | Log source | Ban action |
|---|---|---|---|---|
| Debian 12 | 1.0.2 | yes | log file by default | iptables |
| Debian 13 | 1.1.0 | yes | systemd journal | nftables |
| Ubuntu 24.04 | 1.0.2 | yes | systemd journal | nftables |
| Rocky Linux 9 and 10 (EPEL) | 1.1.0 | no | log file by default | firewalld |
| Alpine 3.24 | 1.1.0 | yes | /var/log/messages |
iptables |
Debian 12 does not write /var/log/auth.log unless rsyslog is installed, so the SSH jail needs the journal
there, and its iptables ban action fails when iptables is missing: apt installs nftables with fail2ban, not
iptables. The firewalld action of EPEL bans only while firewalld runs (sudo firewall-cmd --state). The
configuration below reads the journal and bans with nftables on all systemd distributions.
Configure
Do not edit jail.conf; package updates replace it. Put your settings in /etc/fail2ban/jail.local:
[DEFAULT]
bantime = 1h
findtime = 10m
maxretry = 5
bantime.increment = true
ignoreip = 127.0.0.1/8 ::1 198.51.100.7
banaction = nftables
banaction_allports = nftables[type=allports]
[sshd]
enabled = true
backend = systemd
| Setting | Meaning |
|---|---|
maxretry within findtime |
5 failures in 10 minutes lead to a ban. The upstream defaults are 5 and 10 minutes. |
bantime |
How long a ban lasts. The upstream default is 10 minutes. |
bantime.increment |
Repeat offenders get longer bans each time. |
ignoreip |
Addresses that are never banned. Add the fixed address of your office or VPN, so a typo does not lock you out. |
backend = systemd |
Read sshd messages from the journal instead of a log file (needs python3-systemd). Leave it out on Alpine. |
banaction, banaction_allports |
Ban in nftables, as Debian 13 and Ubuntu 24.04 do by default. Leave both lines out on Alpine, where the package bans with iptables. |
On a server that uses ufw, write banaction = ufw instead and delete the banaction_allports line, so bans
show up in ufw status.
Test the configuration, then start the service. Debian and Ubuntu start fail2ban during the installation, so a
restart is needed to read jail.local:
sudo fail2ban-client -t
sudo systemctl enable fail2ban && sudo systemctl restart fail2ban # Debian, Ubuntu, RHEL family
sudo rc-update add fail2ban && sudo rc-service fail2ban start # Alpine
fail2ban-client -t ends with OK: configuration test is successful. The file passed this test on Debian 12,
Debian 13, Ubuntu 24.04, Rocky Linux 8.10 and 9.9, and with banaction = ufw on Debian 12 and 13 and Ubuntu
24.04. With the two banaction lines, a ban of a test address landed in nftables on Debian 12 and Rocky Linux
9.9.
To see that bans reach the firewall, ban a documentation address and look for it:
sudo fail2ban-client set sshd banip 192.0.2.1
sudo nft list ruleset | grep 192.0.2.1
sudo fail2ban-client set sshd unbanip 192.0.2.1
Daily use
sudo fail2ban-client status sshd # failures and banned addresses
sudo fail2ban-client set sshd unbanip 203.0.113.5 # unban one address
If you lock yourself out, log in from another address (a phone hotspot is enough) or through the console of the provider, and unban your address.
CrowdSec
CrowdSec has two parts. The security engine reads logs and detects attacks with scenarios (for SSH, the
collection crowdsecurity/sshd). A remediation component, called a bouncer, applies the decisions, for
example in nftables. By default, the engine sends signals about attacks (the address, the scenario, a
timestamp) to the Central API of CrowdSec, and the shared community blocklist is built from these signals.
Decide whether you want that before you install it; the documentation describes what is sent and how to
turn parts of it off.
Debian 12, Debian 13 and Ubuntu 24.04 package CrowdSec 1.4.6. The CrowdSec project recommends its own repository, which has current releases:
curl -fsSL https://install.crowdsec.net -o crowdsec-repo.sh
less crowdsec-repo.sh # the script adds the package repository
sudo sh crowdsec-repo.sh
sudo apt install crowdsec # RHEL family: sudo dnf install crowdsec
sudo apt install crowdsec-firewall-bouncer-nftables
Day to day:
sudo cscli metrics # what the engine reads and detects
sudo cscli decisions list # current bans
sudo cscli decisions delete --ip 203.0.113.5
Which one
| fail2ban | CrowdSec | |
|---|---|---|
| Packages | In the distribution archives (EPEL for RHEL) | Own repository recommended |
| Scope | Local only | Local, plus a shared blocklist through the Central API |
| Configuration | INI files, regex filters | YAML scenarios and collections, cscli |
| Data sent elsewhere | Nothing | Signals to the Central API by default |
For a few servers with SSH on keys only, fail2ban with the configuration above, or the built-in penalties of a recent OpenSSH, is enough. CrowdSec fits better when several servers or services should share what they see.
Vulnpocalypse notes on each server whether fail2ban or CrowdSec runs, and raises it when SSH also accepts passwords.
Sources
- fail2ban jail.conf: defaults of
bantime,findtime,maxretry,bantime.increment,ignoreip; usejail.local - OpenSSH 9.8 release notes:
PerSourcePenalties - Test runs on 2026-11-26 with
sshd -T: Rocky Linux 9.9 (OpenSSH 9.9p1), Debian 13 (10.0p1) and Alpine 3.24 (10.3p1) havePerSourcePenaltieson; the sshd of Debian 12 (9.2p1) and Ubuntu 24.04 (9.6p1) rejects the option - Rocky Linux 10 BaseOS packages: openssh-server 9.9p1-34.el10_3
- Test runs on 2026-11-27 in containers: the package defaults of Debian 12 (fail2ban 1.0.2-2, nftables installed, iptables not), Debian 13 (1.1.0-8), Ubuntu 24.04 (1.0.2-3ubuntu0.1), Rocky Linux 9.9 (EPEL 1.1.0-6.el9,
00-firewalld.conf) and Alpine 3.24 (1.1.0-r3, depends on iptables); bans of a test address that failed withiptables: not found(Debian 12 defaults) and without a running firewalld (Rocky Linux 9.9), and landed in nftables with the twobanactionlines - CrowdSec: install on Linux: repository setup and packages
- CrowdSec: firewall bouncer: nftables and iptables packages
- CrowdSec: Central API: what the engine shares
Check your servers every day
The Vulnpocalypse collector reports the packages and settings of a server once a day, and the digest lists what to fix, most urgent first.
Vulnpocalypse