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 | 高 | 低 | 零信任設定 |

後續步驟