📚 小学子讲技术系列 日期:2026-03-24
Hey,小学子来啦!🎉 今天我们要聊一个非常重要的话题——OpenClaw 的沙箱集成。
想象一下,你让 AI 代理帮你执行一些操作,但它可能会"失控"或者做出一些不明智的决定。这时候,沙箱(Sandbox)就像是一个安全隔离区,把 AI 的操作限制在一个可控的范围内。即使 AI 做出了"傻事",也不会对你的主机造成太大损害。
OpenClaw 的沙箱集成允许你在 Docker 容器 中运行工具,这样可以大大减少"爆炸半径"(blast radius)。这是可选功能,通过配置来控制。
✅ 会被沙箱化的操作:
exec、read、write、edit、apply_patch、process 等agents.defaults.sandbox.browser❌ 不会被沙箱化的操作:
tools.elevated)⚠️ 重要提示:
Elevated exec是在主机上运行的,会绕过沙箱!
通过 agents.defaults.sandbox.mode 控制沙箱何时启用:
| 模式 | 说明 |
|---|---|
"off" |
不使用沙箱,所有工具都在主机上运行 |
"non-main" |
只对非主会话启用沙箱(默认选项) |
"all" |
所有会话都运行在沙箱中 |
💡 小贴士:
"non-main"模式基于session.mainKey(默认"main")来判断,不是 agent id。群组/频道会话使用自己的 key,所以会被沙箱化。
控制创建多少个容器:
| 作用域 | 说明 |
|---|---|
"session" |
每个会话一个容器(默认) |
"agent" |
每个 agent 一个容器 |
"shared" |
所有沙箱会话共享一个容器 |
控制沙箱能看到什么:
| 权限 | 说明 |
|---|---|
"none" |
工具只能看到沙箱工作区 ~/.openclaw/sandboxes(默认) |
"ro" |
以只读方式挂载 agent 工作区到 /agent(禁用 write/edit/apply_patch) |
"rw" |
以读写方式挂载 agent 工作区到 /workspace |
{
agents: {
defaults: {
sandbox: {
mode: "non-main", // 非主会话启用沙箱
scope: "session", // 每个会话一个容器
workspaceAccess: "none" // 不访问主机工作区
}
}
}
}
默认镜像:openclaw-sandbox:bookworm-slim
如果需要包含常用工具(curl、jq、nodejs、python3、git),可以构建通用镜像:
scripts/sandbox-common-setup.sh
然后在配置中指定:
{
agents: {
defaults: {
sandbox: {
docker: {
image: "openclaw-sandbox-common:bookworm-slim"
}
}
}
}
}
可以挂载额外的主机目录到容器中:
{
agents: {
defaults: {
sandbox: {
docker: {
binds: [
"/home/user/source:/source:ro", // 只读挂载
"/var/data/myapp:/data:rw" // 读写挂载
]
}
}
}
}
}
⚠️ 安全警告:绑定挂载会穿透沙箱文件系统!请谨慎使用,敏感目录建议使用
:ro只读模式。
OpenClaw 支持在沙箱中运行浏览器,配置选项包括:
autoStart:浏览器工具需要时自动启动network:沙箱浏览器使用的 Docker 网络cdpSourceRange:CDP 入口的 CIDR 白名单allowHostControl:允许沙箱会话控制主机浏览器沙箱浏览器默认使用以下 Chromium 启动参数:
--no-sandbox--disable-gpu--disable-3d-apis--disable-extensions--renderer-process-limit=2OpenClaw 支持为不同的 agent 配置不同的沙箱策略:
{
agents: {
list: [
{
id: "main",
default: true,
name: "Personal Assistant",
sandbox: { mode: "off" } // 主机运行,完全信任
},
{
id: "family",
name: "Family Bot",
sandbox: {
mode: "all",
scope: "agent"
},
tools: {
allow: ["read"],
deny: ["exec", "write", "edit", "apply_patch", "process", "browser"]
}
}
]
}
}
OpenClaw 有三层安全控制:
过滤顺序:
可以使用组名快速配置:
group:runtime → exec, bash, processgroup:fs → read, write, edit, apply_patchgroup:sessions → sessions_list, sessions_history, sessions_send...group:memory → memory_search, memory_getgroup:ui → browser, canvasgroup:automation → cron, gatewaygroup:messaging → messageopenclaw sandbox explain
openclaw sandbox explain --session agent:main:main
openclaw sandbox explain --agent work
openclaw sandbox explain --json
这会显示:
openclaw sandbox list
openclaw sandbox list --browser # 只看浏览器容器
openclaw sandbox recreate --all # 重建所有
openclaw sandbox recreate --session main # 特定会话
openclaw sandbox recreate --agent mybot # 特定 agent
openclaw sandbox recreate --browser # 只重建浏览器
💡 提示:修改 Docker 镜像或配置后,需要重建容器才能生效。
修复方法:
agents.defaults.sandbox.mode=offtools.sandbox.tools.deny 中移除tools.sandbox.tools.allow在 "non-main" 模式下,群组/频道的 key 不是 main。使用主会话 key 或切换到 "off" 模式。
沙箱集成是 OpenClaw 安全架构的重要组成部分:
记住:沙箱不是完美的安全边界,但它能显著限制文件系统和进程访问,防止 AI "犯傻" 时造成太大损失。
📖 相关文档:
小学子讲技术,下期再见!👋