> ## Documentation Index
> Fetch the complete documentation index at: https://openclaw.veiseule.ai/llms.txt
> Use this file to discover all available pages before exploring further.

# Thiết lập Gateway từ xa

# Chạy OpenClaw\.app với Gateway từ xa

OpenClaw\.app dùng đường hầm SSH để kết nối tới gateway từ xa. Hướng dẫn này cho bạn thấy cách thiết lập.

## Tổng quan

```mermaid theme={"theme":{"light":"min-light","dark":"min-dark"}}
%%{init: {
  'theme': 'base',
  'themeVariables': {
    'primaryColor': '#ffffff',
    'primaryTextColor': '#000000',
    'primaryBorderColor': '#000000',
    'lineColor': '#000000',
    'secondaryColor': '#f9f9fb',
    'tertiaryColor': '#ffffff',
    'clusterBkg': '#f9f9fb',
    'clusterBorder': '#000000',
    'nodeBorder': '#000000',
    'mainBkg': '#ffffff',
    'edgeLabelBackground': '#ffffff'
  }
}}%%
flowchart TB
    subgraph Client["Client Machine"]
        direction TB
        A["OpenClaw.app"]
        B["ws://127.0.0.1:18789\n(local port)"]
        T["SSH Tunnel"]

        A --> B
        B --> T
    end
    subgraph Remote["Remote Machine"]
        direction TB
        C["Gateway WebSocket"]
        D["ws://127.0.0.1:18789"]

        C --> D
    end
    T --> C
```

## Thiết lập nhanh

### Bước 1: Thêm cấu hình SSH

Chỉnh sửa `~/.ssh/config` và thêm:

```ssh theme={"theme":{"light":"min-light","dark":"min-dark"}}
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
```

Thay `<REMOTE_IP>` và `<REMOTE_USER>` bằng giá trị của bạn.

### Bước 2: Sao chép khóa SSH

Sao chép khóa công khai của bạn lên máy từ xa (chỉ cần nhập mật khẩu một lần):

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
ssh-copy-id -i ~/.ssh/id_rsa <REMOTE_USER>@<REMOTE_IP>
```

### Bước 3: Đặt Gateway Token

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
launchctl setenv OPENCLAW_GATEWAY_TOKEN "<your-token>"
```

### Bước 4: Khởi động đường hầm SSH

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
ssh -N remote-gateway &
```

### Bước 5: Khởi động lại OpenClaw\.app

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
# Quit OpenClaw.app (⌘Q), then reopen:
open /path/to/OpenClaw.app
```

Ứng dụng bây giờ sẽ kết nối tới gateway từ xa thông qua đường hầm SSH.

***

## Tự động khởi động đường hầm khi đăng nhập

Để đường hầm SSH tự động khởi động khi bạn đăng nhập, hãy tạo một Launch Agent.

### Tạo file PLIST

Lưu file này thành `~/Library/LaunchAgents/bot.molt.ssh-tunnel.plist`:

```xml theme={"theme":{"light":"min-light","dark":"min-dark"}}
<?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>
```

### Nạp Launch Agent

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
launchctl bootstrap gui/$UID ~/Library/LaunchAgents/bot.molt.ssh-tunnel.plist
```

Đường hầm bây giờ sẽ:

* Tự động khởi động khi bạn đăng nhập
* Tự khởi động lại nếu bị lỗi
* Luôn chạy trong nền

Ghi chú cũ: nếu có, hãy xóa mọi LaunchAgent `com.openclaw.ssh-tunnel` còn sót lại.

***

## Xử lý sự cố

**Kiểm tra xem đường hầm có đang chạy không:**

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
ps aux | grep "ssh -N remote-gateway" | grep -v grep
lsof -i :18789
```

**Khởi động lại đường hầm:**

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
launchctl kickstart -k gui/$UID/bot.molt.ssh-tunnel
```

**Dừng đường hầm:**

```bash theme={"theme":{"light":"min-light","dark":"min-dark"}}
launchctl bootout gui/$UID/bot.molt.ssh-tunnel
```

***

## Cách hoạt động

| Thành phần                           | Chức năng                                                   |
| ------------------------------------ | ----------------------------------------------------------- |
| `LocalForward 18789 127.0.0.1:18789` | Chuyển tiếp cổng cục bộ 18789 tới cổng 18789 trên máy từ xa |
| `ssh -N`                             | SSH không thực thi lệnh từ xa (chỉ chuyển tiếp cổng)        |
| `KeepAlive`                          | Tự động khởi động lại đường hầm nếu bị lỗi                  |
| `RunAtLoad`                          | Khởi động đường hầm khi agent được nạp                      |

OpenClaw\.app kết nối tới `ws://127.0.0.1:18789` trên máy khách của bạn. Đường hầm SSH chuyển tiếp kết nối đó tới cổng 18789 trên máy từ xa nơi Gateway đang chạy.
