目录

OpenClaw VPS部署方案

最新部署日期: 2026年2月7日
版本: Gateway v2026.2.3-1, Node v2026.2.3-1
状态: 生产就绪


架构概览

整体架构图

┌─────────────────────────────────────────────────────────────┐
│                      外部访问层                              │
│  ┌──────────────┐  ┌──────────────┐  ┌──────────────┐      │
│  │   Telegram   │  │  Control UI  │  │    SSH       │      │
│  │   Bot API    │  │   (HTTP)     │  │   管理       │      │
│  └──────┬───────┘  └──────┬───────┘  └──────┬───────┘      │
└─────────┼─────────────────┼─────────────────┼───────────────┘
          │                 │                 │
          ▼                 ▼                 ▼
┌─────────────────────────────────────────────────────────────┐
│                    AWS EC2 服务器                            │
│  实例ID: <your-ec2-instance-id>                             │
│  公网IP: <your-ec2-ip>                                      │
│  类型: t3.small (2 vCPU, 2GB RAM)                           │
│  系统: Amazon Linux 2023                                    │
│                                                             │
│  ┌───────────────────────────────────────────────────────┐  │
│  │         OpenClaw Gateway v2026.2.3-1                  │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐   │  │
│  │  │  Telegram   │  │  WebSocket  │  │  HTTP API   │   │  │
│  │  │   通道      │  │ 服务器     │  │ 接口       │   │  │
│  │  │  (端口: -)  │  │ (端口:18789)│  │ (端口:18789)│   │  │
│  │  └──────┬──────┘  └──────┬──────┘  └──────┬──────┘   │  │
│  │         │                │                 │          │  │
│  │         └────────────────┴─────────────────┘          │  │
│  │                          │                            │  │
│  │              ┌───────────┴───────────┐                │  │
│  │              │   Agent 处理引擎      │                │  │
│  │              │   (AI 模型: GPT-OSS)  │                │  │
│  │              └───────────┬───────────┘                │  │
│  │                          │                            │  │
│  │              ┌───────────┴───────────┐                │  │
│  │              │   工具路由层          │                │  │
│  │              │   (exec → Node)       │                │  │
│  │              └───────────┬───────────┘                │  │
│  └──────────────────────────┼────────────────────────────┘  │
└─────────────────────────────┼───────────────────────────────┘
                              │ WebSocket over SSH 隧道
                              │ (端口: 18790 → 18789)
┌─────────────────────────────┼───────────────────────────────┐
│                             ▼                               │
│                    本地 Mac 客户端                          │
│                                                             │
│  ┌───────────────────────────────────────────────────────┐  │
│  │         OpenClaw Node v2026.2.3-1 (<your-node-name>)  │  │
│  │                                                        │  │
│  │  ┌─────────────┐  ┌─────────────┐  ┌─────────────┐   │  │
│  │  │  SSH 隧道   │  │  WebSocket  │  │  Exec       │   │  │
│  │  │  客户端     │  │  客户端     │  │  审批系统   │   │  │
│  │  │(launchd管理)│  │             │  │ (allowlist) │   │  │
│  │  └─────────────┘  └─────────────┘  └─────────────┘   │  │
│  │         │                │                 │          │  │
│  │         └────────────────┴─────────────────┘          │  │
│  │                          │                            │  │
│  │              ┌───────────┴───────────┐                │  │
│  │              │   本地执行层          │                │  │
│  │              │   - 文件读写          │                │  │
│  │              │   - 命令执行          │                │  │
│  │              │   - 浏览器控制        │                │  │
│  │              └───────────────────────┘                │  │
│  └───────────────────────────────────────────────────────┘  │
└─────────────────────────────────────────────────────────────┘

数据流向

  1. 用户 → Telegram Bot: 发送消息
  2. Gateway → Agent: 处理消息,决定行动
  3. Agent → Node: 如需本地操作,通过 WebSocket 发送 RPC
  4. Node → Mac 系统: 在本地执行命令(受 allowlist 限制)
  5. 结果 → Gateway → Telegram: 返回执行结果

组件清单

EC2 服务器 (AWS)

组件 版本/配置 状态
实例类型 t3.small ✅ 运行中
操作系统 Amazon Linux 2023 ✅ 已配置
Node.js v22.22.0 ✅ 已安装
OpenClaw Gateway v2026.2.3-1 ✅ 运行中
SSH 服务 OpenSSH 9.x ✅ 已加固
Swap 2GB ✅ 已配置
SSM Agent v3.3.x ✅ 已启用
IAM Role EC2-SSM-Role ✅ 已挂载

Mac 客户端 (本地)

组件 版本/配置 状态
Node.js v25.1.0 ✅ 已安装
OpenClaw CLI v2026.2.3-1 ✅ 已安装
OpenClaw Node ✅ 已连接
SSH 客户端 OpenSSH 10.0 ✅ 已配置
launchd 服务 2个 plist ✅ 已加载

外部服务

服务 用途 状态
OpenAI AI 模型 API ✅ 已配置
Telegram Bot 消息通道 ✅ 运行中

