OpenClaw
安全中心10 分钟阅读

如何安全地远程访问 OpenClaw

学习如何使用 SSH 隧道、反向代理和 VPN 安全地暴露您的 OpenClaw 网关以进行远程访问,同时保持安全性。

O

OpenClaw Guides

Tutorial Authors

为什么需要远程访问?

默认情况下,OpenClaw 绑定到 127.0.0.1(仅本地主机),这是最安全的配置。但是,在以下情况下您可能需要远程访问:

  • 在家庭服务器上运行 OpenClaw,但需要外出访问
  • 在 VPS 上托管并从多个设备连接
  • 在本地网络中共享您的 AI 助手

警告: 切勿在没有适当身份验证和加密的情况下直接将 OpenClaw 暴露到互联网。本指南仅介绍安全的方法。

方法1:SSH 隧道(推荐用于个人使用)

SSH 隧道是个人远程访问最简单、最安全的方法。

在远程服务器上

确保 OpenClaw 在 localhost 上运行:

bash
openclaw gateway start
# Gateway listening on http://127.0.0.1:18789

在本地机器上

创建 SSH 隧道:

bash
ssh -L 18789:localhost:18789 user@your-server-ip

现在您可以在本地机器上通过 http://localhost:18789 访问 OpenClaw。

使用 autossh 创建持久 SSH 隧道

对于自动重连的隧道:

bash
# 安装 autossh
# macOS
brew install autossh

# Ubuntu/Debian
sudo apt install autossh

# 创建持久隧道
autossh -M 0 -f -N -L 18789:localhost:18789 user@your-server-ip

方法2:使用 Nginx 反向代理

对于更高级的设置,使用 Nginx 作为带 SSL 终止的反向代理。

安装 Nginx 和 Certbot

bash
# Ubuntu/Debian
sudo apt update
sudo apt install nginx certbot python3-certbot-nginx

配置 Nginx

创建 /etc/nginx/sites-available/openclaw

nginx
server {
    listen 80;
    server_name openclaw.yourdomain.com;

    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;
        proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
        proxy_set_header X-Forwarded-Proto $scheme;
        proxy_cache_bypass $http_upgrade;
    }
}

启用站点:

bash
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
sudo nginx -t
sudo systemctl reload nginx

使用 Let's Encrypt 添加 SSL

bash
sudo certbot --nginx -d openclaw.yourdomain.com

添加基本身份验证

生成密码文件:

bash
sudo apt install apache2-utils
sudo htpasswd -c /etc/nginx/.htpasswd your-username

更新 Nginx 配置:

nginx
server {
    listen 443 ssl;
    server_name openclaw.yourdomain.com;

    # SSL 配置由 certbot 添加...

    location / {
        auth_basic "OpenClaw Access";
        auth_basic_user_file /etc/nginx/.htpasswd;

        proxy_pass http://127.0.0.1:18789;
        # ... 其他代理设置
    }
}

方法3:WireGuard VPN

为了最高安全性,使用 VPN 访问您的家庭网络。

安装 WireGuard

bash
# 服务器(Ubuntu/Debian)
sudo apt install wireguard

# 生成密钥
wg genkey | tee privatekey | wg pubkey > publickey

服务器配置

创建 /etc/wireguard/wg0.conf

ini
[Interface]
PrivateKey = <server-private-key>
Address = 10.0.0.1/24
ListenPort = 51820

[Peer]
PublicKey = <client-public-key>
AllowedIPs = 10.0.0.2/32

客户端配置

ini
[Interface]
PrivateKey = <client-private-key>
Address = 10.0.0.2/24

[Peer]
PublicKey = <server-public-key>
Endpoint = your-server-ip:51820
AllowedIPs = 10.0.0.1/32
PersistentKeepalive = 25

启动 WireGuard

bash
# 服务器
sudo wg-quick up wg0
sudo systemctl enable wg-quick@wg0

# 客户端
sudo wg-quick up wg0

现在通过 VPN IP 访问 OpenClaw:http://10.0.0.1:18789

方法4:Cloudflare Tunnel(零信任)

Cloudflare Tunnel 提供无需暴露端口的安全访问。

安装 cloudflared

bash
# macOS
brew install cloudflare/cloudflare/cloudflared

# Linux
curl -L --output cloudflared.deb https://github.com/cloudflare/cloudflared/releases/latest/download/cloudflared-linux-amd64.deb
sudo dpkg -i cloudflared.deb

认证并创建隧道

bash
cloudflared tunnel login
cloudflared tunnel create openclaw

配置隧道

创建 ~/.cloudflared/config.yml

yaml
tunnel: <tunnel-id>
credentials-file: /home/user/.cloudflared/<tunnel-id>.json

ingress:
  - hostname: openclaw.yourdomain.com
    service: http://localhost:18789
  - service: http_status:404

运行隧道

bash
cloudflared tunnel route dns openclaw openclaw.yourdomain.com
cloudflared tunnel run openclaw

安全最佳实践

1. 启用速率限制

在您的 ~/.openclaw/openclaw.json 中:

json
{
  "security": {
    "rateLimiting": {
      "enabled": true,
      "maxRequests": 60,
      "windowMs": 60000
    }
  }
}

2. 使用强 API 密钥

bash
# 定期轮换 API 密钥
openclaw config set api-key

3. 监控访问日志

bash
# 检查网关日志
openclaw logs --follow

4. 设置 Fail2Ban(用于 Nginx)

bash
sudo apt install fail2ban

# 创建 /etc/fail2ban/jail.local
[nginx-http-auth]
enabled = true

对比表

| 方法 | 安全性 | 复杂度 | 最适合 | |--------|----------|------------|----------| | SSH 隧道 | 高 | 低 | 个人使用 | | Nginx + SSL | 高 | 中 | 公共访问 | | WireGuard VPN | 非常高 | 中 | 团队访问 | | Cloudflare Tunnel | 高 | 低 | 零信任设置 |

后续步骤