CrowdSec in a container spots SSH brute force in auth.log, and a bouncer on the host bans the address in nftables on every port, containers included. Then check from outside that only 22, 80 and 443 are open. The why is in the manuals, linked from every stage.
Start froma server after level 1, “Base”: key login as example-user, ufw lets in SSH, 80 and 443, Docker installed with capped logs.
What you get
sshd ──▶ /var/log/auth.log ──▶ crowdsec (container) ── decision “ban 90d” │ LAPI on 127.0.0.1:8080 ▼ crowdsec-firewall-bouncer (host) ──▶ nftables: input and forward hooks/opt/crowdsec/ the stack: compose, data/config — sources and profiles, data/db — decisionsoutside only 22, 80 and 443
You’ll needA phone on mobile data: it has a different address, so it’s where you test the ban from
You’ll neednmap on your computer to check ports from outside; on Windows, the installer from nmap.org
Your values
Filled into the commands and files of this and other recipes on the path; stored in this browser only. Highlighted parts come from “Your values”.
Server address
203.0.113.10placeholder
User
example-userplaceholder
Without the script the commands show placeholder values: replace them with yours.
Steps
The same recipe, short: commands from the stages, files as one line. For a second server and for those who already know what each command does.
ifconfig.me shows the phone’s address on mobile data. Ban it for 2 minutes: if you got the address wrong,
the ban lifts by itself (Test your server’s protection by action). Replace <phone-address> in the command with it.
bans the phone’s address; the bouncer picks up the decision within 10 s
On the phone, open http://203.0.113.10 in Chrome. Without a ban it says ERR_CONNECTION_REFUSED right
away: port 80 is open, but nothing listens on it yet. With the ban the page keeps loading with no answer:
the packets are dropped.
Done when
Command
What you should see
sudo docker exec crowdsec cscli decisions list
the phone’s address, test in reason
sudo nft list table ip crowdsec | grep -c <phone-address>
not zero: the address is in the bouncer’s table
Not like this — see “If it doesn’t work”, stage cases.
After 2 minutes the ban lifts by itself, and Chrome on the phone says ERR_CONNECTION_REFUSED right away again.
A port only the server needs listens on 127.0.0.1 (A host-only port goes on 127.0.0.1); reach it through an SSH tunnel
(SSH tunnels). That’s why CrowdSec’s LAPI on 127.0.0.1:8080 isn’t visible from outside.
22/tcp open; 80/tcp and 443/tcp are closed: ufw lets them through, but nothing listens there yet; the rest are filtered
Not like this — see “If it doesn’t work”, stage cases.
If it doesn’t work
Stage 1
cscli metrics show acquisition shows no auth.log line
Cause
The file isn’t in the container: compose has no /var/log volume, or there’s no auth.log at all — minimal images without rsyslog keep logins only in the systemd journal.
Fix
sudo docker exec crowdsec ls -l /var/log/auth.log. Not on the host either — sudo apt install rsyslog and sudo docker compose restart.
Stage 1
The auth.log line is there, but the numbers are zeros
Cause
CrowdSec reads the file from the end: it doesn’t count old lines, and there have been no login attempts since the restart.
Fix
Wait a few minutes — bots knock on SSH all the time — or log in yourself from a new window.
Stage 1
Bind for 127.0.0.1:8080 failed: port is already allocated
Cause
Another service on the server already holds port 8080.
Fix
sudo ss -tlnp | grep 8080 shows which. Pick another port — 127.0.0.1:8081:8080 in compose and api_url: http://127.0.0.1:8081/ in the bouncer config.
Stage 2
cscli says firewall-bouncer already exists, and api_key in the config is empty
Cause
The key command ran a second time: the name is taken, so no new key was issued.
Fix
sudo docker exec crowdsec cscli bouncers delete firewall-bouncer, then the commands from the “Key” table again.
Stage 2
The bouncer won’t start; journalctl -u crowdsec-firewall-bouncer shows a key error or 403
Cause
api_key is empty or wrong: the package registers a key by itself only when CrowdSec runs on the host, and here it’s in a container.
Fix
The commands from the “Key” table; if the name is taken, see the case above.
Stage 2
nft list table ip crowdsec | grep hook shows only input
Cause
The bouncer config lacks forward in nftables_hooks: without it the ban doesn’t cover containers with published ports (Docker ports bypass the input chain).
Fix
In /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml set nftables_hooks to input and forward, then sudo systemctl restart crowdsec-firewall-bouncer.
Stage 3
The ban decision is there, but Chrome on the phone still says ERR_CONNECTION_REFUSED right away
Cause
The bouncer isn’t pulling decisions: cscli bouncers list shows an old Last API pull. Or the ban hit the wrong address: the phone has two, IPv4 and IPv6.
Fix
sudo systemctl status crowdsec-firewall-bouncer; check the address against ifconfig.me in the phone’s browser and ban that one.
Stage 3
You banned your own address: SSH from your computer says Connection timed out
Cause
Several failed logins in a row: the wrong key, or ssh went through every key in the agent.
Fix
From the phone on mobile data or the provider’s console: sudo docker exec crowdsec cscli decisions delete --ip <address>. For the future, IdentitiesOnly yes (~/.ssh/config).
A stopped bouncer removes its tables: sudo nft list tables shows no crowdsec or crowdsec6.
If the bouncer crashed instead of stopping (kill -9), the tables and their bans stay — delete them by hand:
sudo nft delete table ip crowdsec, sudo nft delete table ip6 crowdsec6. The CrowdSec repository
stays in apt — it installs nothing on its own.