How to Install CrowdSec on an Ubuntu VPS: Replacing fail2ban with Collaborative Intrusion Detection



When one server is left to face attacks alone

Open the SSH log of a VPS that has been running for a few weeks, and the pattern is almost always the same: login attempts from a string of different IP addresses, usernames guessed one by one, a short pause, then the attack resumes from another address. fail2ban handles this in a familiar way, reading logs, counting failures, and blocking the address for a while. That approach works, but it has a hard limit: fail2ban on your server knows nothing about what is happening on other servers. Every VPS starts from zero, faces the same attacker, and learns the same lesson separately.

CrowdSec changes that position. It still reads local logs and detects attack patterns on your own machine, but its blocking decisions are enriched by a community blocklist: an IP already identified as attacking other CrowdSec installations can be blocked on your server before it manages a single login attempt. That does not mean your server stops thinking for itself. Local detection keeps running, and the community list simply adds an early layer that leaves repeat offenders with fewer chances.

This guide walks through installing CrowdSec on Ubuntu or Debian, including the firewall bouncer that actually enforces decisions, a whitelist that keeps your own addresses from being blocked, and a short daily monitoring routine. By the end, you will know exactly when CrowdSec is only recording, when it is blocking, and how to lift a block when something goes wrong.

One note before you start: this guide does not remove fail2ban automatically. If fail2ban is still active, do not let two tools manage the same firewall rules. The troubleshooting section at the end explains the safe choice for that situation.

Prerequisites

Prepare a VPS running Ubuntu 22.04, Ubuntu 24.04, or Debian 12 and newer, with sudo access and SSH already using keys rather than passwords. CrowdSec itself is light, but leave at least 1 vCPU and 512 MB of free RAM so log analysis does not compete with your main services.

Check which firewall backend the system uses first. Modern Ubuntu and Debian use nftables, and this guide uses the nftables bouncer. Run the following to confirm:

sudo nft list ruleset | head -20

If that command shows tables and chains, nftables is active. If the system instead uses legacy iptables, swap the bouncer package name at the installation step for the iptables variant, as explained there.

Also write down the IP addresses that must never be blocked: the public IP you work from, the IP of any monitoring server, and the office IP if you reach the VPS from there. This list goes into the whitelist in its own step, before you rely fully on automatic blocking.

Finally, make sure the system clock is accurate. Time-based detection falls apart when the server clock drifts badly, and decisions shared with the community also depend on correct timestamps. The following command shows sync status:

timedatectl status

The expected result is the line System clock synchronized: yes. If it is not there, fix time sync before going further.

Step 1: Add the official CrowdSec repository

CrowdSec provides a repository setup script at https://install.crowdsec.net. The script detects the distribution, adds the correct package source, and imports the signing key. Because piping a script from the internet straight into a shell always deserves a second look, download it first, read it, then run it from a local file:

curl -s https://install.crowdsec.net -o crowdsec-repo.sh
less crowdsec-repo.sh
sudo sh crowdsec-repo.sh

The expected output names the detected distribution and the new source file at /etc/apt/sources.list.d/crowdsec_crowdsec.list. This script only prepares the repository; it does not install the engine yet. Then refresh the package index:

sudo apt update

If this fails with a key error or a missing repository, re-check the script output from less and make sure no proxy or network filter altered the download. Do not proceed to install packages from a repository you have not verified.

Step 2: Install the Security Engine

Install the main CrowdSec package:

sudo apt install -y crowdsec

During installation, CrowdSec runs service detection: it scans existing logs and installs matching collections, for example the Linux collection and the SSH collection when those services are detected. A collection is a configuration package containing log parsers and detection scenarios for one service. Without the right collection, CrowdSec does not know how to read that service's logs, so this stage decides whether protection is genuinely active or merely installed.

Check the service and its version:

sudo systemctl status crowdsec --no-pager
sudo cscli version

The expected result is a service in active (running) state and a version number printed without errors. The cscli command is CrowdSec's command-line interface for managing the engine, and nearly every check that follows uses it.

Then look at the installed collections:

sudo cscli collections list

The expected result includes at least crowdsecurity/linux and crowdsecurity/sshd on a VPS running SSH. If the Nginx collection is missing while Nginx is running, install it by hand:

sudo cscli collections install crowdsecurity/nginx
sudo systemctl reload crowdsec

The reload command makes the engine read the new configuration without stopping the process entirely. If you add a new service later, such as Apache or another web service, return to this command and install the matching collection, because automatic detection only runs during the initial installation.

Step 3: Install the firewall bouncer so blocks are actually enforced

