How it works
sshd ── login attempts ──→ /var/log/auth.log (rsyslog)
│ /var/log volume, read-only
▼
crowdsec (container) crowdsecurity/sshd scenarios: password guessing, user enumeration,
│ slow brute force → decision "ban 90d"
│ LAPI on 127.0.0.1:8080
▼
crowdsec-firewall-bouncer (host) pulls decisions every 10 s
│
▼
nftables: crowdsec and crowdsec6 tables, input and forward hooks
packets from the address are dropped on every port
Detection and enforcement are separate. CrowdSec only decides whom to ban — it blocks nothing itself. The bouncer blocks. That way one CrowdSec can feed both the firewall and a Traefik plugin (CrowdSec with Traefik behind Cloudflare).
Besides its own decisions, CrowdSec gets the community blocklist (CAPI): addresses that attacked other servers running CrowdSec. The bouncer bans those too — before they knock on yours.
Other ways to stop brute force
| With | How it works | When to use |
|---|---|---|
| CrowdSec + firewall-bouncer | scenarios over logs, a ban in nftables on every port, the community blocklist | an internet-facing server with sites and SSH — this manual’s setup |
| fail2ban | regexes over logs, bans through iptables, nftables or ufw | a single server without Docker; a familiar tool, no external service |
PerSourcePenalties in sshd |
sshd itself pushes back an address that fails to log in |
Ubuntu 26.04 (OpenSSH 10.2): on by default, SSH only |
ufw limit |
at most 6 connections per 30 s from an address | nothing to install; gets in the way of your own scripts (The ufw firewall) |
With key-only login (sshd_config) guessing is useless anyway. The ban is for something else: an address that probes SSH usually goes on to scan the sites, and the community blocklist cuts it off in advance.
Setup
Docker is already on the server (Docker on Ubuntu).
The CrowdSec stack
/opt/crowdsec/docker-compose.yaml services: crowdsec: image: crowdsecurity/crowdsec:latest container_name: crowdsec restart: unless-stopped mem_limit: 256M memswap_limit: 384Mmem_limitcaps the container’s memory,memswap_limitcaps memory plus swap. Without a cap, big logs and many scenarios make CrowdSec eat hundreds of megabytes, which shows on a 1–2 GB VPS.environment: COLLECTIONS: "crowdsecurity/linux crowdsecurity/sshd"Which scenario bundles to download at start:
linuxparses syslog,sshdbrings the SSH brute force scenarios.volumes: - ./data/config:/etc/crowdsec - ./data/db:/var/lib/crowdsec/data - /var/log:/var/log:roKey partEverything CrowdSec writes lives in the stack’s
data/:configholds the config and the LAPI and CAPI keys,dbthe decision database. CrowdSec reads the host’s/var/logbut can’t change it (:ro). Only your own files fromdata/go into git — sources and profiles (Server configs in git).ports: - "127.0.0.1:8080:8080"LAPI on
127.0.0.1:8080: the bouncer on the host talks to it, and there’s no port from outside (A host-only port goes on 127.0.0.1).The source: auth.log
The image puts a placeholder with the path
/does/not/existintoacquis.yaml. Real sources go in separate files inacquis.d/./opt/crowdsec/data/config/acquis.d/auth.yaml filenames: - /var/log/auth.log labels: type: syslogWhich file to read and with which parser:
type: syslogmeans syslog lines, and thesshdscenarios take login attempts from them.The
data/configdirectory appears after the first start, so add this file after step 4 and restart the container.Ban duration
/opt/crowdsec/data/config/profiles.yaml name: default_ip_remediation filters: - Alert.Remediation == true && Alert.GetScope() == "Ip" decisions: - type: ban duration: 90d on_success: breakKey partBan an address for 90 days instead of the default 4 hours: an address that was guessing SSH won’t become harmless in a week. The duration is written into the decision when it’s created, so an edit affects only new bans.
--- name: default_range_remediation filters: - Alert.Remediation == true && Alert.GetScope() == "Range" decisions: - type: ban duration: 90d on_success: breakThe second profile does the same for subnets, when a decision covers an address range.
The file replaces the image’s
profiles.yamlentirely. If your own address gets banned, remove the ban by hand.Start it
cd /opt/crowdsec && docker compose up -d docker logs crowdsec 2>&1 | tail -5The first start takes about a minute: CrowdSec downloads the collections and registers with CAPI.
Install the bouncer on the host
The package comes from CrowdSec’s repository. The script adds the repository and the signing key and installs nothing itself:
curl -s https://install.crowdsec.net | sudo sh sudo apt install crowdsec-firewall-bouncer-nftablesThe commands follow the CrowdSec documentation as of 2026-10-18; on production servers the bouncer was installed earlier, and the steps weren’t repeated from scratch.
The bouncer runs on the host, not in a container: it needs access to the host’s nftables.
Give the bouncer a key
docker exec crowdsec cscli bouncers add firewall-bouncer # API key for 'firewall-bouncer': # # Kx9… # # Please keep this key since you will not be able to retrieve it!The key is shown once. Put it in the bouncer’s config without leaving it in shell history: open the file in an editor instead of using
echoon the command line.sudo nano /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yamlmode: nftables api_url: http://127.0.0.1:8080/ api_key: Kx9… update_frequency: 10ssudo chmod 600 /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml sudo systemctl restart crowdsec-firewall-bouncerCheck the hooks
sudo nft list table ip crowdsec | grep hook # type filter hook input priority filter - 10; policy accept; # type filter hook forward priority filter - 10; policy accept;Both hooks,
inputandforward. Withoutforwarda ban doesn’t cover containers with published ports (Docker ports bypass the input chain). If theforwardhook is missing, look atnftables_hooksin the same yaml. The config shipped with the package listsinputandforward; if the line is missing altogether, the bouncer uses onlyinput. The bouncer’s documentation doesn’t mention this setting — only its config and code do.
If it doesn’t work
cscli metrics shows no lines from auth.log
- Cause
- The container can’t see the file: no
/var/log:/var/log:rovolume, or the source isn’t inacquis.d/. Sometimes there’s noauth.logat all — on a server withoutrsyslog, login events exist only in the systemd journal. - Fix
docker exec crowdsec ls -l /var/log/auth.log— the file must be there. Missing on the host —sudo apt install rsyslog: the regular Ubuntu image has it, minimal images and some hosting providers don’t.
The bouncer won’t start; journalctl -u crowdsec-firewall-bouncer shows a key error or 403
- Cause
api_keyincrowdsec-firewall-bouncer.yamlis empty or wrong. The package registers a key itself only when CrowdSec runs on the host; with CrowdSec in a container, write the key in by hand.- Fix
docker exec crowdsec cscli bouncers add firewall-bouncer, put the key intoapi_key, thensudo systemctl restart crowdsec-firewall-bouncer.
Banned your own address; SSH gives Connection timed out
- Cause
- Several failed logins in a row: the wrong key, or
sshwent through every key in the agent. - Fix
- From your phone on mobile data or through the provider console:
docker exec crowdsec cscli decisions delete --ip <address>. For next time —IdentitiesOnly yes(~/.ssh/config).
Ban decisions exist, but the address still connects
- Cause
- The bouncer doesn’t pull decisions (
cscli bouncers listshows an oldLast API pull), or it sits only on theinputhook while the address reaches a container throughforward. - Fix
sudo systemctl status crowdsec-firewall-bouncerandsudo nft list table ip crowdsec | grep hook— you need both hooks (Docker ports bypass the input chain).
The CrowdSec container eats hundreds of megabytes and keeps growing
- Cause
- Without a limit the container’s memory is unbounded; with big logs and many scenarios that shows on a 1–2 GB VPS.
- Fix
mem_limit: 256Min compose — that’s what production servers use.
How to check
CrowdSec reads auth.log and parses the lines:
docker exec crowdsec cscli metrics show acquisition
# │ file:/var/log/auth.log │ 1.24k │ 1.24k │ - │ - │
The numbers in the first two columns are lines read and parsed. Zeros mean the source doesn’t work.
The bouncer is connected:
docker exec crowdsec cscli bouncers list
# Name IP Address … Last API pull Type
# firewall-bouncer 172.17.0.1 … 2026-10-18T19:41:02Z crowdsec-firewall-bouncer
Last API pull should be seconds ago.
Test the ban by action, from a phone on mobile data. Its address shows on ifconfig.me. Ban it for 2 minutes: if you got the address wrong, the ban lifts itself.
docker exec crowdsec cscli decisions add --ip <phone address> --duration 2m --reason test
# from the phone: SSH and the sites on the server time out
docker exec crowdsec cscli decisions delete --ip <phone address>
Who’s banned already: docker exec crowdsec cscli decisions list; the other commands — cscli and bouncers.