网络拓扑

端口映射

端口 协议 服务 绑定地址 说明
22 TCP SSH 0.0.0.0 EC2 管理入口
80 TCP HTTP 0.0.0.0 预留 (未使用)
443 TCP HTTPS 0.0.0.0 预留 (未使用)
18789 TCP OpenClaw Gateway 127.0.0.1 WebSocket + HTTP API
18790 TCP SSH 隧道本地端 127.0.0.1 Mac 本地端口

安全组规则

# 入站规则
TCP 22   0.0.0.0/0   # SSH 访问
TCP 80   0.0.0.0/0   # HTTP (预留)
TCP 443  0.0.0.0/0   # HTTPS (预留)

注意: Gateway 绑定在 loopback (127.0.0.1:18789),不直接暴露公网。

连接路径

Mac (localhost:18790)
    ↓ SSH 本地转发 (-L)
EC2 (localhost:18789)
    ↓ Gateway 内部路由
Agent 处理 → Node RPC → Mac Node 执行

安全配置

认证凭证

⚠️ 重要: 以下凭证仅保存在本地,请勿分享。

Gateway Token

<your-gateway-token>
  • 用途: 认证 WebSocket 连接
  • 位置: ~/.openclaw/openclaw.json (EC2), launchd plist (Mac)

SSH 密钥

  • 位置: ~/.ssh/openclaw.pem
  • 权限: 400 (仅所有者可读)
  • 对应 EC2 Key Pair: openclaw

Telegram Bot Token

<your-telegram-bot-token>
  • 用途: Telegram Bot API 认证
  • 位置: EC2 Gateway 配置中

API Key (agentify.top)

<your-api-key>
  • 用途: AI 模型调用
  • 位置: EC2 Gateway 配置中

访问控制

Telegram 配置

{
  "dmPolicy": "pairing",
  "allowFrom": ["<your-telegram-user-id>"]
}
  • dmPolicy: pairing: 新用户需要配对码才能使用
  • allowFrom: 仅允许你的 Telegram 用户 ID

Exec Approvals (Mac Node)

{
  "security": "allowlist",
  "ask": "off",
  "askFallback": "allowlist"
}

