fail2ban or CrowdSec: block SSH brute force on a Linux server
Every public SSH server receives login attempts from bots around the clock. fail2ban and CrowdSec read the logs, notice addresses that fail again and again, and block them in the firewall for a while. How much that matters depends on one setting: whether your SSH server accepts passwords.
Do you need it?
- SSH accepts passwords: yes. Better still, switch to keys (key-only login); until then, blocking repeated failures slows guessing down.
- SSH accepts keys only: guessing cannot succeed, so blocking mainly keeps the logs short and saves a little CPU. It is optional.
- Other services with logins (mail, a web admin, a VPN with passwords): yes, 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
fail2ban is packaged by every common distribution (on RHEL, Rocky and Alma through EPEL), and it only acts on the local server.
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)
What the packages turn on by default differs (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. The configuration below sets it for 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
[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. |
On a server that uses ufw, add banaction = ufw to [DEFAULT], so bans show up in ufw status.
Test the configuration, then start the service:
sudo fail2ban-client -t
sudo systemctl enable --now 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 and Rocky Linux 10, and with banaction = ufw on both Debian releases.
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 works in 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, an old release. The CrowdSec project recommends its own repository:
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
Useful commands:
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.
Patchbrief 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 - 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 agent of Patchbrief reports the packages and settings of a server once a day; the digest names what to fix first.