LLI Wiki — My Digital Knowledge Base

Securing an Arch Linux VPS: CrowdSec, nftables, Fail2ban and GoAccess

My runbook: who does what between CrowdSec, nftables, Fail2ban and Nginx, the everyday commands, troubleshooting and the commands never to run blindly.

This wiki runs on a small Arch Linux VPS, behind Nginx. And a server exposed to the Internet is like a public swimming pool in summer: there's always someone trying to get in without paying. Here is my security runbook: who does what, the everyday commands, and the ones you never run blindly.

Read commands are harmless. Block and unblock commands change security decisions: use them knowingly, and with a second SSH session open.

1. Architecture

Component Role
CrowdSec Analyses the logs, detects suspicious behaviour and produces alerts and decisions
crowdsec-firewall-bouncer-nftables Applies CrowdSec decisions in nftables, over IPv4 and IPv6
nftables The main firewall, with an input policy of drop
Fail2ban An additional, independent protection, with its own f2b-table table
Nginx Serves the applications (this wiki, Mastodon, etc.) and provides the logs
GoAccess HTTP statistics per application, generated every 15 minutes
CrowdSec HTML report A summary of alerts and decisions, refreshed every 5 minutes

The chain: HTTP request or SSH attempt → Nginx logs or journald → CrowdSec analysis → decision → nftables bouncer → network block.

Three things to keep in mind:

2. Service status

sudo systemctl status crowdsec --no-pager
sudo systemctl status crowdsec-firewall-bouncer --no-pager
sudo systemctl status fail2ban --no-pager
sudo systemctl status nftables --no-pager
sudo systemctl status sshd --no-pager
sudo systemctl status nginx --no-pager

The condensed version:

for svc in crowdsec crowdsec-firewall-bouncer fail2ban nftables sshd nginx; do
  printf '%-36s ' "$svc"
  systemctl is-active "$svc"
done

Recent logs:

sudo journalctl -u crowdsec -n 80 --no-pager
sudo journalctl -u crowdsec-firewall-bouncer -n 80 --no-pager
sudo journalctl -u fail2ban -n 80 --no-pager

3. CrowdSec: the essential commands

sudo cscli version
sudo cscli metrics
sudo cscli alerts list
sudo cscli alerts list -o json
sudo cscli decisions list
sudo cscli decisions list -o json
sudo cscli collections list
sudo cscli parsers list
sudo cscli scenarios list
sudo cscli bouncers list
sudo cscli allowlists list

The vocabulary

To look at an alert, get its ID first:

sudo cscli alerts list
sudo cscli alerts inspect ID

To check that the logs are actually coming in:

sudo cscli metrics
sudo journalctl -u crowdsec --since "1 hour ago" --no-pager

Alerts created by manual tests with cscli stay in the history. Don't mistake them for real attacks.

4. Blocking and unblocking an IP address

Active decisions:

sudo cscli decisions list

Ban an address by hand (carefully):

sudo cscli decisions add --ip 203.0.113.10 --duration 10m --reason "manual-test"

203.0.113.10 is an address reserved for documentation: perfect for a test, without blocking anybody.

Remove the decision:

sudo cscli decisions delete --ip 203.0.113.10

Important: never ban your own admin address, your VPN or the relay that gives you access to the server. Before a real test, keep a second SSH session open. Banning yourself is the system administrator's DELETE without a WHERE.

The nftables rules:

sudo nft list ruleset
sudo nft list tables
sudo nft -a list ruleset | grep -iE 'crowdsec|f2b|drop|reject'

The bouncer uses nftables sets and drop rules, and works over IPv4 and IPv6 alike. Don't flush its tables or sets by hand: let it manage its own decisions.

5. Fail2ban: diagnosis and unbanning

sudo fail2ban-client ping
sudo fail2ban-client status
sudo fail2ban-client status JAIL_NAME

Remove a banned IP from a jail:

sudo fail2ban-client set JAIL_NAME unbanip IP_ADDRESS

An IP can be blocked by both Fail2ban and CrowdSec. Unblocking it in only one of the two tools isn't always enough.

6. SSH: configuration and checks

The principles applied:

Check the configuration actually in effect:

sudo sshd -T | grep -Ei '^(port|permitrootlogin|passwordauthentication|pubkeyauthentication) '
sudo ss -ltnp | grep -E ':(<SSH_PORT>|443)\b'
sudo journalctl -u sshd --since "today" --no-pager
sudo journalctl SYSLOG_IDENTIFIER=sshd-session -n 80 --no-pager

With a WebSocket tunnel, SSH connections come from 127.0.0.1: that's not necessarily an anomaly.

Don't restart SSH or touch the firewall remotely without a backup session.

7. The logs analysed by CrowdSec

One Nginx access log per application, plus journald for SSH:

