欢迎光临
我们一直在努力

VibeCoding - OpenClaw 公网访问配置指南 (自动化)

欢迎关注我的CSDN:https://spike.blog.csdn.net/ 本文地址:https://spike.blog.csdn.net/article/details/160054603

免责声明:本文来源于个人知识与公开资料,仅用于学术交流,欢迎讨论,不支持转载。


OpenClaw

详细教程:将 OpenClaw Gateway 配置为公网 IP + 端口访问模式

📋 目录

  • 背景介绍
  • 适用场景
  • 前置条件
  • 配置步骤
  • 完整配置示例
  • 访问验证
  • 常见问题
  • 安全建议

  • 背景介绍

    OpenClaw 默认将 Gateway 绑定在本地回环地址 127.0.0.1(loopback 模式),这意味着只能通过本机浏览器访问 Control UI。

    本教程将指导你如何将 Gateway 配置为监听所有网络接口(0.0.0.0),从而允许通过服务器的公网 IP 地址远程访问 OpenClaw 的 Web 界面。


    适用场景

    • ✅ 远程服务器上部署 OpenClaw,需要在本地浏览器管理
    • ✅ 内网穿透或 VPN 环境下访问 OpenClaw
    • ✅ 将 OpenClaw 作为服务提供给局域网内其他用户使用
    • ✅ 测试环境快速搭建

    前置条件

    在开始配置前,请确保:

  • OpenClaw 已安装并运行

    openclaw –version

  • 确认 Gateway 当前状态

    openclaw gateway status

  • 获取服务器 IP 地址

    hostname -I
    # 或
    ip addr show

  • 确保防火墙放行目标端口(默认 18789)

    # 检查端口是否开放
    sudo ufw status
    # 或
    sudo iptables -L | grep 18789


  • 配置步骤

    步骤 1:备份当前配置

    重要! 修改前先备份:

    # 创建备份
    mkdir -p ~/.openclaw/backups
    cp ~/.openclaw/openclaw.json ~/.openclaw/backups/openclaw.json.$(date +%Y%m%d_%H%M%S)

    # 确认备份成功
    ls -la ~/.openclaw/backups/

    步骤 2:编辑配置文件

    # 使用你喜欢的编辑器打开配置文件
    nano ~/.openclaw/openclaw.json
    # 或
    vim ~/.openclaw/openclaw.json

    步骤 3:修改 Gateway 绑定地址

    找到 gateway 配置块,进行以下修改:

    修改前(仅本地访问):

    "gateway": {
    "port": 18789,
    "mode": "local",
    "bind": "loopback"
    }

    修改后(允许公网访问):

    "gateway": {
    "port": 18789,
    "mode": "local",
    "bind": "0.0.0.0",
    "controlUi": {
    "allowInsecureAuth": true,
    "allowedOrigins": ["*"],
    "dangerouslyDisableDeviceAuth": true
    }
    }

    配置项说明
    配置项说明值注意事项
    bind 监听地址 0.0.0.0 监听所有网络接口
    allowInsecureAuth 允许不安全认证 true HTTP 模式下必须开启
    allowedOrigins 允许的来源 ["*"] * 表示允许任意来源
    dangerouslyDisableDeviceAuth 禁用设备身份验证 true 解决 HTTP 限制问题

    步骤 4:保存并重启 Gateway

    保存配置文件后,重启 Gateway 使配置生效:

    # 重启 Gateway
    openclaw gateway restart

    # 等待几秒后检查状态
    sleep 3
    openclaw gateway status

    重启后,你应该能看到类似输出:

    Gateway: bind=lan (0.0.0.0), port=18789
    Dashboard: http://<你的IP>:18789/
    Probe note: bind=lan listens on 0.0.0.0 (all interfaces)

    步骤 5:配置防火墙(如需要)

    如果无法从外部访问,检查防火墙设置:

    # Ubuntu/Debian (UFW)
    sudo ufw allow 18789/tcp
    sudo ufw reload

    # CentOS/RHEL (Firewalld)
    sudo firewall-cmd –permanent –add-port=18789/tcp
    sudo firewall-cmd –reload

    # 使用 iptables
    sudo iptables -A INPUT -p tcp –dport 18789 -j ACCEPT
    sudo iptables-save

    # 检查云服务器安全组(AWS/阿里云/腾讯云等)
    # 需要在控制台添加 18789 端口的入站规则


    完整配置示例

    以下是完整的 ~/.openclaw/openclaw.json 配置示例:

    {
    "agents": {
    "defaults": {
    "model": {
    "primary": "volcengine-plan/kimi-k2.5"
    },
    "models": {
    "volcengine-plan/ark-code-latest": {},
    "volcengine-plan/kimi-k2.5": {}
    },
    "workspace": "/root/.openclaw/workspace"
    }
    },
    "gateway": {
    "port": 18789,
    "mode": "local",
    "bind": "0.0.0.0",
    "auth": {
    "mode": "token",
    "token": "your-secure-token-here"
    },
    "controlUi": {
    "allowInsecureAuth": true,
    "allowedOrigins": ["*"],
    "dangerouslyDisableDeviceAuth": true
    }
    },
    "tools": {
    "profile": "coding",
    "web": {
    "search": {
    "enabled": true,
    "provider": "duckduckgo"
    }
    }
    }
    }


    访问验证

    本地测试

    # 测试本地访问
    curl http://127.0.0.1:18789

    # 查看完整 Gateway 状态
    openclaw gateway status

    远程访问

    在浏览器中打开:

    http://<你的服务器IP>:18789

    例如:

    http://172.31.0.2:18789

    验证步骤

  • 页面应正常加载,显示登录界面
  • 输入 token 后应成功进入 Control UI
  • 检查浏览器控制台,确认没有跨域错误

  • 常见问题

    Q1: “origin not allowed” 错误

    现象: 登录时提示 origin not allowed

    原因: Control UI 的跨域来源限制

    解决: 在 gateway.controlUi 中添加 allowedOrigins: ["*"]

    "controlUi": {
    "allowedOrigins": ["*"]
    }


    Q2: “control ui requires device identity” 错误

    现象: 登录时提示 control ui requires device identity

    原因: HTTP 模式下浏览器禁用了 Web Crypto API,无法进行设备身份验证

    解决: 在 gateway.controlUi 中添加 dangerouslyDisableDeviceAuth: true

    "controlUi": {
    "dangerouslyDisableDeviceAuth": true
    }


    Q3: “unauthorized: too many failed authentication attempts” 错误

    现象: 登录时提示认证失败次数过多

    原因: 多次尝试失败后触发了速率限制

    解决:

  • 等待 5-10 分钟后重试
  • 清除浏览器缓存和 Cookie
  • 重启 Gateway: openclaw gateway restart

  • Q4: 无法从外部网络访问

    现象: 本地可以访问,但外部无法连接

    排查步骤:

    # 1. 确认 Gateway 监听地址
    openclaw gateway status | grep -i "listening\\|bind"

    # 2. 检查端口监听情况
    ss -tlnp | grep 18789

    # 3. 检查防火墙
    sudo ufw status
    sudo iptables -L -n | grep 18789

    # 4. 测试端口连通性(从另一台机器)
    telnet <服务器IP> 18789
    nc -vz <服务器IP> 18789

    常见原因:

    • Gateway 绑定在 127.0.0.1 而非 0.0.0.0
    • 防火墙阻止了 18789 端口
    • 云服务器安全组未放行端口

    Q5: 如何恢复到仅本地访问模式

    如果需要恢复默认的安全配置:

    "gateway": {
    "bind": "loopback",
    "controlUi": {
    "allowInsecureAuth": false,
    "allowedOrigins": [],
    "dangerouslyDisableDeviceAuth": false
    }
    }

    然后重启 Gateway:

    openclaw gateway restart


    安全建议

    ⚠️ 重要提示: 本教程的配置会降低安全性,仅建议在以下场景使用:

    • ✅ 受信任的局域网环境
    • ✅ 临时测试环境
    • ✅ 有防火墙保护的服务器
    • ✅ 已经配置反向代理和 HTTPS

    生产环境安全加固建议

  • 使用 HTTPS

    "controlUi": {
    "allowInsecureAuth": false
    }

    配合 Nginx 反向代理 + SSL 证书

  • 限制允许的来源

    "allowedOrigins": ["https://your-domain.com"]

  • 使用强 Token

    # 生成随机强密码
    openssl rand -base64 32

  • 防火墙限制

    # 仅允许特定 IP 访问
    sudo ufw allow from 192.168.1.0/24 to any port 18789


  • 相关资源

    • OpenClaw 官方文档
    • OpenClaw GitHub
    • OpenClaw FAQ
    • OpenClaw 故障排除

    作者与贡献

    本教程由 OpenClaw 社区整理编写。

    如有问题或建议,欢迎:

    • 提交 Issue 到 OpenClaw GitHub
    • 加入 OpenClaw Discord 社区

    最后更新时间: 2026年4月5日

    适用版本: OpenClaw 2026.4.2 及更高版本

    赞(0)
    未经允许不得转载:171主机测评 » VibeCoding - OpenClaw 公网访问配置指南 (自动化)
    分享到: 更多 (0)

    评论 抢沙发

    • 昵称 (必填)
    • 邮箱 (必填)
    • 网址