Back to tutorials
Tutorial

Install CrowdSec on Ubuntu VPS to Block Bruteforce

Install CrowdSec on Ubuntu to block SSH/Nginx bruteforce with firewall bouncers—then verify bans safely without locking yourself out.

By Anurag Singh
Updated on Nov 28, 2026
Category: Tutorial
Share article
Install CrowdSec on Ubuntu VPS to Block Bruteforce

Bruteforce traffic rarely shows up as one “big incident.” It starts as noisy auth logs and a few dropped packets. Then you get a CPU spike or an upstream rate-limit.

This walkthrough shows you how to install CrowdSec on Ubuntu on a VPS. You’ll add a firewall bouncer so blocks happen at the network edge. You’ll also validate decisions without locking yourself out.

The steps below assume Ubuntu Server 24.04 LTS or 22.04 LTS on a VPS or dedicated server. Commands are written for root (or use sudo).

What you’ll build (and what it replaces)

You’ll set up:

  • CrowdSec agent parsing logs (SSH and optionally Nginx)
  • Local API (LAPI) storing signals and decisions
  • Firewall bouncer enforcing bans via iptables/nftables
  • A safer workflow for testing blocks and recovering quickly

If you already run Fail2ban, don’t rip it out immediately. Leave it in place during rollout.

That makes it easier to compare detections and tune whitelists.

If your main goal is WordPress login protection, HostMyCode’s Fail2ban walkthrough is still useful. The log-reading patterns are similar: Set Up Fail2ban For WordPress Login On Nginx.

Prerequisites (do this before you touch security)

Before you change anything that can block traffic, make sure you can get back in.

Assume your IP will get banned during testing.

  • Out-of-band access: VPS console / rescue mode / provider console access should be available.
  • Two SSH sessions: keep one logged in while testing.
  • Know your admin IP: the public IP you connect from.

Confirm SSH is reachable and note your source IP

who
curl -fsS https://ifconfig.me
ss -tulpn | grep -E 'sshd|:22'

Verify: you can see your active SSH sessions, your public IP, and sshd listening.

(Recommended) Basic firewall sanity check

If you already use UFW, confirm SSH is explicitly allowed:

ufw status verbose

Verify: there’s a rule allowing OpenSSH or TCP/22 (or your custom SSH port).

Plan your deployment: SSH-only first, then web logs

Start with SSH. It’s the fastest win, and it’s hard to misconfigure.

Add web protection next. Nginx setups vary (proxies, CDNs, custom log formats). Wrong inputs can create false positives.

If you’re migrating from Fail2ban rules for Nginx/WordPress abuse patterns, keep expectations realistic. Blocking bots won’t fix a slow PHP-FPM stack.

If you’re already fighting timeouts, address that first.

This guide covers one common bottleneck: Fix WordPress 504 Gateway Timeout on Nginx VPS.

Step 1 — Install CrowdSec on Ubuntu (agent + LAPI)

CrowdSec provides an official APT repository. On Ubuntu, the install is straightforward.

Install packages

apt update
apt install -y curl gnupg

curl -fsSL https://packagecloud.io/crowdsec/crowdsec/gpgkey | gpg --dearmor -o /usr/share/keyrings/crowdsec-archive-keyring.gpg

. /etc/os-release
echo "deb [signed-by=/usr/share/keyrings/crowdsec-archive-keyring.gpg] https://packagecloud.io/crowdsec/crowdsec/ubuntu/ ${VERSION_CODENAME} main" > /etc/apt/sources.list.d/crowdsec.list

apt update
apt install -y crowdsec

Verify: the service is running.

systemctl status crowdsec --no-pager
cscli version

You should see CrowdSec active and a version string from cscli.

Confirm which log sources CrowdSec is watching

cscli metrics

Verify: under “Acquisition Metrics,” SSH parsing usually appears if your auth logs are available.

On Ubuntu, that’s commonly /var/log/auth.log.

Step 2 — Install a firewall bouncer (so bans actually block)

CrowdSec generates decisions. A bouncer enforces them.

On Ubuntu, the usual choice is the firewall bouncer. It injects rules into iptables or nftables.

Install the bouncer package

apt install -y crowdsec-firewall-bouncer-iptables

Verify: the bouncer is running and registered with LAPI.

systemctl status crowdsec-firewall-bouncer --no-pager
cscli bouncers list

You should see a bouncer entry with a recent “last pull.”

Make the bouncer safer: whitelist your admin IP early

