摘要:本文手把手教你从零部署 OpenClaw 网关,并通过 SSH 隧道实现安全的远程访问。涵盖 OpenClaw 安装配置、防火墙安全加固、SSH 隧道搭建(含 Windows/Mac/Linux 全平台方案),以及常见问题排查。无需公网暴露端口,5分钟搞定安全远程管理!
一、前言
在远程服务器管理工具层出不穷的今天,OpenClaw 凭借其轻量级、高性能的特点,成为众多开发者的新宠。但如何安全地远程访问它,避免直接将管理端口暴露在互联网上,是每个运维人员必须掌握的技能。
本文将带你完成一套生产级安全部署方案 :通过 SSH 隧道 实现本地安全访问,彻底杜绝公网暴露风险。
二、OpenClaw 安装与配置
2.1 初始化配置
bash
# 首次运行,生成默认配置
openclaw config get gateway
# 配置文件位置:~/.openclaw/openclaw.json
2.2 核心配置
bash
# 设置监听端口
openclaw config set gateway.port 8087
# 监听所有网卡(为 SSH 隧道做准备)
openclaw config set gateway.bind "lan"
# 设置强密码令牌(务必修改!)
openclaw config set gateway.auth.token "YourStrongToken123"
# 配置允许的来源(localhost 用于 SSH 隧道)
openclaw config set gateway.controlUi.allowedOrigins '["http://localhost:8087", "https://localhost:8087", "http://127.0.0.1:8087", "https://127.0.0.1:8087"]'
查看配置确认:
bash
openclaw config get gateway
输出示例:
json
{
"port": 8087,
"bind": "lan",
"controlUi": {
"allowedOrigins": [
"http://localhost:8087",
"https://localhost:8087",
"http://127.0.0.1:8087",
"https://127.0.0.1:8087"
]
},
"auth": {
"mode": "token",
"token": "__OPENCLAW_REDACTED__"
}
}
三、启动服务与防火墙加固
3.1 启动网关
bash
# 前台调试
openclaw gateway run
# 后台运行
openclaw gateway run &
# 或设为系统服务(推荐生产环境)
openclaw gateway install
检查状态确认:
bash
openclaw gateway status
确认显示:
Runtime: runningListening: *:8087bind=lan (0.0.0.0)
3.2 设置开机自启(可选)
bash
# 创建 systemd 服务
sudo tee /etc/systemd/system/openclaw.service << 'EOF'
[Unit]
Description=OpenClaw Gateway
After=network.target
[Service]
Type=simple
User=root
ExecStart=/root/.nvm/versions/node/v22/bin/openclaw gateway run
Restart=always
[Install]
WantedBy=multi-user.target
EOF
sudo systemctl daemon-reload
sudo systemctl enable openclaw
sudo systemctl start openclaw
3.3 🔒 关键安全步骤:关闭公网访问
bash
# 禁止 8087 端口公网访问(只允许本地)
sudo ufw deny 8087/tcp
# 或
sudo iptables -A INPUT -p tcp --dport 8087 -j DROP
# 开放 SSH 端口(示例为 26509)
sudo ufw allow 26509/tcp
⚠️ 安全警告:永远不要将 OpenClaw 管理端口直接暴露在公网!
四、SSH 隧道配置(全平台)
SSH 隧道是本方案的核心,它将服务器的 127.0.0.1:8087 映射到你本地的 localhost:8087,实现加密安全访问。
4.1 临时连接(命令行)
全平台通用:
bash
ssh -p 26509 -L 8087:127.0.0.1:8087 root@103.47.81.189
保持终端运行,不要关闭!
4.2 后台运行(推荐)
Mac / Linux:
bash
ssh -fN -p 26509 -L 8087:127.0.0.1:8087 root@103.47.81.189
关闭隧道:
bash
pkill -f "ssh.*26509.*8087"
4.3 配置 SSH 别名(一劳永逸)
编辑 ~/.ssh/config(Mac/Linux)或 C:\Users\用户名\.ssh\config(Windows):
bash
Host openclaw
HostName 103.47.81.189
User root
Port 26509
LocalForward 8087 127.0.0.1:8087
ServerAliveInterval 60
ServerAliveCountMax 3
之后只需执行:
bash
ssh openclaw
4.4 Windows PuTTY 配置
- 打开 PuTTY → Session:Host Name 填
103.47.81.189,Port 填26509 - Connection → SSH → Tunnels :
- Source port:
8087 - Destination:
127.0.0.1:8087 - 点击 Add
- Source port:
- Connection :设置
Seconds between keepalives: 60 - 点击 Open 连接
五、浏览器访问 OpenClaw
5.1 确认隧道已建立
bash
# Mac/Linux
lsof -i :8087
curl http://localhost:8087
# Windows
netstat -an | findstr 8087
5.2 登录使用
浏览器访问:http://localhost:8087
| 配置项 | 填写内容 |
|---|---|
| WebSocket URL | ws://localhost:8087(自动填充) |
| 网关令牌 | 你设置的 token(如 YourStrongToken123) |
| 密码 | 可选,不填 |
点击连接,开始使用!
六、常用命令速查表
| 操作 | 命令 |
|---|---|
| 启动服务 | openclaw gateway run |
| 停止服务 | pkill openclaw 或 systemctl stop openclaw |
| 查看状态 | openclaw gateway status |
| 查看配置 | openclaw config get gateway |
| 修改令牌 | openclaw config set gateway.auth.token "新密码" |
| 查看日志 | tail -f /tmp/openclaw/openclaw-*.log |
| 更新版本 | openclaw update |
| 卸载 | openclaw uninstall |
七、安全加固(工信部建议)
bash
# 1. 创建专用用户(不要用 root 运行)
sudo adduser --shell /bin/rbash --disabled-password clawuser
# 2. 限制文件访问权限
sudo chmod 700 /root/.openclaw
# 3. 开启详细日志
openclaw gateway --log-level debug >> /var/log/openclaw.log 2>&1
# 4. 定期安全审计
openclaw security audit
八、故障排查指南
| 现象 | 原因 | 解决方案 |
|---|---|---|
localhost refused to connect |
SSH 隧道断开 | 重新建立 SSH 连接 |
origin not allowed |
CORS 配置错误 | 检查 allowedOrigins 是否包含 localhost |
disconnected (1006) |
WebSocket 断开 | 刷新页面,检查 OpenClaw 状态 |
control ui requires device identity |
直接访问 IP | 必须使用 SSH 隧道或 HTTPS |
| 页面空白 | 服务未启动 | 服务器执行 openclaw gateway run |
九、写在最后
通过本文的方案,你不仅完成了 OpenClaw 的部署,更重要的是建立了一套安全的远程访问机制。SSH 隧道作为老牌的安全方案,配合正确的防火墙策略,能为你的服务提供企业级的安全防护。
如果你在部署过程中遇到任何问题,欢迎在评论区留言交流!如果觉得本文对你有帮助,别忘了点赞、收藏和转发,让更多人看到这份安全部署指南 🙌
标签: OpenClaw SSH隧道 远程访问安全
📌 版权声明:本文为原创内容,版权归作者所有,转载需注明出处。