# 安装 CrowdSec

{{< steps >}}
{{< step >}}

**下载存储库**

```bash
curl -s https://install.crowdsec.net | sh && apt install -y crowdsec
```

安装完成后，CrowdSec 会自动：

- 注册本机（生成机器身份）
- 下载检测规则（SSH、系统日志等）
- 创建日志采集配置

{{< /step >}}
{{< step >}}

重新载入配置:

```bash
systemctl reload crowdsec
```

{{< /step >}}
{{< step >}}

**使用修复组件阻止攻击(安装防火墙组件)**


CrowdSec 默认不会拦截流量，必须安装 Bouncer 才会封禁 IP


{{< tabs "firewall" >}}
{{< tab "NFTables" >}}

```bash
apt install crowdsec-firewall-bouncer-nftables
```

{{< /tab >}}
{{< tab "IPTables" >}}

```bash
apt install crowdsec-firewall-bouncer-iptables
```

{{< /tab >}}
{{< /tabs >}}


请选择与你系统一致的防火墙（推荐 nftables）


安装完成后，bouncer 会：

- 自动注册到 CrowdSec
- 自动同步封禁列表（CAPI）
- 自动写入防火墙规则

{{< /step >}}
{{< step >}}

**重启服务**

```bash
systemctl restart crowdsec
```

{{< /step >}}
{{< step >}}

**连接 CrowdSec 控制台（可选）**

打开 [CrowdSec Engines如何安装](https://app.crowdsec.net/security-engines/setup?distribution=linux) 页面 ,在 3 Connect with the Console 中复制命令执行,例:

```bash
cscli console enroll You_token
```

在终端中输入注册成功后:
    您可以在 [引擎](https://app.crowdsec.net/security-engines) 页面找到您的引擎，等待审核。

{{< /step >}}
{{< /steps >}}


### 验证安装

**检查是否获取社区封禁（CAPI）**

```bash
cscli metrics | grep -i capi
```

{{< details "输出结果" >}}

```
root@LA:~# cscli metrics | grep -i capi
| generic:scan      | CAPI   | ban    | 2023  |
| ssh:bruteforce    | CAPI   | ban    | 12970 |
| ssh:exploit       | CAPI   | ban    | 7     |
```

{{< /details >}}

**检查 bouncer 是否正常**

```bash
cscli bouncers list
```

{{< details "输出结果" >}}

```
root@LA:~# cscli bouncers list
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 Name              IP Address  Valid  Last API pull         Type                       Version                                                                  Auth Type
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
 firewall-bouncer  127.0.0.1   ✔️     2026-04-07T05:22:51Z  crowdsec-firewall-bouncer  v0.0.34-debian-pragmatic-amd64-12345678900987654321ee64253dfd90bbc1f698  api-key
──────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────────
```

{{< /details >}}

**查看防火墙拦截统计**

```bash
cscli metrics show bouncers
```

{{< details "输出结果" >}}

```
root@LA:~#

cscli metrics show bouncers
╭────────────────────────────────────────────────────────────────────────────────────╮
│ Bouncer Metrics (firewall-bouncer) since 2026-04-07 05:16:51 +0000 UTC             │
├────────────────────────────┬──────────────────┬──────────────────┬─────────────────┤
│ Origin                     │ active_decisions │      dropped     │    processed    │
│                            │        IPs       │  bytes │ packets │ bytes │ packets │
├────────────────────────────┼──────────────────┼────────┼─────────┼───────┼─────────┤
│ CAPI (community blocklist) │           15.00k │  5.38k │      98 │     - │       - │
│ lists:firehol_greensnow    │                - │  7.16k │     179 │     - │       - │
├────────────────────────────┼──────────────────┼────────┼─────────┼───────┼─────────┤
│                      Total │           15.00k │ 12.54k │     277 │ 3.42M │  11.49k │
╰────────────────────────────┴──────────────────┴────────┴─────────┴───────┴─────────╯
```

{{< /details >}}

**查看日志**

```bash
cat /var/log/crowdsec-firewall-bouncer.log
```

### 常见问题（非常重要）

**为什么没有 Bouncer Metrics？**

```
cscli metrics show bouncers
No bouncer metrics found.
```

这不是错误，而是：

  当前没有流量命中封禁规则