Do this before you start testing.

Replace YOUR_ADMIN_IP with your actual public IP.

cscli decisions add --type ip --scope ip --value YOUR_ADMIN_IP --duration 8760h --reason "Admin IP whitelist"

Important: The command above creates a decision.

CrowdSec decisions can be bans, captchas, or whitelists. That depends on the bouncer and profiles.

If you want a hard “never block this IP” guarantee, use the bouncer whitelist (or an explicit UFW allow rule). On Ubuntu with the firewall bouncer, you can whitelist at the bouncer level:

grep -n "^WHITELISTS_PATH" /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml || true
sed -n '1,200p' /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml

Look for a whitelist path (often /etc/crowdsec/bouncers/whitelists.yaml or similar).

If it’s there, add:

mkdir -p /etc/crowdsec/bouncers
cat > /etc/crowdsec/bouncers/whitelists.yaml <<'EOF'
whitelists:
  - name: admin-ip
    reason: "Always allow admin IP"
    ip:
      - "YOUR_ADMIN_IP"
EOF

systemctl restart crowdsec-firewall-bouncer

Verify: the bouncer restarts cleanly and doesn’t complain about YAML formatting.

journalctl -u crowdsec-firewall-bouncer -n 50 --no-pager

Step 3 — Enable SSH protection (and verify it works)

CrowdSec uses collections (bundles of parsers + scenarios).

Install the SSH collection, then confirm it’s reading your auth logs.

Install the SSH collection

cscli collections install crowdsecurity/sshd
systemctl restart crowdsec

Verify: the collection shows up as installed.

cscli collections list | grep -E 'sshd|crowdsecurity/sshd'

Generate a controlled “failed login” test (from a different IP if possible)

The safest test is from a different network. Use a mobile hotspot, a second VPS, or a coworker’s connection.

If you only have one IP, don’t hammer production SSH from that address.

Only do it if you’re ready to recover via the provider console.

From a separate machine, try a few incorrect SSH attempts:

ssh nonexistent-user@YOUR_SERVER_IP

Enter a wrong password a few times, then stop.

Check CrowdSec alerts and decisions

cscli alerts list
cscli decisions list

Verify: you see an SSH brute-force alert and a decision targeting the testing IP.

Confirm the firewall is actually blocking

Depending on your firewall backend, rules may land in different places.

On Ubuntu, iptables-nft is common.

iptables -S | head
iptables -L -n | sed -n '1,200p'

Verify: you can see chains or rules created by the CrowdSec bouncer (names vary by version).

If your test SSH attempts stop reaching the server, enforcement is working.

Step 4 — Add Nginx protection (real IPs, not your proxy)

If Nginx sits behind Cloudflare, a load balancer, or any reverse proxy, CrowdSec must see the real client IP.

If CrowdSec only sees the proxy address, it can ban the proxy. That takes the site down for everyone.

Start by checking what’s actually in your access log:

tail -n 5 /var/log/nginx/access.log

Verify: the left-most IP should look like real client IPs.

If you see one repeating IP (your proxy), fix Nginx real IP handling before enabling web scenarios.

If you use Cloudflare Tunnel or a similar setup, HostMyCode’s guide on limiting admin exposure is relevant here: Set Up Cloudflare Tunnel on a VPS for Private Admin.

Install the Nginx collection

cscli collections install crowdsecurity/nginx
systemctl restart crowdsec

Verify:

cscli collections list | grep -E 'nginx|crowdsecurity/nginx'
cscli metrics | sed -n '1,200p'

Ensure CrowdSec is acquiring the correct Nginx logs

On Ubuntu, acquisition configs usually live under /etc/crowdsec/acquis.d/.

Check what’s already defined.

ls -la /etc/crowdsec/acquis.d/

# Show any nginx-related acquisition files
grep -R "nginx" -n /etc/crowdsec/acquis.d/ || true

If nothing references Nginx yet, add an acquisition file:

cat > /etc/crowdsec/acquis.d/nginx.yaml <<'EOF'
filenames:
  - /var/log/nginx/access.log
  - /var/log/nginx/error.log
labels:
  type: nginx
EOF

systemctl restart crowdsec

Verify: metrics show Nginx activity once traffic hits the site.

cscli metrics | grep -n "nginx" -n || true

Step 5 — Tune what gets blocked (avoid banning legitimate traffic)

Defaults are fairly conservative. Even so, you can still ban yourself during testing.

You can also block real traffic if your app behavior looks like scanning.

