OpenClaw Linux 教程OpenClaw Linux 教程
首页
基础教程
最佳实践
OpenClaw 官网
编程指南
首页
基础教程
最佳实践
OpenClaw 官网
编程指南
  • 最佳实践

    • 🚀 最佳实践
    • 第1章 - 远程 Gateway 部署
    • 第2章 - 安全加固
    • 第3章 - 多 Agent 路由
    • 第4章 - 自动化工作流
    • 第5章 - 故障排除

第1章 - 远程 Gateway 部署

嗨,朋友!这一章我们来聊聊如何在远程 Linux 服务器上部署 OpenClaw Gateway,让你的 AI 助手 7×24 小时在线。

🤔 为什么要远程部署?

对比项本地运行远程服务器
可用性电脑关机就断了7×24 小时在线
稳定性受日常操作影响独立稳定运行
网络依赖本地网络服务器带宽稳定
成本免费VPS 费用(最低约 5 美元/月)

OpenClaw 官方也推荐在 Linux 上远程部署:"Linux is great" for running remote Gateway。

🖥️ 选择服务器

推荐配置

项目最低配置推荐配置
CPU1 核2 核
内存1 GB2 GB
存储10 GB20 GB
系统Ubuntu 22.04Ubuntu 24.04 LTS
网络能访问外网低延迟连接

推荐 VPS 提供商

  • Hetzner - 性价比极高,欧洲数据中心
  • DigitalOcean - 操作简单,全球节点
  • Vultr - 价格合理,多地区可选
  • AWS Lightsail - Amazon 云轻量级方案

🚀 部署步骤

第一步:SSH 连接到服务器

ssh root@你的服务器IP

第二步:创建普通用户

重要

不要用 root 用户运行 OpenClaw!创建一个普通用户:

# 创建用户
adduser openclaw

# 添加 sudo 权限
usermod -aG sudo openclaw

# 切换到新用户
su - openclaw

第三步:安装依赖和 OpenClaw

# 更新系统
sudo apt update && sudo apt upgrade -y

# 安装 Node.js 22
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
sudo apt install -y nodejs

# 安装 OpenClaw
curl -fsSL https://openclaw.ai/install.sh | bash

# 运行引导向导
openclaw onboard --install-daemon

第四步:配置开机自启

# 确保用户服务在注销后继续运行(非常重要!)
sudo loginctl enable-linger openclaw

# 验证
systemctl --user is-enabled openclaw-gateway

🔐 安全访问 Gateway

Gateway 默认只监听 127.0.0.1,外部无法直接访问。以下是安全访问的方式:

方式一:SSH 隧道(最简单)

在你的本地电脑上运行:

# 建立 SSH 隧道
ssh -L 18789:127.0.0.1:18789 openclaw@你的服务器IP

# 然后在本地浏览器打开
# http://127.0.0.1:18789/

设置别名简化操作:

# 添加到 ~/.bashrc 或 ~/.zshrc
alias openclaw-tunnel='ssh -L 18789:127.0.0.1:18789 openclaw@你的服务器IP -N'

方式二:Tailscale(推荐)

Tailscale 是一个零配置 VPN,可以让你安全地从任何地方访问服务器。

# 在服务器上安装 Tailscale
curl -fsSL https://tailscale.com/install.sh | sh

# 启动并认证
sudo tailscale up

# 记下你的 Tailscale IP
tailscale ip -4

然后在 openclaw.json 中配置 Tailscale 模式:

{
  "gateway": {
    "port": 18789,
    "bind": "127.0.0.1",
    "tailscale": {
      "mode": "serve"
    }
  }
}

Tailscale 模式说明:

模式说明安全性
off不使用 Tailscale(默认)-
serve仅 Tailnet 内部可访问🔒 安全
funnel公开 HTTPS 访问⚠️ 需要密码认证

注意

如果使用 funnel 模式,必须设置密码认证:

{
  "gateway": {
    "auth": {
      "mode": "password"
    }
  }
}

方式三:反向代理(Nginx)

如果你想通过域名访问,可以用 Nginx 反向代理:

# 安装 Nginx
sudo apt install -y nginx

# 创建配置
sudo tee /etc/nginx/sites-available/openclaw << 'EOF'
server {
    listen 443 ssl;
    server_name your-domain.com;

    ssl_certificate /etc/letsencrypt/live/your-domain.com/fullchain.pem;
    ssl_certificate_key /etc/letsencrypt/live/your-domain.com/privkey.pem;

    location / {
        proxy_pass http://127.0.0.1:18789;
        proxy_http_version 1.1;
        proxy_set_header Upgrade $http_upgrade;
        proxy_set_header Connection "upgrade";
        proxy_set_header Host $host;
        proxy_set_header X-Real-IP $remote_addr;
    }
}
EOF

# 启用配置
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

重要

使用反向代理时必须启用 HTTPS 和身份认证。绝对不要裸 HTTP 暴露 Gateway!

📊 服务器监控

自动重启脚本

#!/bin/bash
# 保存为 ~/monitor-openclaw.sh

if ! systemctl --user is-active openclaw-gateway > /dev/null 2>&1; then
    echo "[$(date)] Gateway 已停止,正在重启..." >> ~/openclaw-monitor.log
    systemctl --user restart openclaw-gateway
    echo "[$(date)] Gateway 已重启" >> ~/openclaw-monitor.log
fi
chmod +x ~/monitor-openclaw.sh

# 添加到 crontab,每分钟检查
crontab -e
# 添加:*/1 * * * * /home/openclaw/monitor-openclaw.sh

🏗️ 架构总览

远程部署后的完整架构:

你的手机/电脑
├── WhatsApp ───┐
├── Telegram ───┤
└── Discord ────┤
                │
                ▼
    ┌──────────────────────┐
    │  远程 Linux 服务器    │
    │                      │
    │  OpenClaw Gateway    │
    │  ws://127.0.0.1:18789│
    │                      │
    │  ├─ AI 模型 API      │
    │  ├─ 浏览器(headless)│
    │  ├─ Shell 执行       │
    │  └─ 技能插件         │
    └──────────────────────┘
         ↑
         │ SSH 隧道 / Tailscale
         │
    你的本地电脑(Dashboard 访问)

💪 练习题

  1. 为什么不应该用 root 用户运行 OpenClaw?
  2. enable-linger 命令的作用是什么?
  3. 比较 SSH 隧道和 Tailscale 两种远程访问方式的优缺点。

答案提示

  1. root 权限过大,OpenClaw 可以执行 Shell 命令,用 root 运行存在安全风险
  2. 让 systemd 用户服务在用户注销后继续运行,否则 SSH 断开后 Gateway 会停止
  3. SSH 隧道:简单直接,但需要手动建立连接;Tailscale:零配置 VPN,自动发现,但需要安装额外软件

下一步: 第2章 - 安全加固 →

最近更新: 2026/2/28 09:29
Contributors: 王长安
Prev
🚀 最佳实践
Next
第2章 - 安全加固