Introduction: Why Fail2Ban is Dead
We have used Fail2Ban for a decade. It was great, but the modern
internet is too aggressive for it. Fail2Ban is reactive—it only blocks an IP
after they have already attacked you. It's like locking the door after the thief is
inside.
Enter CrowdSec. Think of it as the "Waze of Cyber
Security." If a hacker attacks a server in Germany, CrowdSec sees it, marks the IP
as malicious, and your server in ServerMO instantly blocks it—before they even try to touch
you. This is proactive defense.
Architecture: The Brain & The Muscle
Most tutorials fail because they don't explain this key concept. CrowdSec has two parts.
You need BOTH.
The BrainThe AgentReads logs & Detects attacks.
The MuscleThe BouncerTalks to Firewall to BLOCK IPs.
Critical Concept:
Many users install the Agent and forget the Bouncer. The result? Your server detects the
hacker but does nothing. You must complete Phase 3 to be safe.
Phase 1
: Install The Agent (The Brain)
First, we need to install the core CrowdSec detection engine. This works
on Ubuntu 20.04/22.04, Debian 11/12, and CentOS.
Step 1: Add the Repository
We will use the official installation script which detects your OS and
adds the correct repositories.
curl -s https://install.crowdsec.net | sudo sh
Step 2: Install the Package
Once the repository is added, install the agent.
sudo apt install crowdsec
During installation, CrowdSec will automatically detect running services
(like SSH, Nginx, or Apache) and install the necessary "scenarios" (rules) for them. You
don't need to configure anything manually for SSH.
Phase 2
: Enroll in the Web Console
CrowdSec has a fantastic free Web Console where you can see a map of
attacks and manage your servers. Let's connect your server to it.
Step 1: Create an Account
Go to app.crowdsec.net and sign up for a
free account.
Step 2: Get Enrollment Command
In the dashboard, click "Add Instance". You will see a command that
looks like this: sudo cscli console enroll [YOUR_KEY].
Step 3: Run on Server
Copy that command and run it on your server:
sudo cscli console enroll xxxxx-your-key-xxxxx
Go back to your browser and click "Accept". Your server is now
syncing with the global database.
Phase 3
: Install The Bouncer (The Muscle)
This is the most critical phase. Right now, CrowdSec is detecting
attacks but NOT blocking them. We need to install the "Firewall Bouncer" which talks to
iptables or nftables to drop packets.
Step 1: Install Firewall Bouncer
(NFTables)
We will use the nftables bouncer. This is the modern replacement
for iptables on Ubuntu 22.04+ and Debian 12, offering better performance and syntax.
sudo apt install crowdsec-firewall-bouncer-nftables
Note: If you are on an older legacy system (CentOS 7), use
crowdsec-firewall-bouncer-iptables instead.
Step 2: Verify it is Running
Check if the bouncer is registered correctly:
You should see a checkmark ✅ under "Status". If you see this, your
server is now an impenetrable fortress.
Phase 4
: Trial by Fire (Attack Simulation)
Don't take our word for it. Let's try to break into your own server to
prove it works.
WARNING:
Do this from a mobile network (4G/5G) or a VPN. Do NOT do this from your home IP, or you
will lock yourself out of your own server!
Step 1: The Attack
Open a terminal on your computer (or use PuTTY) and try to SSH into your
server with a wrong password 10 times rapidly.
Step 2: The Defense
After about 6-10 attempts, your connection will simply "hang" or "time
out". The server is no longer responding to you. It has blocked you.
Log in to your server from a different IP (or console) and run:
sudo cscli decisions list
The Result: You will see your attacking IP listed with the reason
crowdsecurity/ssh-bf and action ban: 4h.
| ID | 1024 |
|---|
| Source IP | 192.168.x.x (Your attacking IP) |
|---|
| Reason | crowdsecurity/ssh-bf |
|---|
| Action | ban: 4h |
|---|
CrowdSec has successfully detected and neutralized the threat.
Step 3: How to Unban Yourself (Emergency)
Panic Button: Did you accidentally block your home WiFi or office
static IP? Don't worry. You can manually remove the ban using the CLI.
# Unban a specific IP
sudo cscli decisions delete --ip 192.168.x.x
# Unban everything (Reset all bans)
sudo cscli decisions delete --all
Pro Tip: Whitelist Your IP
To prevent this permanently, add your IP to the whitelist file located at
/etc/crowdsec/parsers/s02-enrich/whitelist.yaml and restart the service.
Step 4: Whitelist Your IP (Permanent
Safety)
To avoid banning yourself in the future, you should add your home/office
IP to the permanent whitelist. First, create a new configuration file:
sudo nano /etc/crowdsec/parsers/s02-enrich/my-whitelist.yaml
Paste the following code into the file. Important: YAML is
sensitive to indentation. Copy this exactly and replace the IP with yours.
name: my/custom-whitelist
description: "Whitelist my Admin IP"
whitelist:
ip:
- "192.168.1.5" # <--- REPLACE THIS with your actual IP
Save the file (Ctrl+O, Enter) and exit
(Ctrl+X). Finally, reload CrowdSec to apply the changes:
sudo systemctl reload crowdsec
Conclusion: Secure Foundation Matters
You have now upgraded your server security from the "Old School"
reactive method to the "Next Gen" AI-powered defense.
Need a Secure Server?
Security starts with the hardware. CrowdSec runs best on a Bare Metal environment
where you have full root control.