Inspect what scenarios are active

cscli scenarios list

Look for SSH brute-force and Nginx probing scenarios.

If you run an API, your monitoring or CI may hit it hard. Plan to whitelist those source IPs (office egress, status checkers, etc.).

Whitelisting: do it intentionally

Typical candidates:

  • Your office/home IP(s)
  • Your uptime checker IP(s)
  • Your CDN / reverse proxy IP ranges (only if you don’t parse real client IPs yet)

When you have a choice, prefer bouncer-level whitelists.

They prevent enforcement even if detections still fire.

Step 6 — Day-2 operations: view bans, expire them, and test restores

Getting CrowdSec installed is quick.

Recovering from a bad block is what makes it production-safe.

List current decisions (bans)

cscli decisions list

Verify: you can see scope (IP/range), duration, and reason/scenario.

Remove a ban safely

If you blocked a legitimate IP:

cscli decisions delete --ip THE_IP

Verify:

cscli decisions list | grep -F THE_IP || echo "No decision for THE_IP"

Check bouncer health (pulls decisions regularly)

cscli bouncers list
journalctl -u crowdsec-firewall-bouncer -n 100 --no-pager

If “last pull” is stale, the bouncer may not be reaching the Local API.

Its API key may also be wrong.

Common mistakes (and quick fixes)

  • You banned your reverse proxy / Cloudflare IP: fix real client IP logging first, then clear decisions. Check Nginx real_ip_header and set_real_ip_from for your proxy ranges.
  • No bans happen: CrowdSec is installed but no bouncer is enforcing. Install crowdsec-firewall-bouncer-iptables and confirm it appears in cscli bouncers list.
  • Logs aren’t being parsed: acquisition config points at the wrong files. Confirm paths and permissions, then re-check cscli metrics after traffic.
  • Lockout anxiety stops you from testing: whitelist your admin IP at the firewall level (UFW allow) and keep provider console access ready.

Running CrowdSec on production hosting (sizing and placement)

CrowdSec stays lightweight on most VPS setups. Still, log parsing can spike during a bot storm.

If you host multiple sites, leave headroom for bursts.

On a 2 GB VPS, it usually coexists fine with Nginx + PHP-FPM.

The main risk is stacking too many services on one node and calling it “security.” Watch memory, and keep swap behavior in mind.

If you’re moving off shared hosting because you need firewall control and access to logs, a HostMyCode VPS gives you root access for agents like CrowdSec and lets you enforce bans at the firewall.

If you don’t want to manage OS hardening and updates yourself, managed VPS hosting is a better fit for production sites with strict uptime requirements.

Summary: the safe rollout checklist

  • Keep two SSH sessions open and confirm console access.
  • Install CrowdSec and confirm systemctl status crowdsec.
  • Install the firewall bouncer and confirm cscli bouncers list.
  • Enable SSH collection; test from a non-admin IP; validate decisions.
  • Before enabling Nginx scenarios, confirm real client IP logging.
  • Practice unbanning: cscli decisions delete --ip ....

If you also need to shore up email trust (often impacted by bot traffic and compromised forms), read Fix WordPress Not Sending Email on a VPS and Set Up Postfix Relay on a VPS for WordPress Mail for a deliverability-first setup.

If you’re deploying CrowdSec because shared hosting hides logs and won’t let you control the firewall, move the site to infrastructure you can actually administer. Start with a HostMyCode VPS, or choose managed VPS hosting if you want help keeping security tooling and updates production-safe.

FAQ: Install CrowdSec on Ubuntu

Will CrowdSec replace Fail2ban completely?

It can, but you don’t need a day-one cutover.

Run both for a week. Compare what gets banned, then retire Fail2ban rules once you trust your CrowdSec scenarios and whitelists.

How do I confirm CrowdSec is blocking at the firewall?

Check cscli decisions list for an IP ban.

Then confirm firewall rules with iptables -L -n (or nft list ruleset if you use pure nftables). If you want the full story, watch the bouncer logs via journalctl -u crowdsec-firewall-bouncer.

What’s the safest way to test without locking myself out?

Whitelist your admin IP at the bouncer (and keep UFW’s SSH allow rule).

Generate failed logins from another network/IP, then remove the test decision with cscli decisions delete --ip ....

Should I enable Nginx protection if I’m behind Cloudflare?

Yes—but only after Nginx logs record real client IPs.

If CrowdSec only sees Cloudflare’s proxy IPs, it will ban the proxy and disrupt real users.