Skip to content

使用远程 Gateway 运行 OpenClaw.app

OpenClaw.app 使用 SSH 隧道连接到远程 gateway。本指南说明如何完成设置。

概览

┌─────────────────────────────────────────────────────────────┐
│                        Client Machine                          │
│                                                              │
│  OpenClaw.app ──► ws://127.0.0.1:18789 (local port)           │
│                     │                                        │
│                     ▼                                        │
│  SSH Tunnel ────────────────────────────────────────────────│
│                     │                                        │
└─────────────────────┼──────────────────────────────────────┘


┌─────────────────────────────────────────────────────────────┐
│                         Remote Machine                        │
│                                                              │
│  Gateway WebSocket ──► ws://127.0.0.1:18789 ──►              │
│                                                              │
└─────────────────────────────────────────────────────────────┘

快速设置

步骤 1:添加 SSH 配置

编辑 ~/.ssh/config 并添加:

ssh
Host remote-gateway
    HostName <REMOTE_IP>          # e.g., 172.27.187.184
    User <REMOTE_USER>            # e.g., jefferson
    LocalForward 18789 127.0.0.1:18789
    IdentityFile ~/.ssh/id_rsa

<REMOTE_IP><REMOTE_USER> 替换为你的值。

步骤 2:复制 SSH Key

把你的公钥复制到远程机器(只需输入一次密码):

bash
ssh-copy-id -i ~/.ssh/id_rsa <REMOTE_USER>@<REMOTE_IP>

步骤 3:设置 Gateway Token

bash
launchctl setenv OPENCLAW_GATEWAY_TOKEN "<your-token>"

步骤 4:启动 SSH 隧道

bash
ssh -N remote-gateway &

步骤 5:重启 OpenClaw.app

bash
# Quit OpenClaw.app (⌘Q), then reopen:
open /path/to/OpenClaw.app

现在 app 会通过 SSH 隧道连接到远程 gateway。


登录时自动启动隧道

如果你希望 SSH 隧道在登录时自动启动,可以创建一个 Launch Agent。

创建 PLIST 文件

保存为 ~/Library/LaunchAgents/bot.molt.ssh-tunnel.plist

xml
<?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>bot.molt.ssh-tunnel</string>
    <key>ProgramArguments</key>
    <array>
        <string>/usr/bin/ssh</string>
        <string>-N</string>
        <string>remote-gateway</string>
    </array>
    <key>KeepAlive</key>
    <true/>
    <key>RunAtLoad</key>
    <true/>
</dict>
</plist>

加载 Launch Agent

bash
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/bot.molt.ssh-tunnel.plist

该隧道将会:

  • 在你登录时自动启动
  • 崩溃时自动重启
  • 在后台持续运行

Legacy 备注:如果存在遗留的 com.openclaw.ssh-tunnel LaunchAgent,请删除它。


排障

检查隧道是否在运行:

bash
ps aux | grep "ssh -N remote-gateway" | grep -v grep
lsof -i :18789

重启隧道:

bash
launchctl kickstart -k gui/$UID/bot.molt.ssh-tunnel

停止隧道:

bash
launchctl bootout gui/$UID/bot.molt.ssh-tunnel

工作原理

ComponentWhat It Does
LocalForward 18789 127.0.0.1:18789Forwards local port 18789 to remote port 18789
ssh -NSSH without executing remote commands (just port forwarding)
KeepAliveAutomatically restarts tunnel if it crashes
RunAtLoadStarts tunnel when the agent loads

OpenClaw.app 在客户端机器上连接 ws://127.0.0.1:18789。SSH 隧道会把该连接转发到远端机器的 18789 端口(Gateway 正运行在那台机器上)。