/var/log/nginx/access.log
/var/log/nginx/<application>_access.log
journald: SYSLOG_IDENTIFIER=sshd-session

Check that the files exist and are alive:

sudo ls -lh /var/log/nginx/*access.log
sudo tail -n 30 /var/log/nginx/<application>_access.log
sudo cscli metrics

The base collections: nginx, sshd, linux, base-http, http-cve, auditd, plus those for the databases on the server. Careful: an installed collection doesn't mean all its log sources are fed. cscli metrics tells you what is really being read.

8. GoAccess and the CrowdSec dashboard

The reports are served by Nginx on a dedicated subdomain (here stats.example.com):

Access is protected by HTTP Basic Auth (auth_basic + a .htpasswd file). A request without credentials must return 401 Unauthorized.

The CrowdSec report is produced by a small script run by a systemd timer. It is generated with mode 600, then published as root:http, mode 640.

The timer:

[Timer]
OnCalendar=*-*-* *:0/5:00
AccuracySec=30s
Persistent=true
Unit=crowdsec-html-report.service

Check the generation:

sudo systemctl status crowdsec-html-report.timer --no-pager
sudo systemctl list-timers --all crowdsec-html-report.timer --no-pager
sudo systemctl show crowdsec-html-report.service \
  -p Result -p ExecMainStatus -p ExecMainStartTimestamp
sudo stat -c '%y %a %U:%G %n' /usr/share/webapps/goaccess/apps/crowdsec.html

Regenerate right now:

sudo systemctl start crowdsec-html-report.service
sudo systemctl show crowdsec-html-report.service -p Result -p ExecMainStatus

Check that the report is not public, from the server itself:

curl -sk --resolve stats.example.com:443:127.0.0.1 \
  -o /dev/null -w 'HTTP %{http_code}\n' \
  https://stats.example.com/apps/crowdsec.html

Expected result without authentication: 401. And no, noindex and robots.txt don't replace authentication: they politely ask polite robots not to look. The others look anyway.

9. Nginx security checks

sudo nginx -t
sudo nginx -T 2>/dev/null | grep -nE 'server_name|auth_basic|root |listen 443'
curl -sSI https://stats.example.com/ | head -n 20

The headers to have: HSTS, X-Content-Type-Options, X-Frame-Options, Referrer-Policy, Permissions-Policy, and a CSP. A CSP in Report-Only mode observes violations without blocking them: great for testing, but you then need to switch to enforcing mode.

Cross-site: don't mix everything up

CrowdSec spots some suspicious HTTP behaviour in the logs, but does not replace the application's own protection against XSS and CSRF.

10. The check routine

echo "=== SERVICES ==="
for svc in crowdsec crowdsec-firewall-bouncer fail2ban nftables nginx sshd; do
  printf '%-36s ' "$svc"
  systemctl is-active "$svc"
done

echo
echo "=== CROWDSEC ALERTS ==="
sudo cscli alerts list

echo
echo "=== CROWDSEC BANS ==="
sudo cscli decisions list

echo
echo "=== FAIL2BAN ==="
sudo fail2ban-client status

echo
echo "=== REPORT TIMER ==="
systemctl list-timers --all crowdsec-html-report.timer --no-pager

echo
echo "=== LAST GENERATION ==="
stat -c '%y %n' /usr/share/webapps/goaccess/apps/crowdsec.html

11. Troubleshooting

No CrowdSec alerts? That can be normal if no scenario fired. Check cscli metrics, the acquisitions and the service logs.

An alert, but the IP isn't blocked? Check cscli decisions list, expired decisions, allowlists and the bouncer.

The IP is still blocked after an unban? Check CrowdSec, Fail2ban and the nftables rules separately.

The CrowdSec report is old? Check the timer, the service, its log and the file date:

sudo journalctl -u crowdsec-html-report.service -n 80 --no-pager
sudo systemctl list-timers --all crowdsec-html-report.timer --no-pager

The link to the CrowdSec report disappeared from the home page? There are two separate index pages:

grep -n 'crowdsec.html' /usr/share/webapps/goaccess/index.html
grep -n 'crowdsec.html' /usr/share/webapps/goaccess/apps/index.html

Golden rule: never share the bouncer API keys, Nginx secrets, private SSH keys or the contents of authentication files. Not in a ticket, not in a wiki. Especially not in a wiki.

12. Commands not to run without a rollback plan

# DO NOT run blindly:
# sudo nft flush ruleset
# sudo systemctl stop nftables
# sudo systemctl stop sshd
# sudo cscli decisions delete --all

They can remove protections, cut remote access or wipe useful decisions. Usually all three at once, on a Friday evening.

The principle: diagnose → back up → change one component at a time → check SSH access → check the logs and the protection.

Mastodon LLIMastodon SocialFosstodon