This is the step most often skipped, and its consequence is the most confusing: the engine is installed, alerts appear, but not a single attacker is actually blocked. The reason is simple. The Security Engine only makes decisions. The component that carries them out is called a bouncer. Without a bouncer, CrowdSec records attacks without stopping them.

Install the firewall bouncer for nftables:

sudo apt install -y crowdsec-firewall-bouncer-nftables
sudo systemctl enable --now crowdsec-firewall-bouncer

On a system still using legacy iptables, use the package name crowdsec-firewall-bouncer-iptables instead. Do not install both on the same backend, because two bouncers fighting over firewall rules produce behaviour that is hard to trace.

Check that the bouncer registered with the CrowdSec Local API:

sudo cscli bouncers list

The expected output looks roughly like this:

Name                              IP Address  Valid  Last API pull         Type
--------------------------------  ----------  -----  -------------------   --------
cs-firewall-bouncer-xxxxxxxxxx    127.0.0.1   valid  2026-10-28T01:00:00Z  firewall

A valid entry in the Valid column and a recent Last API pull timestamp mean the enforcement path is live. From this point, every decision the engine writes is pushed into nftables and traffic from blocked IPs is dropped in the kernel. Also check the service status:

sudo systemctl status crowdsec-firewall-bouncer --no-pager

An active (running) status is the expected result. If the bouncer is registered but its service is dead, decisions are still not enforced, so do not rely on the bouncer table alone.

Step 4: Add baseline scenarios and whitelist your own addresses

A scenario is a detection rule, for example detecting SSH brute force or HTTP probing. Install the baseline scenarios relevant to a general-purpose VPS:

sudo cscli scenarios install crowdsecurity/ssh-bf crowdsecurity/http-probing
sudo systemctl reload crowdsec

Now the part that protects you from yourself: the whitelist. Before letting automatic blocking run at full strength, register the IP addresses that must never be blocked under any circumstances. CrowdSec keeps whitelists in /etc/crowdsec/parsers/s02-enrich/whitelists.yaml. Open that file and add your addresses to the whitelist list, following the format already in the file. Replace 203.0.113.10 in the example below with your real public IP, and add a separate line for every other address that needs protection:

whitelist:
  reason: "Admin and monitoring addresses, never block"
  ip:
    - "203.0.113.10"
  cidr:
    - "198.51.100.0/24"

Save the file, then reload the engine:

sudo systemctl reload crowdsec

Skipping this step carries a risk that is far from theoretical. One false positive on a monitoring or work address can lock you out of your own server, and recovery then needs the very access that was just lost. A narrow, specific whitelist is far safer than an overly broad CIDR range, so list the addresses you actually use, not the whole network block of your internet provider.

Step 5: Read metrics, alerts, and decisions

These three commands are the CrowdSec monitoring routine, and each answers a different question. Start with metrics:

sudo cscli metrics

This output shows which logs are being read, how many lines have been processed, and which scenarios have triggered. If the line for the SSH log shows zero while login attempts keep appearing in the system log, CrowdSec is not reading the right source. Check /etc/crowdsec/acquis.yaml, the file that registers log sources, and make sure the SSH log path is listed there. Silent metrics are the earliest signal that protection is not active, long before the first alert appears.

The second command shows detected attacks:

sudo cscli alerts list

The third shows active decisions, the list of blocks currently in force:

sudo cscli decisions list

Decision output lists the blocked IP, the reason, the duration, and the origin of the decision, whether from local detection or from the community list. If this list is empty on the first day, that is not necessarily a problem. On a quiet VPS, emptiness can simply mean no attack has yet met a scenario threshold. What should raise suspicion is metrics at zero combined with decisions at zero, because that combination points to an engine reading nothing at all.

For direct service logs during diagnosis, use the systemd journal:

sudo journalctl -u crowdsec -n 50 --no-pager

Configuration errors, log-reading failures, and Local API connection problems usually show up here with messages clear enough to act on.

Step 6: Lift blocks and manage manual decisions

Sooner or later you will need to lift a block, whether because a legitimate address triggered a rule or because you are testing from a new network. Removing the block for one IP works like this, replacing the address with the one you mean:

sudo cscli decisions delete --ip 203.0.113.10

This removes the active decision for that address, so its traffic is allowed again. If the same address is blocked again immediately, the cause is usually a scenario that keeps triggering on behaviour from that address, not a leftover decision. In that case, check the related alerts before lifting the block repeatedly:

sudo cscli alerts list --ip 203.0.113.10

If you need to block one address by hand, for example an address clearly scanning the server but not yet meeting a scenario threshold, the following command adds a manual decision:

sudo cscli decisions add --ip 203.0.113.10 --duration 24h --reason "Aggressive scanning, blocked manually"

