Loading... ``` # OpenClaw 云服务器部署 + 远程控制本地 macOS 完整指南 ## 目录 1. [架构概述](#1-架构概述) 2. [Debian 云服务器部署](#2-debian-云服务器部署-openclaw-gateway) 3. [本地 macOS 配置](#3-本地-macos-配置) 4. [Telegram 集成](#4-telegram-集成) 5. [安全配置](#5-安全配置) 6. [故障排查](#6-故障排查) --- ## 1. 架构概述 ### 目标架构 ``` ┌─────────────┐ ┌──────────────────┐ ┌─────────────────┐ │ Telegram │────▶│ Debian 云服务器 │────▶│ 本地 macOS │ │ (手机/电脑) │ │ (OpenClaw Gateway) │ │ (Node Host) │ └─────────────┘ │ - 处理 Telegram │ │ - 执行命令 │ │ - 运行 AI 模型 │ │ - 文件操作 │ │ - 路由工具调用 │ └─────────────────┘ └──────────────────┘ ▲ │ SSH 隧道 │ (本地 18789 → 远程 18789) │ ┌──────┴──────┐ │ macOS SSH 客户端 │ └─────────────────┘ ``` ### 工作流程 1. **Telegram 消息** → 云服务器 Gateway 2. **Gateway** 处理消息,调用 AI 模型 3. **AI 决策** 需要执行命令时,通过 WebSocket 转发到 macOS Node 4. **macOS Node** 执行命令并返回结果 5. **结果** → Gateway → Telegram --- ## 2. Debian 云服务器部署 (OpenClaw Gateway) ### 2.1 系统要求 - **系统**: Debian 12 (Bookworm) 或 Debian 11 (Bullseye) - **配置**: 最低 1 核 CPU、1GB 内存、20GB 存储 - **网络**: 公网 IP ### 2.2 安装 OpenClaw ```bash # 1. 更新系统 sudo apt update && sudo apt upgrade -y # 2. 安装依赖 sudo apt install -y curl git ca-certificates gnupg # 3. 安装 Node.js (使用 NodeSource,Debian 官方源版本较旧) curl -fsSL https://deb.nodesource.com/setup_20.x | sudo -E bash - sudo apt install -y nodejs # 4. 验证安装 node --version # 应显示 v20.x.x npm --version # 5. 安装 OpenClaw curl -fsSL https://openclaw.ai/install.sh | bash ``` ### 2.3 配置 Gateway 编辑配置文件 `/root/.openclaw/openclaw.json`: ```bash nano /root/.openclaw/openclaw.json ``` #### 最小配置(推荐起点) ```json5 { // Gateway 配置 gateway: { mode: "local", port: 18789, bind: "loopback", // 只绑定本地,通过 SSH 隧道访问 auth: { mode: "token", token: "your-secure-token-here" // 设置一个强密码 } }, // Agent 默认配置 agents: { defaults: { workspace: "/root/.openclaw/workspace", model: { primary: "anthropic/claude-opus-4-6" } } }, // Telegram 配置 channels: { telegram: { enabled: true, botToken: "YOUR_TELEGRAM_BOT_TOKEN", dmPolicy: "pairing", // 需要配对才能使用 groups: { "*": { requireMention: true } } } } } ``` #### 完整配置示例 ```json5 { // Gateway 配置 gateway: { mode: "local", port: 18789, bind: "loopback", auth: { mode: "token", token: "your-secure-token-here", allowTailscale: false }, controlUi: { enabled: true, basePath: "/" } }, // Agent 配置 agents: { defaults: { workspace: "/root/.openclaw/workspace", model: { primary: "anthropic/claude-opus-4-6", fallbacks: ["openai/gpt-4o"] }, maxConcurrent: 3, thinkingDefault: "low" } }, // 认证配置 auth: { profiles: { "anthropic:default": { provider: "anthropic", mode: "api_key" } } }, // Telegram 配置 channels: { telegram: { enabled: true, botToken: "YOUR_TELEGRAM_BOT_TOKEN", dmPolicy: "pairing", allowFrom: [], // 留空使用 pairing,或添加你的 user id groups: { "*": { requireMention: true } }, historyLimit: 50, replyToMode: "first", streamMode: "partial" } }, // 工具配置 tools: { exec: { host: "node", // 默认使用远程 node 执行命令 security: "allowlist" }, web: { search: { enabled: true, apiKey: "${BRAVE_API_KEY}", maxResults: 5 }, fetch: { enabled: true } } }, // 日志配置 logging: { level: "info", consoleLevel: "info", redactSensitive: "tools" } } ``` ### 2.4 设置 API Key ```bash # 设置 Anthropic API Key export ANTHROPIC_API_KEY="your-anthropic-api-key" # 或写入 .env 文件(推荐用于 systemd) cat >> /root/.openclaw/.env << 'EOF' ANTHROPIC_API_KEY=your-anthropic-api-key EOF ``` ### 2.5 创建 Systemd 服务 ```bash cat > /etc/systemd/system/openclaw.service << 'EOF' [Unit] Description=OpenClaw AI Agent Gateway After=network.target [Service] Type=simple User=root WorkingDirectory=/root ExecStart=/usr/bin/openclaw gateway Restart=always RestartSec=10 Environment=HOME=/root Environment="OPENCLAW_GATEWAY_TOKEN=your-secure-token-here" [Install] WantedBy=multi-user.target EOF # 重新加载并启动 sudo systemctl daemon-reload sudo systemctl enable openclaw sudo systemctl start openclaw # 检查状态 sudo systemctl status openclaw ``` ### 2.6 验证 Gateway 运行 ```bash # 检查服务状态 sudo systemctl status openclaw # 查看日志 journalctl -u openclaw -f # 或使用 OpenClaw CLI openclaw status openclaw health ``` --- ## 3. 本地 macOS 配置 ### 3.1 安装 OpenClaw ```bash # 使用 Homebrew 或一键安装脚本 curl -fsSL https://openclaw.ai/install.sh | bash ``` ### 3.2 配置 SSH 隧道 #### 方式一:手动 SSH 隧道 ```bash # 创建 SSH 配置 cat >> ~/.ssh/config << 'EOF' Host openclaw-server HostName YOUR_SERVER_IP User root Port 22 LocalForward 18789 127.0.0.1:18789 IdentityFile ~/.ssh/id_rsa ServerAliveInterval 60 ServerAliveCountMax 3 EOF # 启动 SSH 隧道 ssh -N openclaw-server & ``` #### 方式二:LaunchAgent 自动启动隧道(推荐) ```bash # 创建 LaunchAgent plist cat > ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist << 'EOF' <?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>openclaw-server</string> </array> <key>RunAtLoad</key> <true/> <key>KeepAlive</key> <true/> <key>StandardOutPath</key> <string>/tmp/openclaw-tunnel.log</string> <key>StandardErrorPath</key> <string>/tmp/openclaw-tunnel.log</string> </dict> </plist> EOF # 加载服务 launchctl load ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist ``` ### 3.3 配置 macOS CLI 连接远程 Gateway 编辑 `~/.openclaw/openclaw.json`: ```bash nano ~/.openclaw/openclaw.json ``` ```json5 { gateway: { mode: "remote", remote: { url: "ws://127.0.0.1:18789", token: "your-secure-token-here" // 与服务器上设置的 token 相同 } } } ``` ### 3.4 验证连接 ```bash # 测试连接 openclaw status openclaw health # 应该显示远程 Gateway 的状态 ``` --- ## 4. Telegram 集成 ### 4.1 创建 Telegram Bot 1. 打开 Telegram,搜索 `@BotFather` 2. 发送 `/newbot` 3. 按提示设置机器人名称和用户名 4. 复制获得的 **Bot Token** ### 4.2 获取你的 Telegram User ID 1. 搜索 `@userinfobot` 或 `@my_id_bot` 2. 发送任意消息 3. 记录返回的 User ID ### 4.3 更新服务器配置 在服务器上编辑 `/root/.openclaw/openclaw.json`: ```json5 { channels: { telegram: { enabled: true, botToken: "123456789:ABCdefGHIjklMNOpqrsTUVwxyz", dmPolicy: "pairing", allowFrom: [123456789], // 你的 Telegram User ID groups: { "*": { requireMention: true } } } } } ``` ### 4.4 重启 Gateway ```bash # 在服务器上 sudo systemctl restart openclaw ``` ### 4.5 配对 Telegram 1. 在 Telegram 中向你的机器人发送 `/start` 2. 在服务器上查看配对请求: ```bash openclaw pairing list telegram ``` 3. 批准配对: ```bash openclaw pairing approve telegram <CODE> ``` ### 4.6 测试 Telegram 控制 在 Telegram 中向机器人发送消息,例如: - `你好` - `在 macOS 上列出当前目录的文件` - `执行 uname -a` --- ## 5. 安全配置 ### 5.1 重要安全警告 ⚠️ **OpenClaw 具有完整的系统访问权限**,包括: - 执行任意 shell 命令 - 读写文件系统 - 访问网络 - 控制浏览器 ### 5.2 安全最佳实践 #### 服务器端 ```bash # 设置正确的文件权限 chmod 600 /root/.openclaw/openclaw.json chmod 700 /root/.openclaw ``` #### 配置安全选项 ```json5 { // Gateway 绑定到 loopback,不直接暴露到公网 gateway: { bind: "loopback", // 只允许本地连接 auth: { mode: "token", token: "使用强随机密码" } }, // 只允许特定用户 channels: { telegram: { dmPolicy: "pairing", // 需要配对 allowFrom: [123456789], // 你的 User ID groups: { "*": { requireMention: true } // 群组需要 @ 提及 } } }, // 工具限制 tools: { exec: { security: "ask", // 执行命令前询问 backgroundMs: 10000 }, elevated: { enabled: false // 禁用提升权限 } } } ``` #### macOS 端 Exec 批准 ```bash # 编辑 macOS 上的 exec 批准配置 nano ~/.openclaw/exec-approvals.json ``` ```json5 { "mode": "ask", // ask | allowlist | full "allowlist": { "/bin/ls": {}, "/bin/cat": {}, "/usr/bin/uname": {}, "/usr/bin/sw_vers": {} } } ``` ### 5.3 防火墙配置 ```bash # 在服务器上配置防火墙(使用 ufw) sudo apt install -y ufw # 默认策略 sudo ufw default deny incoming sudo ufw default allow outgoing # 允许 SSH sudo ufw allow 22/tcp # 不需要开放 18789 端口(使用 SSH 隧道) # 启用防火墙 sudo ufw enable ``` ### 5.4 更新到最新版本 ```bash # 定期更新 OpenClaw openclaw update ``` --- ## 6. 故障排查 ### 6.1 服务器端 #### Gateway 无法启动 ```bash # 查看详细日志 journalctl -u openclaw -n 50 --no-pager # 或直接运行查看错误 openclaw gateway ``` #### Telegram 机器人无响应 ```bash # 检查 Gateway 状态 openclaw status # 检查日志 openclaw logs --follow # 验证 Bot Token curl "https://api.telegram.org/bot<YOUR_TOKEN>/getMe" ``` ### 6.2 macOS 端 #### SSH 隧道断开 ```bash # 检查隧道状态 ps aux | grep "ssh.*openclaw-server" # 查看隧道日志 tail -f /tmp/openclaw-tunnel.log # 重启隧道 launchctl unload ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist launchctl load ~/Library/LaunchAgents/com.openclaw.ssh-tunnel.plist ``` #### 无法连接到远程 Gateway ```bash # 测试 SSH 隧道 curl http://127.0.0.1:18789/health # 查看 CLI 配置 openclaw config list # 测试远程连接 OPENCLAW_GATEWAY_TOKEN="your-token" openclaw health ``` ### 6.3 Node 相关 #### 查看节点状态 ```bash # 在服务器上查看连接的节点 openclaw nodes list # 查看待批准的节点 openclaw nodes pending # 查看节点详情 openclaw nodes describe --node <node-id> ``` #### 批准节点 ```bash # 批准节点配对 openclaw nodes approve <node-id> # 重命名节点 openclaw nodes rename --node <node-id> --name "My MacBook" ``` ### 6.4 常用诊断命令 ```bash # 完整状态检查 openclaw doctor # 检查所有会话 openclaw sessions list # 查看特定会话 openclaw sessions show <session-key> # 运行安全审计 openclaw security audit ``` --- ## 附录:快速配置清单 ### 服务器端 (Debian) - [ ] 安装 OpenClaw - [ ] 配置 `/root/.openclaw/openclaw.json` - [ ] 设置 API Keys (ANTHROPIC_API_KEY) - [ ] 配置 Telegram bot token - [ ] 创建 systemd 服务 - [ ] 启动服务 `sudo systemctl start openclaw` - [ ] 验证服务状态 `sudo systemctl status openclaw` ### macOS 端 - [ ] 安装 OpenClaw - [ ] 配置 SSH 隧道 (`~/.ssh/config`) - [ ] 创建 LaunchAgent 自动启动隧道 - [ ] 配置 `~/.openclaw/openclaw.json` (remote mode) - [ ] 验证连接 `openclaw status` ### Telegram - [ ] 创建 Bot (@BotFather) - [ ] 获取 Bot Token - [ ] 获取 User ID (@userinfobot) - [ ] 配置服务器 allowFrom - [ ] 发送 /start 测试 - [ ] 批准 pairing `openclaw pairing approve telegram <CODE>` --- **参考来源:** - [OpenClaw 官方文档](https://docs.openclaw.ai) - [OpenClaw GitHub](https://github.com/openclaw/openclaw) - [Node 配置文档](https://docs.openclaw.ai/nodes) - [Tailscale 集成](https://docs.openclaw.ai/gateway/tailscale) ``` ``` 最后修改:2026 年 02 月 07 日 © 允许规范转载 打赏 赞赏作者 支付宝微信 赞 如果觉得我的文章对你有用,请随意赞赏