白名单命令:

  • /bin/ls, /bin/cat, /bin/echo
  • /usr/bin/find, /usr/bin/grep, /usr/bin/wc, /usr/bin/head, /usr/bin/tail
  • /usr/bin/file, /usr/bin/stat, /usr/bin/du, /usr/bin/df
  • /usr/bin/uname, /usr/bin/whoami, /usr/bin/sw_vers
  • /usr/bin/git, /usr/bin/python3, /usr/bin/open
  • /opt/homebrew/bin/*

⚠️ 不在白名单的命令会被拒绝执行:

  • rm, mv, chmod, sudo 等危险命令
  • 需要时手动添加到 ~/.openclaw/exec-approvals.json

初始部署步骤

本节介绍从零开始部署的完整步骤。如果你已经部署完成,可以跳过此章节。

1. AWS 账户配置

1.1 查看当前账户

aws sts get-caller-identity

1.2 切换账户(如需要)

# 清除旧凭证
rm ~/.aws/credentials ~/.aws/config

# 配置新账户
aws configure
# 输入:
# - AWS Access Key ID: 你的 access_key
# - AWS Secret Access Key: 你的 secret_key
# - Default region name: us-east-1
# - Default output format: json

2. 创建 EC2 实例

2.1 创建密钥对

  1. 访问 AWS 控制台: https://console.aws.amazon.com/ec2/
  2. 导航到 Key Pairs (密钥对)
  3. 点击 Create key pair
  4. 名称: openclaw
  5. 格式: .pem (Mac/Linux) 或 .ppk (Windows)
  6. 点击 Create 并下载私钥

2.2 创建实例

  1. 点击 Launch Instance
  2. Name: OpenClaw-Gateway
  3. AMI: Amazon Linux 2023
  4. Instance type: t3.small (2 vCPU, 2GB RAM)
  5. Key pair: 选择 openclaw
  6. Network settings:
    • 创建安全组,允许 SSH (22), HTTP (80), HTTPS (443)
  7. Storage: 8GB gp3 (默认)
  8. 点击 Launch

2.3 配置安全组

确保入站规则包含:

类型 协议 端口 来源 用途
SSH TCP 22 0.0.0.0/0 SSH 管理
HTTP TCP 80 0.0.0.0/0 预留
HTTPS TCP 443 0.0.0.0/0 预留

3. 连接服务器

# 设置密钥权限
chmod 400 ~/.ssh/openclaw.pem

# SSH 连接
ssh -i ~/.ssh/openclaw.pem ec2-user@<你的-EC2-公网IP>

4. 安装 OpenClaw

⚠️ 重要: Gateway 和 Node 必须使用相同版本的 OpenClaw,否则会出现兼容性问题(如 exec 无法路由到 Node)。

4.1 安装 Node.js 22+

# 安装 fnm (Node 版本管理器)
curl -fsSL https://fnm.vercel.app/install | bash

# 加载 fnm
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)

# 安装 Node.js 22
fnm install 22
fnm use 22

# 验证
node -v  # v22.x.x
npm -v   # 10.x.x

4.2 安装 Git(如需要)

sudo yum install -y git

4.3 安装 OpenClaw

export PATH=$HOME/.local/share/fnm/bin:$PATH
npm install -g openclaw@latest

# 验证
openclaw --version

5. 配置 OpenClaw

5.1 创建配置目录

mkdir -p ~/.openclaw

5.2 编辑配置文件

nano ~/.openclaw/openclaw.json

使用以下配置模板(替换占位符):

{
  "gateway": {
    "mode": "local",
    "bind": "loopback",
    "port": 18789,
    "auth": {
      "mode": "token",
      "token": "your-secret-token-here"
    },
    "http": {
      "endpoints": {
        "chatCompletions": {
          "enabled": true
        }
      }
    }
  },
  "agents": {
    "defaults": {
      "model": {
        "primary": "openai-compat/openai/gpt-oss-120b"
      },
      "elevatedDefault": "full",
      "maxConcurrent": 4,
      "subagents": {
        "maxConcurrent": 8
      }
    }
  },
  "models": {
    "mode": "merge",
    "providers": {
      "openai-compat": {
        "baseUrl": "https://api.agentify.top/v1",
        "apiKey": "your-api-key-here",
        "api": "openai-completions",
        "models": [
          {
            "id": "openai/gpt-oss-120b",
            "name": "GPT-OSS-120B"
          }
        ]
      }
    }
  },
  "tools": {
    "allow": [
      "exec", "process", "read", "write", "edit",
      "web_search", "web_fetch", "cron"
    ],
    "elevated": {
      "enabled": true,
      "allowFrom": {
        "telegram": ["your-telegram-user-id"]
      }
    },
    "exec": {
      "backgroundMs": 10000,
      "timeoutSec": 1800,
      "cleanupMs": 1800000,
      "notifyOnExit": true
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "your-telegram-bot-token",
      "dmPolicy": "pairing",
      "allowFrom": ["your-telegram-user-id"],
      "groupPolicy": "disabled",
      "groups": {
        "*": {
          "requireMention": true
        }
      }
    }
  },
  "plugins": {
    "entries": {
      "telegram": {
        "enabled": true
      }
    }
  }
}

5.3 配置说明

配置项 说明 获取方式
gateway.auth.token Gateway 访问令牌 随机生成长字符串
models.providers.openai-compat.apiKey 中转站 API Key agentify.top 控制台
channels.telegram.botToken Telegram Bot Token @BotFather
channels.telegram.allowFrom 允许的 Telegram 用户 ID 首次对话后查看日志

6. 配置 Systemd 服务

创建服务文件实现自动启动:

mkdir -p ~/.config/systemd/user
cat > ~/.config/systemd/user/openclaw-gateway.service << 'EOF'
[Unit]
Description=OpenClaw Gateway (v2026.2.3-1)
After=network.target

[Service]
Type=simple
Environment="PATH=/home/ec2-user/.local/share/fnm/bin:/usr/local/bin:/usr/bin:/bin"
Environment="HOME=/home/ec2-user"
ExecStart=/bin/bash -c 'export PATH=/home/ec2-user/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw gateway --port 18789'
Restart=always
RestartSec=10
StandardOutput=journal
StandardError=journal

[Install]
WantedBy=default.target
EOF

# 启用并启动服务
systemctl --user daemon-reload
systemctl --user enable openclaw-gateway
systemctl --user start openclaw-gateway

# 验证状态
systemctl --user status openclaw-gateway

7. 系统加固

7.1 配置 Swap

sudo fallocate -l 2G /swapfile
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile
echo '/swapfile swap swap defaults 0 0' | sudo tee -a /etc/fstab

7.2 保护 SSHD

# 设置 sshd OOM 保护
echo -1000 | sudo tee /proc/$(pgrep -x sshd)/oom_score_adj

EC2 服务器配置

基本信息

实例ID: <your-ec2-instance-id>
公网IP: <your-ec2-ip>
私网IP: <your-private-ip>
可用区: us-east-1a
密钥对: openclaw
安全组: <your-security-group-id>
IAM角色: EC2-SSM-Role

系统加固

1. Swap 配置 (防 OOM)

# 已配置 2GB swap
$ free -h
              total        used        free
Mem:          1.9Gi       539Mi       942Mi
Swap:         2.0Gi          0B       2.0Gi

2. SSHD 保护

# sshd 进程 OOM score 设为 -1000 (不会被 kill)
$ cat /proc/$(pgrep -x sshd)/oom_score_adj
-1000

3. Host Key 重建

由于原始 RSA host key 丢失,已通过 SSM 重建:

ssh-keygen -t rsa -b 4096 -f /etc/ssh/ssh_host_rsa_key

重要文件路径

文件 路径 说明
主配置 ~/.openclaw/openclaw.json Gateway 配置
日志 /tmp/openclaw/openclaw-YYYY-MM-DD.log 运行日志
Systemd 服务 ~/.config/systemd/user/openclaw-gateway.service 服务定义
Node 安装 ~/.local/share/fnm/ fnm + Node.js

服务管理

# 查看 Gateway 状态
systemctl --user status openclaw-gateway

# 重启 Gateway
systemctl --user restart openclaw-gateway

# 查看日志
journalctl --user -u openclaw-gateway -f

# Gateway CLI 命令
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)
openclaw --version
openclaw status
openclaw nodes status
openclaw models list

Gateway 配置详情

{
  "gateway": {
    "mode": "local",
    "bind": "loopback",
    "port": 18789,
    "auth": {
      "mode": "token",
      "token": "<your-gateway-token>"
    }
  },
  "channels": {
    "telegram": {
      "enabled": true,
      "dmPolicy": "pairing",
      "botToken": "<your-telegram-bot-token>",
      "allowFrom": ["<your-telegram-user-id>"],
      "groupPolicy": "disabled"
    }
  },
  "models": {
    "providers": {
      "openai-compat": {
        "baseUrl": "https://api.agentify.top/v1",
        "apiKey": "<your-api-key>",
        "models": [{"id": "openai/gpt-oss-120b"}]
      }
    }
  },
  "tools": {
    "exec": {
      "host": "node",
      "security": "allowlist",
      "node": "<your-node-name>"
    }
  }
}

Mac 客户端配置

安装路径

/opt/homebrew/bin/openclaw          # OpenClaw CLI
/opt/homebrew/bin/node              # Node.js v25.1.0
/usr/bin/ssh                        # SSH 客户端

配置文件

1. launchd SSH 隧道服务

路径: ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.openclaw.ssh-tunnel</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/ssh</string>
        <string>-N</string>
        <string>-L</string>
        <string>18790:127.0.0.1:18789</string>
        <string>-i</string>
        <string>~/.ssh/openclaw.pem</string>
        <string>-o</string>
        <string>ServerAliveInterval=30</string>
        <string>-o</string>
        <string>ServerAliveCountMax=3</string>
        <string>-o</string>
        <string>ExitOnForwardFailure=yes</string>
        <string>-o</string>
        <string>StrictHostKeyChecking=yes</string>
        <string>ec2-user@<your-ec2-ip></string>
    </array>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <dict>
        <key>NetworkState</key>
        <true/>
    </dict>
    <key>ThrottleInterval</key>
    <integer>30</integer>
    <key>StandardOutPath</key>
    <string>/tmp/openclaw-ssh-tunnel.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/openclaw-ssh-tunnel.log</string>
</dict>
</plist>

2. launchd Node 客户端服务

路径: ~/Library/LaunchAgents/com.openclaw.node.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
    <key>Label</key>
    <string>com.openclaw.node</string>
    <key>ProgramArguments</key>
    <array>
        <string>/opt/homebrew/bin/openclaw</string>
        <string>node</string>
        <string>run</string>
        <string>--host</string>
        <string>127.0.0.1</string>
        <string>--port</string>
        <string>18790</string>
        <string>--display-name</string>
        <string><your-node-name></string>
    </array>
    <key>EnvironmentVariables</key>
    <dict>
        <key>OPENCLAW_GATEWAY_TOKEN</key>
        <string><your-gateway-token></string>
        <key>PATH</key>
        <string>/opt/homebrew/bin:/opt/homebrew/sbin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin</string>
    </dict>
    <key>RunAtLoad</key>
    <true/>
    <key>KeepAlive</key>
    <dict>
        <key>NetworkState</key>
        <true/>
    </dict>
    <key>ThrottleInterval</key>
    <integer>15</integer>
    <key>StandardOutPath</key>
    <string>/tmp/openclaw-node.log</string>
    <key>StandardErrorPath</key>
    <string>/tmp/openclaw-node.log</string>
</dict>
</plist>

3. Exec Approvals 配置

路径: ~/.openclaw/exec-approvals.json

⚠️ 重要: 必须包含 /bin/sh/bin/bash,因为 nodes run --raw 使用 shell 执行命令。

{
  "version": 1,
  "defaults": {
    "security": "allowlist",
    "ask": "off",
    "askFallback": "allowlist"
  },
  "agents": {
    "main": {
      "security": "allowlist",
      "ask": "off",
      "askFallback": "allowlist",
      "autoAllowSkills": true,
      "allowlist": [
        {"pattern": "/bin/sh"},
        {"pattern": "/bin/bash"},
        {"pattern": "/bin/ls"},
        {"pattern": "/bin/cat"},
        {"pattern": "/bin/echo"},
        {"pattern": "/usr/bin/find"},
        {"pattern": "/usr/bin/grep"},
        {"pattern": "/usr/bin/wc"},
        {"pattern": "/usr/bin/head"},
        {"pattern": "/usr/bin/tail"},
        {"pattern": "/usr/bin/file"},
        {"pattern": "/usr/bin/stat"},
        {"pattern": "/usr/bin/du"},
        {"pattern": "/usr/bin/df"},
        {"pattern": "/usr/bin/uname"},
        {"pattern": "/usr/bin/whoami"},
        {"pattern": "/usr/bin/sw_vers"},
        {"pattern": "/usr/bin/git"},
        {"pattern": "/usr/bin/python3"},
        {"pattern": "/usr/bin/open"},
        {"pattern": "/opt/homebrew/bin/*"}
      ]
    }
  }
}

Node 连接信息

Node ID: <your-node-id>
Name: <your-node-name>
Version: core v2026.2.3-1
Capabilities: browser, system
Status: paired · connected

日常使用指南

首次启动 (Mac 重启后)

无需手动操作,launchd 会自动启动所有服务。

验证服务状态:

# 查看服务状态
launchctl list | grep openclaw

# 预期输出:
# 7485    0   com.openclaw.node
# 7379    0   com.openclaw.ssh-tunnel

# 验证 Node 连接
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw nodes status'

手动管理服务

# 停止服务
launchctl unload ~/Library/LaunchAgents/com.openclaw.node.plist
launchctl unload ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist

# 启动服务
launchctl load ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist
launchctl load ~/Library/LaunchAgents/com.openclaw.node.plist

# 查看日志
tail -f /tmp/openclaw-ssh-tunnel.log
tail -f /tmp/openclaw-node.log

Telegram Bot 配置

创建 Bot

  1. 在 Telegram 中联系 @BotFather
  2. 发送 /newbot
  3. 按提示设置:
    • 名称: 你的 Bot 显示名称(如 “My AI Assistant”)
    • 用户名: 唯一标识,必须以 bot 结尾(如 myai_bot
  4. 复制获得的 Bot Token(格式:123456789:ABCdefGHIjklMNOpqrSTUvwxyz
  5. 将 Token 填入 ~/.openclaw/openclaw.jsonchannels.telegram.botToken

获取你的 Telegram 用户 ID

方法 1: 首次对话后查看日志

ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'tail -50 /tmp/openclaw/openclaw-*.log | grep -i telegram'

方法 2: 使用 @userinfobot

  1. 在 Telegram 中打开 @userinfobot
  2. 发送任意消息
  3. Bot 会回复你的用户 ID

方法 3: 使用 @RawDataBot

  1. 将 Bot 添加到你的频道/群组
  2. 查看返回的数据,找到 message.from.id

配置 Gateway

将获取到的用户 ID 填入配置:

{
  "channels": {
    "telegram": {
      "enabled": true,
      "botToken": "<your-telegram-bot-token>",
      "dmPolicy": "pairing",
      "allowFrom": ["<your-telegram-user-id>"]
    }
  }
}

重启 Gateway 生效:

systemctl --user restart openclaw-gateway

Telegram 日常使用

  1. 开始使用:

    • 打开 Telegram,找到你的 Bot
    • 发送 /start 或任意消息
    • 如果是首次使用,Gateway 会显示配对码
  2. 查看配对码 (在 EC2 上):

    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw pairing list telegram'
  3. 批准配对:

    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw pairing approve telegram <CODE>'
  4. 日常使用:

    • 直接发送消息给 Bot,AI 会处理
    • 如需在 Mac 上执行命令,AI 会通过 Node 执行
    • 危险命令会被 allowlist 拦截

添加新的白名单命令

编辑 ~/.openclaw/exec-approvals.json,在 agents.main.allowlist 中添加:

{"pattern": "/usr/local/bin/your-command"}

然后重启 Node 服务:

launchctl unload ~/Library/LaunchAgents/com.openclaw.node.plist
launchctl load ~/Library/LaunchAgents/com.openclaw.node.plist

备用:手动启动方式

如果 launchd 自动启动出现问题,可以手动启动:

1. 手动启动 SSH 隧道

# 终止现有隧道
pkill -f "ssh.*18790.*<your-ec2-ip-short>"

# 启动隧道
ssh -i ~/.ssh/openclaw.pem -N \
  -L 18790:127.0.0.1:18789 \
  -o ServerAliveInterval=30 \
  -o ServerAliveCountMax=3 \
  -o StrictHostKeyChecking=yes \
  ec2-user@<your-ec2-ip>

# 或后台运行
ssh -i ~/.ssh/openclaw.pem -N \
  -L 18790:127.0.0.1:18789 \
  -o ServerAliveInterval=30 \
  -o ServerAliveCountMax=3 \
  -f ec2-user@<your-ec2-ip>

2. 手动启动 Node 客户端

# 设置环境变量
export OPENCLAW_GATEWAY_TOKEN="<your-gateway-token>"

# 启动 Node(前台)
openclaw node run \
  --host 127.0.0.1 \
  --port 18790 \
  --display-name "<your-node-name>"

# 或后台运行
export OPENCLAW_GATEWAY_TOKEN="<your-gateway-token>"
nohup openclaw node run \
  --host 127.0.0.1 \
  --port 18790 \
  --display-name "<your-node-name>" \
  > /tmp/openclaw-node-manual.log 2>&1 &

3. EC2 上手动启动 Gateway(如无 systemd)

# 前台运行
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)
openclaw gateway --port 18789

# 或后台运行
export PATH=$HOME/.local/share/fnm/bin:$PATH
eval $(fnm env)
nohup openclaw gateway --port 18789 > /tmp/openclaw-gateway.log 2>&1 &

# 检查进程
ps aux | grep openclaw-gateway

# 停止进程
pkill -f openclaw-gateway

验证 Exec 是否正确路由到 Mac Node

测试方法:

在 Telegram 中发送:

/exec echo $HOME

结果判断:

  • 返回 /home/ec2-user = ❌ 在 Gateway (EC2) 上执行
  • 返回 /Users/<your-username> = ✅ 在 Mac Node 上执行

如果仍在 Gateway 执行:

  1. 检查版本一致性:

    # Gateway 版本
    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw --version'
    
    # 两者版本必须一致!
  2. 检查 exec 配置:

    openclaw config get tools.exec
    # 应显示: "host": "node", "node": "<your-node-name>"
  3. 检查 Node 状态:

    openclaw nodes status
    # 应显示: paired · connected
  4. 已知问题: 截至 OpenClaw v2026.2.6,即使配置正确,exec 路由到 Node 仍可能不工作。


扩展 CLI 命令参考

模型管理

# 查看可用模型列表
openclaw models list

# 设置默认模型
openclaw models set openai-compat/openai/gpt-oss-120b

# 查看模型状态
openclaw models status

# 查看模型提供商状态
openclaw models providers

配对管理

# 查看待配对请求
openclaw pairing list telegram

# 批准配对请求
openclaw pairing approve telegram <CODE>

# 拒绝配对请求
openclaw pairing deny telegram <CODE>

# 查看所有配对
openclaw pairing list

频道管理

# 查看频道状态
openclaw channels status

# 查看 Telegram 频道详情
openclaw channels status telegram

会话管理

# 列出所有会话
openclaw sessions list

# 查看会话历史
openclaw sessions history <session-id>

# 向会话发送消息
openclaw sessions send <session-id> "你的消息"

Node 管理

# 查看所有已配对节点
openclaw nodes list

# 查看节点详细描述
openclaw nodes describe --node <node-id>

# 查看待配对请求
openclaw nodes pending

# 批准节点配对
openclaw nodes approve <request-id>

# 重命名节点
openclaw nodes rename --node <id> --name "新名称"

# 在节点上执行命令(需节点支持 system.run)
openclaw nodes run --node <node-id> -- echo "Hello"

# 查看节点执行审批
openclaw approvals allowlist list --node <node-id>

配置管理

# 查看配置
openclaw config get

# 查看特定配置项
openclaw config get gateway.port
openclaw config get channels.telegram.dmPolicy

# 设置配置项
openclaw config set tools.exec.host node
openclaw config set tools.exec.security allowlist

# 删除配置项
openclaw config unset tools.exec.node

网关管理

# 查看 Gateway 状态
openclaw status

# 深度检查
openclaw status --deep

# 查看健康状态
openclaw health

# 发送测试消息到 Gateway
openclaw gateway send --agent main "测试消息"

工具执行

# 在当前主机执行命令(Gateway 端)
openclaw exec -- echo "Hello from Gateway"

# 指定在 Node 上执行
openclaw exec host=node node=<node-id> -- uname -a

日志和调试

# 查看 Gateway 日志(EC2)
journalctl --user -u openclaw-gateway -f

# 查看特定日期日志
tail -f /tmp/openclaw/openclaw-2026-02-07.log

# 格式化查看配置
cat ~/.openclaw/openclaw.json | python3 -m json.tool

故障排除

SSH 连接失败

症状: Connection closed by <your-ec2-ip> port 22

排查步骤:

  1. 检查 known_hosts: ssh-keygen -R <your-ec2-ip>
  2. 重新添加: ssh-keyscan -H <your-ec2-ip> >> ~/.ssh/known_hosts
  3. 检查安全组: AWS 控制台 → EC2 → 安全组 → 入站规则
  4. 通过 SSM 连接 (备用):
    aws ssm start-session --target <your-ec2-instance-id>

Node 无法连接

症状: Known: 1 · Paired: 0 · Connected: 0

排查步骤:

  1. 检查 SSH 隧道: lsof -i :18790
  2. 检查隧道日志: tail /tmp/openclaw-ssh-tunnel.log
  3. 重启服务:
    launchctl unload ~/Library/LaunchAgents/com.openclaw.*.plist
    sleep 5
    launchctl load ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist
    sleep 10
    launchctl load ~/Library/LaunchAgents/com.openclaw.node.plist
  4. 检查 Node 日志: tail /tmp/openclaw-node.log

命令被拦截

症状: AI 说命令被拒绝执行

排查步骤:

  1. 检查 exec-approvals 配置: cat ~/.openclaw/exec-approvals.json
  2. 确认命令在白名单中
  3. 检查 Node 日志中的拒绝原因
  4. 确保包含 shell: nodes run --raw 使用 /bin/sh 执行命令,必须将 /bin/sh/bin/bash 加入白名单:
    {
      "pattern": "/bin/sh",
      "id": "sh-shell-001"
    },
    {
      "pattern": "/bin/bash", 
      "id": "bash-shell-001"
    }

Exec 命令仍在 Gateway 执行,未路由到 Mac Node

症状: 执行 /exec echo $HOME 返回 /home/ec2-user 而非 /Users/<your-username>

可能原因:

  1. Gateway 和 Node 版本不匹配
  2. tools.exec 配置未正确设置
  3. OpenClaw 已知限制: 截至 v2026.2.6,exec 路由到 Node 功能存在 bug

排查步骤:

  1. 检查版本一致性:

    # Gateway 版本
    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw --version'
    
    # Node 版本 (从 Node 日志中查看)
    # 两者必须一致!
  2. 检查 exec 配置:

    openclaw config get tools.exec
    # 应该显示:
    # {
    #   "host": "node",
    #   "node": "<your-node-name>",
    #   "security": "allowlist"
    # }
  3. 检查 Node 连接状态:

    openclaw nodes status
    # 确认显示: paired · connected
  4. 验证 exec 实际执行位置:

    # 在 Telegram 中测试
    /exec echo $HOME
    # 如果返回 /home/ec2-user = 在 Gateway 执行 ❌
    # 如果返回 /Users/<your-username> = 在 Mac 执行 ✅

已知限制:

  • 截至 OpenClaw v2026.2.6,即使配置完全正确,exec 命令仍可能只在 Gateway 上执行,无法路由到 Mac Node
  • 这是软件本身的 bug,非配置问题

Gateway 配置更新后启动失败

症状: systemctl --user status openclaw-gateway 显示 activating (auto-restart)exit-code

可能原因:

  1. 配置文件 JSON 语法错误
  2. 使用了不支持的配置项(如旧版 agents.defaults.tools
  3. 配置项位置错误

解决方法:

  1. 验证 JSON 语法:

    cat ~/.openclaw/openclaw.json | python3 -m json.tool > /dev/null && echo "JSON OK"
  2. 检查不支持的配置项:

    • OpenClaw v2026.1.29 不支持 agents.defaults.tools
    • 配置应放在根级别的 tools.exec
  3. 从备份恢复:

    cp ~/.openclaw/openclaw.json.bak.3 ~/.openclaw/openclaw.json
    systemctl --user restart openclaw-gateway
  4. 使用 doctor 修复:

    openclaw doctor --fix

Systemd 服务版本不匹配

症状: Gateway 已升级但 systemctl status 仍显示旧版本

解决方法:

# 更新 systemd 服务文件中的版本描述
sed -i 's/v2026.1.29/v2026.2.6/' ~/.config/systemd/user/openclaw-gateway.service
systemctl --user daemon-reload
systemctl --user restart openclaw-gateway

Gateway 崩溃/无响应

症状: Telegram Bot 无响应

排查步骤:

  1. SSH 到 EC2: ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip>
  2. 检查 Gateway 状态: systemctl --user status openclaw-gateway
  3. 重启 Gateway: systemctl --user restart openclaw-gateway
  4. 查看日志: journalctl --user -u openclaw-gateway -f

备份与恢复

需要备份的文件

Mac 本地:

  • ~/.openclaw/ 目录 (Node 配置、Exec Approvals)
  • ~/.ssh/openclaw.pem (SSH 密钥)
  • ~/Library/LaunchAgents/com.openclaw.*.plist (服务配置)

EC2 服务器:

  • /home/ec2-user/.openclaw/ 目录 (Gateway 配置、会话、凭证)
  • Systemd 服务文件 (如果自定义过)

备份脚本

#!/bin/bash
# backup-openclaw.sh

BACKUP_DIR="$HOME/openclaw-backup-$(date +%Y%m%d)"
mkdir -p "$BACKUP_DIR"

# Mac 本地
cp -r ~/.openclaw "$BACKUP_DIR/"
cp ~/.ssh/openclaw.pem "$BACKUP_DIR/"
cp ~/Library/LaunchAgents/com.openclaw.*.plist "$BACKUP_DIR/"

# EC2 (通过 SSH)
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'tar czf - -C /home/ec2-user .openclaw' > "$BACKUP_DIR/ec2-openclaw.tar.gz"

echo "备份完成: $BACKUP_DIR"

恢复步骤

  1. Mac 恢复:

    # 停止服务
    launchctl unload ~/Library/LaunchAgents/com.openclaw.*.plist
    
    # 恢复文件
    cp -r backup/.openclaw ~/
    cp backup/openclaw.pem ~/.ssh/
    chmod 400 ~/.ssh/openclaw.pem
    cp backup/com.openclaw.*.plist ~/Library/LaunchAgents/
    
    # 启动服务
    launchctl load ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist
    sleep 10
    launchctl load ~/Library/LaunchAgents/com.openclaw.node.plist
  2. EC2 恢复:

    # 停止 Gateway
    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'systemctl --user stop openclaw-gateway'
    
    # 恢复配置
    scp -i ~/.ssh/openclaw.pem ec2-openclaw.tar.gz ec2-user@<your-ec2-ip>:/tmp/
    ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
      'rm -rf ~/.openclaw && tar xzf /tmp/ec2-openclaw.tar.gz -C ~ && systemctl --user start openclaw-gateway'

安全命令

# 运行 OpenClaw 安全审计 (EC2 上)
ssh -i ~/.ssh/openclaw.pem ec2-user@<your-ec2-ip> \
  'export PATH=$HOME/.local/share/fnm/bin:$PATH && eval $(fnm env) && openclaw security audit'

# 检查文件权限 (Mac)
ls -la ~/.openclaw/
ls -la ~/.ssh/openclaw.pem

通用安全建议

1. AWS 账户安全

  • 使用 IAM 用户: 不要使用 AWS 根账户进行日常操作
  • 最小权限原则: 为 IAM 用户仅授予必要的权限
  • 启用 MFA: 为 AWS 账户和 IAM 用户启用多因素认证
  • 定期轮换密钥: 定期更换 AWS Access Key 和 Secret Key
  • 监控账单: 设置账单告警,防止意外费用

2. API Key 保护

  • 不要提交到代码仓库: API Key 永远不应该出现在 Git 历史中
  • 使用环境变量: 将敏感信息存储在环境变量中
  • 定期轮换: 定期更换 API Key,尤其是怀疑泄露时
  • 限制范围: 为不同用途使用不同的 API Key
  • 监控使用: 定期检查 API 调用日志
# 检查是否意外提交了敏感信息
cd ~/.openclaw
git log --all --full-history -- . | grep -i "token\|key\|secret" || echo "No secrets found"

3. Telegram Bot 安全

  • 使用配对模式: dmPolicy: "pairing" 而非 "open"
  • 限制 allowFrom: 只允许你的 Telegram 用户 ID
  • 不要在群组中暴露 Bot Token: Token 泄露后任何人都可以控制 Bot
  • 定期检查配对列表:
    openclaw pairing list telegram

4. SSH 安全

  • 密钥权限: 私钥权限必须为 400 (chmod 400 key.pem)
  • 不要使用密码认证: 始终使用密钥对认证
  • 限制来源 IP: 如可能,限制 SSH 只允许特定 IP
  • 使用跳板机: 考虑使用堡垒机作为唯一 SSH 入口
  • 定期更换密钥: 建议每 6-12 个月更换一次

5. Gateway 安全

  • 绑定 loopback: Gateway 只监听 127.0.0.1
  • 使用强 Token: Gateway Token 应随机生成,长度至少 32 位
  • 定期更换 Token:
    # 生成新 Token
    openclaw doctor --generate-gateway-token
    # 更新配置并重启
    openclaw config set gateway.auth.token "new-token"
    systemctl --user restart openclaw-gateway
  • 监控日志: 定期检查异常访问

6. Node 执行安全

  • 使用 allowlist 模式: 不要设置为 fulldeny
  • 限制 PATH: Exec Approvals 中的命令使用完整路径
  • 定期审查白名单: 移除不再使用的命令
  • 警惕通配符: * 通配符要谨慎使用(如 /opt/homebrew/bin/*

7. 网络安全

  • 使用 SSH 隧道: 不要直接暴露 Gateway 到公网
  • 启用防火墙: EC2 安全组只开放必要的端口
  • 使用 VPC: 将 EC2 放在私有子网(配合 NAT Gateway)
  • 启用 CloudWatch: 监控网络流量和异常行为

8. 数据安全

  • 加密存储: EC2 使用加密 EBS 卷
  • 定期备份: 备份 ~/.openclaw/ 目录
  • 敏感数据: 不要将密码、密钥存储在会话历史中
  • 日志清理: 定期清理旧日志文件
# 清理旧日志(保留最近 30 天)
find /tmp/openclaw -name "*.log" -mtime +30 -delete

9. 系统安全

  • 及时更新: 定期更新系统和依赖包
# Amazon Linux
sudo yum update -y

# Mac
brew update && brew upgrade
  • 监控进程: 使用 tophtop 监控资源使用
  • 配置 Swap: 防止 OOM 导致服务崩溃
  • 设置 OOM 保护: 保护关键进程(如 sshd)

10. 应急响应

如果怀疑系统被入侵:

  1. 立即隔离:

    # 停止 Gateway
    systemctl --user stop openclaw-gateway
    # 或终止所有相关进程
    pkill -f openclaw
  2. 更换所有凭证:

    • AWS Access Key
    • Telegram Bot Token
    • Gateway Token
    • API Key
  3. 检查日志:

    # 查看最近的可疑活动
    tail -1000 /tmp/openclaw/openclaw-*.log | grep -i "error\|unauthorized\|denied"
    
    # 查看 SSH 登录记录
    last -20
  4. 审查文件:

    # 检查最近修改的文件
    find ~/.openclaw -mtime -1 -type f
    
    # 检查配置是否被篡改
    git -C ~/.openclaw diff
  5. 重新部署: 如有必要,创建新实例并迁移配置


参考资源


文档版本: 1.1
最后更新: 2026-02-07