The duration can be adjusted, and manual decisions appear in the same list as automatic ones, so they stay visible at the next audit. A good habit is to give every manual decision a clear reason, because that reason is the only trace your future self will have for why the address was blocked.

Step 7: Connect to the Console, if you want a dashboard

The CrowdSec Console is an optional web dashboard for viewing alerts from several engines in one place and managing blocklists remotely. This step is not required for local protection, but it helps when you manage more than one VPS. Create a Console account, copy the enrollment key from the Engines menu, then run the following on the VPS, replacing the example key with yours:

sudo cscli console enroll YOUR_ENROLLMENT_KEY
sudo systemctl restart crowdsec

After that, accept the engine in the Console interface. Until you press accept there, the engine is not fully connected. If you manage only one VPS and are comfortable on the command line, skipping this step reduces protection not at all. The local engine and bouncer keep working without the Console.



CrowdSec versus fail2ban: what actually changes

The comparison below uses criteria observable from how each tool behaves, not from marketing claims. fail2ban remains a legitimate, lightweight tool for one simple pattern, but the architectural differences decide when CrowdSec is worth replacing it with.

Aspectfail2banCrowdSec
Detection sourceRegex patterns on one log on one serverParsers and scenarios for many services, plus a community blocklist
Protection before the first attackNone, the attacker must fail on your server firstPresent, IPs that attacked other installations can be blocked earlier
EnforcementFirewall rules written directlySeparate bouncers, for firewall, Nginx, or other services
Service coverageOne filter per service, written by handReady-made collections for SSH, Nginx, Apache, and other services
Central dashboardNot included by defaultOptional Console for multiple engines
Initial complexityLow, one configuration fileModerate, the engine and the bouncer are two components

The last row deserves attention. CrowdSec does add one component to monitor. In return it offers wider detection and community protection that a tool working alone cannot provide. For a VPS exposed to the internet with SSH and web services, that trade is usually worth making.

Common mistakes and how to fix them

SymptomMost likely causeFix
Alerts appear, but attackers can still connectBouncer not installed or its service is deadInstall the bouncer matching the backend, check cscli bouncers list and its service status
Bouncer registered, but nothing is blockedWrong backend, for example an iptables bouncer on a pure nftables systemSwitch to the nftables bouncer package and check nft list ruleset
Metrics show zero for the SSH logLog source not registered in acquis.yaml or the log path differsFix the path in /etc/crowdsec/acquis.yaml, then reload the engine
Your own address gets blockedWhitelist does not contain that addressAdd the address to whitelists.yaml, reload, then remove the decision with decisions delete
fail2ban and CrowdSec overwrite each other's rulesTwo tools managing the same firewall chainKeep one tool per chain, or disable fail2ban once CrowdSec is verified
CrowdSec service fails to start after a configuration editYAML syntax error in the edited fileCheck logs with journalctl -u crowdsec, fix the YAML indentation, then restart
Community decisions never appearEngine has not pulled a fresh list or API connectivity is brokenCheck engine logs and outbound network status, then check cscli metrics at the next interval

One pattern repeats across this table: most CrowdSec failures are not detection failures, but enforcement failures or log-reading failures. The time-saving diagnosis order is to check the engine service, then the bouncer, then the metrics, and only then suspect the scenario.

Final checklist

  • ☐ Official repository added and package index refreshed.
  • ☐ The crowdsec service is active and its version shows via cscli version.
  • ☐ The crowdsecurity/linux and crowdsecurity/sshd collections are installed, plus web service collections where relevant.
  • ☐ A firewall bouncer for the correct backend is installed and shows valid in cscli bouncers list.
  • ☐ The ssh-bf and http-probing scenarios are installed and the engine has been reloaded.
  • ☐ Admin and monitoring IPs are in the whitelist in whitelists.yaml.
  • ☐ cscli metrics shows logs being read, not zero for every source.
  • ☐ Removing a block has been tried on one example address, so you know how to restore access if a mistake happens.
  • ☐ The fail2ban decision is made: keep it on a different chain, or disable it once CrowdSec is verified.

Which attack scenario shows up most often in your VPS logs, and did the community blocklist change how fast you respond to it? Share it in the comments, so other readers can compare the attack patterns they are facing.

Read also: How to Install Stirling PDF with Docker: A Complete Self-Hosted PDF Toolkit on Your VPS for the next service worth installing once your VPS security layer is in place.

Diskusi

Ada pertanyaan atau pengalaman yang ingin dibagikan?

0komentar

Komentar ditinjau sebelum ditampilkan. Mari berdiskusi dengan sopan dan sesuai topik.