Ubuntu 22.04 CrowdSec 入侵防御部署与命令详解
在 Ubuntu 22.04 上安装 CrowdSec 入侵防御系统,通过官方源部署 crowdsec 与 cscli,管理集合、围栏、决策与警报,并结合 nftables 防火墙围栏封禁恶意 IP,附命令行速查与真实回显。
Ubuntu 22.04 CrowdSec 入侵防御部署与命令详解
目标:在 Ubuntu 22.04 上从零部署 CrowdSec 新一代开源入侵防御系统(IPSP),接入官方仓库安装
crowdsec与cscli,安装集合、管理决策与警报、启动 nftables 防火墙围栏(bouncer)实现自动封禁,附完整命令行速查与真实回显。
⚠️ 说明:CrowdSec 安装脚本会访问官方更新服务器(
version.crowdsec.net、api.crowdsec.net等)获取版本信息、集合索引并注册,需保证出网 HTTPS 可达;局部网络抖动可能导致安装后脚本报错,重试即可。
一、环境信息
- 操作系统:Ubuntu 22.04 LTS(x86_64)
- 服务器软件:crowdsec(本地引擎 LAPI + 采集器)
- 命令行工具:cscli
- 版本:crowdsec v1.8.1(Debian 打包版,构建于 Ubuntu 22.04)
- 围栏(bouncer):crowdsec-firewall-bouncer-nftables(nftables 后端)
- 本地 API:
127.0.0.1:8080(LAPI) - 数据目录:
/var/lib/crowdsec/ - 配置目录:
/etc/crowdsec/ - 数据库:SQLite(
/var/lib/crowdsec/data/crowdsec.db)
二、前置条件
- 拥有
root权限或可用sudo。 - 至少 512MB 可用内存、1GB 空闲磁盘。
- 出网可访问 HTTPS(安装、集合下载、CAPI 注册均需联网)。
- Ubuntu 22.04 已配置可用的 APT 软件源。
三、版本选型说明
| 项目 | 说明 |
|---|---|
| CrowdSec | 逻辑与 Fail2ban 相近但更现代化:本地引擎解析日志 → 场景规则触发 → LAPI 汇总 → 围栏执行封禁;且支持接入全局信誉库 |
| 采集器 | 内置 crowdsecurity/linux、crowdsecurity/sshd 等常用集合,SSH 暴力破解开箱即用 |
| 围栏 | crowdsec-firewall-bouncer-nftables 将决策灌入 nftables,实现内核级自动封禁 |
| 与 Fail2ban 区别 | CrowdSec 采用「LAPI 集中决策 + 独立围栏」架构,决策可跨机器、跨类型传播 |
| Ubuntu 22.04 来源 | 官方 apt 仓库(install.crowdsec.net),非 Ubuntu universe |
四、部署步骤
4.1 更新索引并准备基础工具
sudo apt update
sudo apt install -y wget curl gnupg
4.2 添加 CrowdSec 官方仓库
官方提供一键仓库脚本,自动识别发行版并导入 GPG 密钥:
curl -s https://install.crowdsec.net | sudo sh
真实回显关键行:
Detected operating system as ubuntu/22.
Detected apt version as 2.4.14
Checking for gpg...
Detected gpg...
Checking for curl...
Detected curl...
Importing packagecloud gpg key...
Packagecloud gpg key imported to /etc/apt/keyrings/crowdsec_crowdsec-archive-keyring.gpg
Installing /etc/apt/sources.list.d/crowdsec_crowdsec.list...
4.3 安装 crowdsec 主程序
sudo apt update
sudo apt install -y crowdsec
⚠️ 首次安装时包会触发
cscli setup unattended,需联网获取最新版本号并注册。若网络抖动导致安装 script 返回非零(如unable to retrieve latest crowdsec version),执行以下命令重试配置:
sudo dpkg --configure crowdsec
重试成功后回显关键行:
creating /etc/crowdsec/acquis.d/setup.linux.yaml
CrowdSec alone will not block any IP address. If you want to block them, you must use a remediation component.
You can find them on https://app.crowdsec.net/hub/remediation-components
Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.
💡 说明:提示「CrowdSec 本身不会封禁任何 IP」,正是强调安装围栏(bouncer)才是执行封禁的一环;随后小节会安装 nftables 围栏。
4.4 验证 cscli 与版本
cscli version
真实回显:
version: v1.8.1-debian-pragmatic-amd64-909b5157
Codename: alphaga
BuildDate: 2026-09-03_10:57:01
GoVersion: 1.26.3
Platform: linux
libre2: C++
User-Agent: crowdsec/v1.8.1-debian-pragmatic-amd64-909b5157-linux
Constraint_parser: >= 1.0, <= 3.0
Constraint_scenario: >= 1.0, <= 3.0
Constraint_api: v1
Constraint_acquis: >= 1.0, < 2.0
Built-in optional components: cscli_setup, datasource_appsec, datasource_cloudwatch, datasource_file, datasource_http, datasource_journalctl, datasource_kafka, datasource_kinesis, datasource_loki, datasource_s3, datasource_syslog, datasource_victorialogs, datasource_wineventlog, db_mysql, db_postgres, db_sqlite
4.5 启动 crowdsec 引擎
sudo systemctl enable crowdsec
sudo systemctl start crowdsec
sudo systemctl is-active crowdsec # 输出:active
4.6 查看本地配置
cscli config show
真实回显:
Global:
- Configuration Folder : /etc/crowdsec
- Data Folder : /var/lib/crowdsec/data
- Hub Folder : /etc/crowdsec/hub
- Notification Folder : /etc/crowdsec/notifications
- Simulation File : /etc/crowdsec/simulation.yaml
- Log Folder : /var/log
- Log level : info
- Log Media : file
Crowdsec:
- Acquisition File : /etc/crowdsec/acquis.yaml
- Parsers routines : 1
- Acquisition Folder : /etc/crowdsec/acquis.d
cscli:
- Output : human
- Hub Branch :
API Client:
- URL : http://127.0.0.1:8080/
- Login : 59c109d1808b4bdc804ec6acbe6ec54bSYMpEMk21wiTdsdV
- Credentials File : /etc/crowdsec/local_api_credentials.yaml
Local API Server:
- Listen URL : 127.0.0.1:8080
- Listen Socket :
- Profile File : /etc/crowdsec/profiles.yaml
- Trusted IPs:
- 127.0.0.1
- ::1
- Database:
- Type : sqlite
- Path : /var/lib/crowdsec/data/crowdsec.db
- Flush age : 168h0m0s
- Flush size : 5000
4.7 查看当前启用的集合(Collections)
cscli collections list
真实回显(常规安装默认启用 linux 与 sshd 集合):
----------------------------------------------------------------------------------------------------------------
COLLECTIONS
----------------------------------------------------------------------------------------------------------------
Name 📦 Status Version Local Path
----------------------------------------------------------------------------------------------------------------
crowdsecurity/linux ✔️ enabled 0.4 /etc/crowdsec/collections/linux.yaml
crowdsecurity/sshd ✔️ enabled 0.9 /etc/crowdsec/collections/sshd.yaml
crowdsecurity/whitelist-good-actors ✔️ enabled 0.4 /etc/crowdsec/collections/whitelist-good-actors.yaml
----------------------------------------------------------------------------------------------------------------
4.8 更新场景索引并安装 Web 集合(示例:nginx)
sudo cscli hub update
sudo cscli collections install crowdsecurity/nginx
hub update 回显(索引最新时):
Nothing to do, the hub index is up to date.
collections install 回显关键行:
enabling scenarios:crowdsecurity/CVE-2024-38475
enabling scenarios:crowdsecurity/CVE-2024-0012
enabling scenarios:crowdsecurity/CVE-2024-9474
enabling collections:crowdsecurity/http-cve
enabling collections:crowdsecurity/base-http-scenarios
enabling collections:crowdsecurity/nginx
Run 'sudo systemctl reload crowdsec' for the new configuration to be effective.
⚠️ 注意:安装集合后需重载引擎使新解析器与场景生效:
sudo systemctl reload crowdsec。
4.9 查看引擎运行指标
cscli metrics
真实回显(LAPI 路由统计):
+------------------------------------+
| Local API Metrics |
+--------------------+--------+------+
| Route | Method | Hits |
+--------------------+--------+------+
| /v1/alerts | GET | 2 |
| /v1/usage-metrics | POST | 1 |
| /v1/watchers/login | POST | 3 |
+--------------------+--------+------+
+-------------------------------------------------------------------------------+
| Local API Machines Metrics |
+--------------------------------------------------+------------+--------+------+
| Machine | Route | Method | Hits |
+--------------------------------------------------+------------+--------+------+
| 59c109d1808b4bdc804ec6acbe6ec54bSYMpEMk21wiTdsdV | /v1/alerts | GET | 2 |
+--------------------------------------------------+------------+--------+------+
4.10 管理决策(Decisions)
查看当前决策(初始为空):
cscli decisions list
真实回显:
No active decisions
手动添加一条「封禁 24 小时」决策:
cscli decisions add --ip 203.0.113.9 --duration 24h --reason demo
回显:
level=info msg="Decision successfully added"
再次查看,决策已入库:
cscli decisions list
真实回显:
+----+--------+----------------+--------+--------+---------+----+--------+------------+----------+
| ID | Source | Scope:Value | Reason | Action | Country | AS | Events | expiration | Alert ID |
+----+--------+----------------+--------+--------+---------+----+--------+------------+----------+
| 1 | cscli | Ip:203.0.113.9 | demo | ban | | | 1 | 23h59m59s | 1 |
+----+--------+----------------+--------+--------+---------+----+--------+------------+----------+
按 ID 删除决策:
cscli decisions delete --id 2
回显:
level=info msg="1 decision(s) deleted"
4.11 查看警报(Alerts)
cscli alerts list
无警报时回显:
No active alerts
4.12 安装 nftables 防火墙围栏(bouncer)
围栏是把「决策」变成「实际封禁」的组件。此处安装 nftables 版围栏:
sudo apt install -y crowdsec-firewall-bouncer-nftables
⚠️ 说明:官方 apt 包名是
crowdsec-firewall-bouncer-nftables(不是二进制的crowdsec-firewall-bouncer),二进制位于/usr/sbin/crowdsec-firewall-bouncer。
安装过程会自动向 LAPI 注册并生成 API Key,回显关键行:
cscli/crowdsec is present, generating API key
API Key successfully created
查看围栏注册列表:
cscli bouncers list
真实回显:
--------------------------------------------------------------------------------------------
Name IP Address Valid Last API pull Type Version Auth Type
--------------------------------------------------------------------------------------------
my-bouncer ✔️ api-key
cs-firewall-bouncer-1791464377 ✔️ api-key
--------------------------------------------------------------------------------------------
💡 说明:
bouncers list中的每一项代表一个已注册的围栏;Valid ✔️表示密钥有效。手动注册围栏可执行cscli bouncers add my-bouncer,会输出一次性看的 API Key。
启动围栏服务:
sudo systemctl enable crowdsec-firewall-bouncer
sudo systemctl start crowdsec-firewall-bouncer
sudo systemctl is-active crowdsec-firewall-bouncer # 输出:active
4.13 查看围栏配置
sudo cat /etc/crowdsec/bouncers/crowdsec-firewall-bouncer.yaml
真实回显关键参数(API Key 已用变量占位,实际为安装时生成):
mode: nftables
update_frequency: 10s
log_mode: file
api_url: http://127.0.0.1:8080/
api_key: ${CROWDSEC_BOUNCER_API_KEY}
deny_action: DROP
💡 说明:
mode: nftables表示把决策下发到 nftables 规则;update_frequency: 10s每 10 秒向 LAPI 拉取一次决策;deny_action: DROP封禁动作默认丢弃。
五、CLI 速查
| 命令 | 作用 |
|---|---|
cscli version | 查看版本与平台信息 |
cscli config show | 查看全局/LAPI/数据库等配置 |
cscli collections list | 列出已启用集合 |
cscli collections install crowdsecurity/nginx | 安装集合 |
cscli hub update | 更新场景索引 |
cscli decisions list | 列出当前决策 |
cscli decisions add --ip 203.0.113.9 --duration 24h --reason demo | 新增封禁决策 |
cscli decisions delete --id 2 | 按 ID 删除决策 |
cscli alerts list | 列出警报 |
cscli bouncers list | 列出已注册围栏 |
cscli bouncers add my-bouncer | 注册围栏并输出 API Key |
cscli metrics | 查看引擎指标 |
cscli simulation | 管理模拟状态(直接阻止前先演练) |
服务托管(每条仅针对单一单元):
sudo systemctl enable crowdsec
sudo systemctl reload crowdsec
sudo systemctl enable crowdsec-firewall-bouncer
六、注意事项
- 围栏是封禁的执行者:只有安装并启动 bouncer,决策才会真正落地到防火墙,否则 CrowdSec 只检测不拦截。
- 登录 LAPI 前先联网:首次安装、
hub update、collections install均需访问官方服务器。 - 集合安装后重载引擎:
sudo systemctl reload crowdsec让新解析器/场景生效。 - API Key 只显示一次:
cscli bouncers add输出的 Key 需妥善保存,丢失需重新注册。 - 决策类型与范围:决策可作用于 IP、Range、AS、Country 等多种 scope;
ban为封禁、captcha为验证码。
七、常见问题
7.1 安装时 unable to retrieve latest crowdsec version
因网络访问 version.crowdsec.net 超时所致。重试配置即可:
sudo dpkg --configure crowdsec
systemctl is-active crowdsec # 应为 active
7.2 加了决策但 IP 没被封
确认围栏已启动且为 nftables 模式:
sudo systemctl is-active crowdsec-firewall-bouncer
sudo nft list ruleset | grep -i crowdsec # 或查看围栏日志
也可先用 --sim 模拟验证:cscli simulation enable(不落地封禁)。
7.3 cscli 提示无法连接 LAPI
确认 crowdsec 在运行,且 LAPI 监听 8080:
sudo systemctl is-active crowdsec
ss -lntp | grep 8080
7.4 想让某个 IP 永不被封禁
使用白名单:
cscli allowlists add --name my-trusted --reason "trusted host"
cscli allowlists add-item --name my-trusted --value 203.0.113.5
八、卸载方式
sudo systemctl stop crowdsec-firewall-bouncer
sudo systemctl disable crowdsec-firewall-bouncer
sudo systemctl stop crowdsec
sudo systemctl disable crowdsec
sudo apt purge -y crowdsec-firewall-bouncer-nftables crowdsec
⚠️ 注意:
disable/stop需逐单元执行(每行一个单元)。purge会移除配置与本地数据库;如需保留可改用remove。
九、参考链接
- CrowdSec 官网:https://www.crowdsec.net/
- 文档与场景中心:https://hub.crowdsec.net/
- 本地 API / 围栏文档:https://docs.crowdsec.net/