Openclaw的安全问题不容忽视但我们还离不开这个好用的助手于是我研究了如何在云服务器上安装部署一个Openclaw让它远离我们真实的设备只在安全沙盒中运行我选用的是阿里云ecs云服务器现在新用户可以免费领取300元的额度VScode我选用的是4核8GB操作系统选的是Ubuntu 22.04预装应用Docker。接下来进入正题第一步阿里云 ECS 预处理 (放行端口)OpenClaw 启动后会提供一个 Web 控制台Control UI默认运行在18789端口。需要先去阿里云放行这个端口。登录阿里云控制台进入你的 ECS 实例详情。点击左侧的安全组-配置规则。添加一条入方向规则协议选TCP端口填18789授权对象填0.0.0.0/0或者为了安全只填你当前电脑的公网 IP。第二步在服务器上部署 OpenClaw在VScode中使用SSH 登录到你的 Ubuntu 22.04 服务器按照以下步骤拉取并启动注意所有命令行都是在VScode终端输入1. 克隆官方仓库git clone https://github.com/openclaw/openclaw.git cd openclaw2. 运行官方初始化脚本OpenClaw 提供了一键脚本会自动拉取镜像、处理目录权限并运行配置向导./docker-setup.sh到这一步可能会报错,ERROR: failed to build: failed to solve: process /bin/sh -c curl -fsSL https://bun.sh/install | bash did not complete successfully: exit code: 1接下来按照以下步骤操作1. 编辑 Dockerfile在你的终端里确保当前在openclaw目录下输入以下命令打开文件nano Dockerfile①替换 Bun 的安装方式换国内源在文件前半部分找到这行Dockerfile文件中找到以下字段RUN curl -fsSL https://bun.sh/install | bash把它整行删掉或加#注释替换成RUN npm config set registry https://registry.npmmirror.com \ npm install -g bun \ mkdir -p ~/.bun/bin \ ln -s $(which bun) ~/.bun/bin/bun \ echo[install]\nregistry https://registry.npmmirror.com/ ~/.bunfig.toml​​​​​②修改 pnpm 安装参数解除锁定 限制并发防止死机在文件后半部分找到包含--frozen-lockfile的那一段注意它可能有多行重点看最后面RUN --mounttypecache,idopenclaw-pnpm-store,target/root/.local/share/pnpm/store,sharinglocked \ NODE_OPTIONS--max-old-space-size2048 pnpm install --frozen-lockfile把它替换成下面这样RUN --mounttypecache,idopenclaw-pnpm-store,target/root/.local/share/pnpm/store,sharinglocked \ NODE_OPTIONS--max-old-space-size2048 pnpm install --no-frozen-lockfile到这一步还需要在上面②这行代码上面插入一个字段强制 Git 走 HTTP/1.1 协议​​​​​​​RUN git config --global url.https://github.com/.insteadOf ssh://gitgithub.com/ \ git config --global url.https://github.com/.insteadOf gitssh://gitgithub.com/ \ git config --global http.version HTTP/1.1插入后那段代码看起来应该是这样的# (上面其他的代码...) COPY --fromext-deps /out/ ./extensions/ RUN git config --global url.https://github.com/.insteadOf ssh://gitgithub.com/ \ git config --global url.https://github.com/.insteadOf gitssh://gitgithub.com/ \ git config --global http.version HTTP/1.1 RUN --mounttypecache,idopenclaw-pnpm-store,target/root/.local/share/pnpm/store,sharinglocked \ NODE_OPTIONS--max-old-space-size4096 pnpm install --no-frozen-lockfile保存并重试按Ctrl O保存按Enter确认。按Ctrl X退出。再次运行安装脚本./docker-setup.sh这样就没什么问题了运行安装结束就会出现openclaw的配置界面按照自己的需求配置即可配置过程中可能会报错容器在无限重启Error response from daemon: cannot join network namespace of container: Container bdc6d58310f018d7f7227e5f7965faec33f331xxxx1 is restarting, wait until the container is running解决方法是在终端确保在~/openclaw目录下依次复制粘贴执行以下三条命令修改绑定地址为all(允许所有外部 IP 访问)echo OPENCLAW_GATEWAY_BINDlan .envBash 停止并移除处于无限重启状态的错误容器 docker compose downBash 重新在后台启动正确的容器 docker compose up -d使用以下代码确认docker容器是否在工作docker ps1.如果STATUS这一列写着类似Up 3 minutes说明程序运行得非常完美问题100%出在网络防火墙或者 Ubuntu 自身的ufw上。2.如果写着Restarting说明我们刚才修改的.env文件没有被正确加载容器还在无限重启。但一般情况下是没有问题了。有问题检查以上哪步出错再修改一下即可如果通过你会在VScode终端看到这个Current runtime bind: lanGateway token: ecd8b8ddb6d75xxxx这个时候打开你电脑的浏览器复制并访问下面这个专属链接注意把你的阿里云ECS公网IP替换成你真实的 IP 地址这就是你访问openclaw ui的网址http://阿里云ECS公网IP:18789/#tokenecd8b8ddb6d75xxxx但接下来OpenClaw 的安全机制发出了警报需要一个修复脚本直接在VScode终端运行即可python3 -c import json, os path os.path.expanduser(~/.openclaw/openclaw.json) with open(path, r) as f: data json.load(f) if gateway not in data: data[gateway] {} if controlUi not in data[gateway]: data[gateway][controlUi] {} data[gateway][controlUi][dangerouslyAllowHostHeaderOriginFallback] True with open(path, w) as f: json.dump(data, f, indent2) print(✅ 安全策略已成功放行) 看到输出✅ 安全策略已成功放行后直接执行重启命令docker restart openclaw-openclaw-gateway-1但是这个时候注意了浏览器和本地网络限制可能还让你无法打开openclaw ui这就需要一个简单操作了在你电脑自带的终端CMD、PowerShell 或 Terminal里粘贴并回车运行这行命令ssh -L 18789:localhost:18789 root你的阿里云公网IP​​​​​​​(运行后它会像平时一样要求你输入阿里云服务器的密码。登录成功后你就把这个黑框框挂在后台不要关掉它。)这个时候再打开这个网址就会出现openclaw 的ui了http://阿里云ECS公网IP:18789/#tokenecd8b8ddb6d75xxxx刚进来可能是无连接的需要在上面这个网关访问的网管令牌这里再输入一次你的tokenCurrent runtime bind: lanGateway token: ecd8b8ddb6d75xxxx点击连接就能连上但是还可能有问题pairing required此设备需要网关主机的配对批准。这个是安全机制在作用防止你的 Token 泄漏只需要按照以下方法第一步查看待批准的设备列表在终端输入以下命令注意我们要通过 docker 去执行docker exec -it openclaw-openclaw-gateway-1 openclaw devices list运行后你会看到一行列表里面会有一个处于pending待处理状态的请求并且前面有一串类似req_123456...或者几位数字的ID (requestId)。请把这个 ID 复制下来。第二步批准你的电脑浏览器拿到 ID 后运行下面这条命令批准它把requestId替换成你刚才复制的真实 ID注意不要带尖括号​​​​​​​​​​​​​​docker exec -it openclaw-openclaw-gateway-1 openclaw devices approve requestId只要终端提示Approved或者Success你再切回你的电脑浏览器看一眼——那个配对的拦截页面会自动消失到这里基本完成了只需要你去连接飞书之类的就可以使用了完结撒花