第14章 安全与运维
一句话:安全不是可选项——Gateway 认证、渠道白名单、Sandbox 隔离、Exec 审批,四道防线缺一不可。
14.1 Gateway 认证
Section titled “14.1 Gateway 认证”Token 认证
Section titled “Token 认证”{ gateway: { bind: "lan", auth: { mode: "token", // Token 自动生成,存储在 state 目录 }, },}Password 认证
Section titled “Password 认证”{ gateway: { bind: "lan", auth: { mode: "password", password: "your-secure-password", // 或通过环境变量:OPENCLAW_GATEWAY_PASSWORD }, },}export OPENCLAW_GATEWAY_PASSWORD="your-secure-password"Trusted Proxy
Section titled “Trusted Proxy”在反向代理(如 Nginx、Cloudflare)后面运行时:
{ gateway: { bind: "lan", auth: { mode: "trusted-proxy", }, trustedProxies: ["10.0.0.1"], // 信任的代理 IP 列表 },}Tailscale Serve
Section titled “Tailscale Serve”通过 Tailscale 网络安全访问:
{ gateway: { bind: "tailnet", auth: { mode: "token", }, },}14.2 渠道白名单 + DM Policy
Section titled “14.2 渠道白名单 + DM Policy”控制谁可以与 Agent 对话:
{ channels: { telegram: { enabled: true, botToken: "...", dmPolicy: "pairing", // DM 策略 }, },}DM Policy 四种模式
Section titled “DM Policy 四种模式”| 模式 | 说明 |
|---|---|
"pairing" | 新用户需要通过配对流程 |
"allowlist" | 只有白名单中的用户可以对话 |
"open" | 任何人都可以对话 |
"disabled" | 禁止私聊 |
14.3 命令权限控制
Section titled “14.3 命令权限控制”控制谁可以使用 /think、/model、/config 等管理命令:
{ commands: { allowFrom: { "*": ["ADMIN_USER_ID"], // 所有渠道的管理员 discord: ["user:LEAD_DEV_ID"], // Discord 额外允许 }, },}未授权用户的管理命令会被静默忽略。
14.4 Sandbox 隔离
Section titled “14.4 Sandbox 隔离”Sandbox 在 Docker 容器中隔离 Agent 运行。scope 控制容器和工作区的共享粒度:
| Scope | 说明 |
|---|---|
"session" | 每个 session 独立的容器和工作区(最严格) |
"agent" | 同一 Agent 共享一个容器和工作区(默认) |
"shared" | 所有 session 共享容器和工作区(无跨 session 隔离) |
Tool Profiles
Section titled “Tool Profiles”限制 Agent 可以使用的工具集:
{ tools: { profile: "coding", // "minimal" — 最小工具集(仅 session_status) // "coding" — 代码相关工具集(默认) // "messaging" — 消息相关工具集 // "full" — 所有工具(无限制) },}14.5 Exec 审批机制
Section titled “14.5 Exec 审批机制”远程命令执行的安全控制(详见第 13 章 13.6 节)。Exec 审批通过独立的 ~/.openclaw/exec-approvals.json 文件管理,不是 openclaw.json 配置项:
// ~/.openclaw/exec-approvals.json(由 CLI 管理){ "version": 1, "defaults": { "security": "deny", // deny | allowlist | full "ask": "on-miss", "askFallback": "deny" }, "agents": { "main": { "security": "allowlist" } }}通过 CLI 管理白名单:
openclaw approvals allowlist add "/usr/bin/uptime"openclaw approvals allowlist add --agent main "~/Projects/**/bin/rg"openclaw approvals get # 查看当前审批配置14.6 安全审计
Section titled “14.6 安全审计”# 运行安全审计openclaw security audit
# 自动修复发现的问题openclaw security audit --fix审计检查内容:
- 配置文件中是否有硬编码的密钥
- 认证模式是否正确配置
- Sandbox 范围是否合理
- Exec 白名单是否过于宽松
- 渠道 DM Policy 是否设置
14.7 日志与诊断
Section titled “14.7 日志与诊断”# 全面诊断openclaw doctor
# 查看 Gateway 日志openclaw logsopenclaw logs --limit 100
# 查看 Gateway 状态openclaw gateway status
# 查看所有服务状态openclaw status14.8 备份与恢复
Section titled “14.8 备份与恢复”# 备份整个 OpenClaw 目录cp -r ~/.openclaw/ ~/backup/openclaw-$(date +%Y%m%d)/
# 或用 tar 打包tar czf ~/backup/openclaw-$(date +%Y%m%d).tar.gz ~/.openclaw/# 恢复到新机器cp -r ~/backup/openclaw-20260325/ ~/.openclaw/
# 重新安装 OpenClaw 本体npm install -g openclaw
# 启动openclaw gateway startGit 备份工作区
Section titled “Git 备份工作区”推荐将 workspace 目录纳入 Git 管理:
cd ~/.openclaw/workspacegit initecho "*.tmp" > .gitignoregit add -Agit commit -m "initial workspace backup"14.9 升级策略
Section titled “14.9 升级策略”# 查看当前版本openclaw --version
# 升级到最新版npm install -g openclaw@latest
# ARM64 服务器记得 --ignore-scriptsnpm install -g openclaw@latest --ignore-scripts
# 升级后检查openclaw doctor建议:升级前先备份
~/.openclaw/目录。
14.10 systemd 服务管理
Section titled “14.10 systemd 服务管理”# 查看服务状态systemctl --user status openclaw
# 启动 / 停止 / 重启systemctl --user start openclawsystemctl --user stop openclawsystemctl --user restart openclaw
# 开机自启systemctl --user enable openclaw
# 用户登出后保持服务运行sudo loginctl enable-linger $USER
# 查看日志journalctl --user -u openclaw -f14.11 小结
Section titled “14.11 小结”| 层级 | 防护措施 |
|---|---|
| Gateway | Token / Password / Trusted Proxy / Tailscale |
| 渠道 | DM Policy(pairing / allowlist / open / disabled) |
| 命令权限 | commands.allowFrom 控制管理命令的使用者 |
| Agent | Sandbox scope(agent / session / shared)+ Tool profiles |
| 命令执行 | Exec 审批(exec-approvals.json: deny / allowlist / full) |
| 运维 | openclaw doctor + security audit + 日志 + 备份 |
下一章进入实战篇——搭建一个完整的个人 AI 助手。