在 OpenClaw 配置文件里给每个 Agent 单独指定 workspace,核心是在 agents.list 里,给每个 Agent 对象显式写 workspace 字段,覆盖默认值。下面是完整可直接复制的写法与说明。


一、核心配置写法(openclaw.json)

文件路径:~/.openclaw/openclaw.json

json

{
  "agents": {
    // 全局默认(可选,不写也可以)
    "defaults": {
      "workspace": "~/.openclaw/workspace-default"
    },
    // 每个 Agent 单独配置
    "list": [
      {
        "id": "main",
        "model": "gpt-4o",
        // 给 main 单独指定工作区
        "workspace": "~/.openclaw/workspaces/main"
      },
      {
        "id": "write",
        "model": "claude-sonnet",
        // 给 write 单独指定工作区
        "workspace": "~/.openclaw/workspaces/write"
      },
      {
        "id": "design",
        "model": "qwen-vl",
        // 给 design 单独指定工作区
        "workspace": "~/.openclaw/workspaces/design"
      },
      {
        "id": "dev",
        "model": "deepseek-coder",
        // 给 dev 单独指定工作区
        "workspace": "~/.openclaw/workspaces/dev"
      },
      {
        "id": "ops",
        "model": "gpt-4o-mini",
        // 给 ops 单独指定工作区
        "workspace": "~/.openclaw/workspaces/ops"
      }
    ]
  }
}

关键规则

  • agents.list 里的 workspace 优先级最高,会覆盖 agents.defaults.workspace
  • 路径支持 ~(用户目录)、绝对路径、相对路径(相对 ~/.openclaw)。
  • 目录不存在时,OpenClaw 启动会自动创建docs.openclaw.ai。

二、先创建目录(推荐)

先在终端建好所有独立目录,避免权限问题:

bash

运行

# 创建父目录
mkdir -p ~/.openclaw/workspaces

# 为每个 Agent 创建独立工作区
mkdir -p ~/.openclaw/workspaces/main
mkdir -p ~/.openclaw/workspaces/write
mkdir -p ~/.openclaw/workspaces/design
mkdir -p ~/.openclaw/workspaces/dev
mkdir -p ~/.openclaw/workspaces/ops

三、验证是否生效

  1. 重启 OpenClaw 加载配置:

    bash

    运行

    openclaw restart
    
  2. 查看每个 Agent 的工作区:

    bash

    运行

    # 查看 main 的工作区
    openclaw config get --agent main agents.list[0].workspace
    # 查看 write 的工作区
    openclaw config get --agent write agents.list[1].workspace
    
  3. 测试隔离:

    bash

    运行

    # 切换到 write,创建文件
    openclaw use write
    session_send write "write test.txt 'hello write'"
    
    # 切换到 dev,尝试读取(会提示找不到,隔离成功)
    openclaw use dev
    session_send dev "read test.txt"
    

四、命令行快速添加(不手动改 JSON)

如果不想编辑文件,用命令一键创建带独立 workspace 的 Agent:

bash

运行

openclaw agents add \
  --id write \
  --model claude-sonnet \
  --workspace ~/.openclaw/workspaces/write

执行后,配置文件会自动写入 workspace 字段。


五、搭配沙箱(彻底隔离)

在 Agent 里加 sandbox 配置,让每个 Agent 只能访问自己的 workspace,无法越权:

json

{
  "id": "write",
  "model": "claude-sonnet",
  "workspace": "~/.openclaw/workspaces/write",
  "sandbox": {
    "mode": "all",       // 强制沙箱
    "scope": "agent",    // 每个 Agent 独立沙箱
    "workspaceAccess": "none" // 沙箱内看不到主机其他目录
  }
}
Logo

Agent 垂直技术社区,欢迎活跃、内容共建。

更多推荐