【第三篇】OpenClaw安装部署Ubuntu操作系统版 - Linux环境的最佳实践
摘要:Ubuntu是最受欢迎的Linux发行版之一,也是OpenClaw的原生支持平台。本文详细介绍在Ubuntu系统上安装部署OpenClaw的完整流程,包括系统准备、多种安装方式、服务化部署等内容,适合Linux用户和服务器部署场景。

前言:为什么选择Ubuntu?
Ubuntu在OpenClaw部署上的优势:
优势一:完美的原生支持
- OpenClaw官方主要开发和测试平台
- 所有功能都有完整支持
- 性能和稳定性最优
优势二:服务器环境友好
- 适合云服务器部署
- 支持Docker、Kubernetes等容器化部署
- 便于远程管理和监控
优势三:丰富的软件生态
- 包管理器apt简单易用
- 社区支持强大
- 文档资源丰富
适用场景:
- 云服务器部署
- 开发者环境
- 企业级生产环境
- 需要长时间稳定运行的服务
一、系统要求与准备
1.1 系统要求
最低配置:
- Ubuntu 18.04 LTS或更高版本(推荐20.04/22.04/24.04 LTS)
- 2核CPU
- 4GB内存(推荐8GB+)
- 20GB可用磁盘空间(推荐40GB+)
- 稳定的互联网连接
软件要求:
- Node.js 22或更高版本
- npm或pnpm包管理器
- curl或wget
- systemd(用于服务管理)
1.2 检查系统版本
bash
lsb_release -a
期望输出示例:
Distributor ID: Ubuntu
Description: Ubuntu 22.04.3 LTS
Release: 22.04
Codename: jammy
1.3 更新系统包
bash
sudo apt update && sudo apt upgrade -y
1.4 安装必要工具
bash
sudo apt install -y curl wget git build-essential
二、Node.js环境配置
2.1 安装Node.js 22
方法一:使用NodeSource官方源(推荐)
bash
# 添加NodeSource仓库
curl -fsSL https://deb.nodesource.com/setup_22.x | sudo -E bash -
# 安装Node.js
sudo apt-get install -y nodejs
# 验证安装
node --version
npm --version
期望输出:
v22.x.x
10.x.x
方法二:使用NVM(Node Version Manager)
bash
# 安装nvm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.0/install.sh | bash
# 重新加载shell配置
source ~/.bashrc
# 安装Node.js 22
nvm install 22
# 设置为默认版本
nvm use 22
nvm alias default 22
# 验证
node --version
方法三:使用snap(不推荐,版本可能不是最新)
bash
sudo snap install node --classic --channel=22
2.2 配置npm镜像(国内用户)
如果npm下载速度慢,可以配置淘宝镜像:
bash
npm config set registry https://registry.npmmirror.com
验证镜像配置:
bash
npm config get registry

三、OpenClaw安装
3.1 方法一:一键安装脚本(推荐)
bash
curl -fsSL https://openclaw.ai/install.sh | bash
安装过程:
- 检测系统环境
- 安装依赖(如果缺失)
- 下载并安装OpenClaw
- 配置初始设置
安装时间: 通常30秒到2分钟
3.2 方法二:npm全局安装
bash
npm install -g openclaw@latest
3.3 方法三:从源码编译安装(开发者)
bash
# 克隆仓库
git clone https://github.com/openclaw/openclaw.git
cd openclaw
# 安装依赖
npm install
# 构建
npm run build
# 链接到全局
npm link
3.4 验证安装
bash
# 查看版本
openclaw --version
# 运行诊断
openclaw doctor
四、初始化配置
4.1 运行初始化向导
bash
openclaw onboard --install-daemon
4.2 向导步骤详解
步骤1:风险确认
? I understand this is powerful and inherently risky. Continue? (Y/n)
输入 Y 继续。
步骤2:选择模式
? Onboarding mode:
❯ QuickStart - Minimal setup, get running fast
Manual - Full control over all settings
新手选择QuickStart。
步骤3:选择模型提供商
? Which model provider would you like to use?
❯ KIMI
MiniMax
GLM
OpenAI
Anthropic
国内用户推荐KIMI、MiniMax、GLM。
步骤4:配置鉴权
选择Coding Plan或API Key方式,输入你的API Key。
步骤5:选择模型
选择你订阅的模型版本。
步骤6:配置Channel
首次建议先Skip:
? Select channel (QuickStart):
❯ Skip for now
步骤7:配置Skills
选择Yes,安装基础技能。
步骤8:完成Bootstrap
建议完成首次对话,告诉AI你的基本信息。
4.3 验证安装
bash
# 检查状态
openclaw status
# 测试对话
echo "你好,请用一句话介绍你自己。" | openclaw chat

五、配置为系统服务
5.1 使用systemd管理服务
OpenClaw支持systemd,可以实现开机自启和自动重启。
安装服务:
bash
openclaw gateway install
这会创建systemd服务文件:/etc/systemd/system/openclaw-gateway.service
启动服务:
bash
sudo systemctl start openclaw-gateway
设置开机自启:
bash
sudo systemctl enable openclaw-gateway
查看服务状态:
bash
sudo systemctl status openclaw-gateway
期望输出:
● openclaw-gateway.service - OpenClaw Gateway
Loaded: loaded (/etc/systemd/system/openclaw-gateway.service; enabled; vendor preset: enabled)
Active: active (running) since Mon 2026-03-15 10:30:00 CST; 5s ago
5.2 服务管理命令
bash
# 启动
sudo systemctl start openclaw-gateway
# 停止
sudo systemctl stop openclaw-gateway
# 重启
sudo systemctl restart openclaw-gateway
# 查看状态
sudo systemctl status openclaw-gateway
# 查看日志
sudo journalctl -u openclaw-gateway -f
# 禁用开机自启
sudo systemctl disable openclaw-gateway
5.3 查看服务日志
bash
# 实时查看日志
sudo journalctl -u openclaw-gateway -f
# 查看最近100行
sudo journalctl -u openclaw-gateway -n 100
# 查看今天的日志
sudo journalctl -u openclaw-gateway --since today
5.4 配置服务环境变量
如果需要设置环境变量,可以编辑服务文件:
bash
sudo nano /etc/systemd/system/openclaw-gateway.service
在 [Service] 部分添加:
ini
[Service]
Environment="NODE_ENV=production"
Environment="OPENCLAW_WORKSPACE=/path/to/workspace"
保存后重载并重启:
bash
sudo systemctl daemon-reload
sudo systemctl restart openclaw-gateway
六、防火墙配置
6.1 开放必要端口
OpenClaw默认使用以下端口:
| 端口 | 用途 | 是否必需 |
|---|---|---|
| 18789 | Gateway主端口 | 是 |
| 8080 | Docker沙盒(如果启用) | 可选 |
使用ufw开放端口:
bash
# 启用防火墙
sudo ufw enable
# 允许SSH(非常重要!)
sudo ufw allow ssh
# 开放OpenClaw端口
sudo ufw allow 18789/tcp
# 如果使用Docker沙盒
sudo ufw allow 8080/tcp
# 查看防火墙状态
sudo ufw status
注意: 如果你在云服务器上部署,还需要在云服务商的控制台配置安全组规则,开放相应端口。
6.2 限制访问来源(推荐)
为了安全,建议限制访问来源IP:
bash
# 只允许特定IP访问
sudo ufw allow from 192.168.1.100 to any port 18789
# 允许特定网段
sudo ufw allow from 192.168.1.0/24 to any port 18789
七、反向代理配置(可选)
如果你希望通过域名访问OpenClaw,可以使用Nginx反向代理。
7.1 安装Nginx
bash
sudo apt install nginx -y
7.2 配置反向代理
创建配置文件:
bash
sudo nano /etc/nginx/sites-available/openclaw
添加以下内容:
nginx
server {
listen 80;
server_name your-domain.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;
}
}
7.3 启用配置
bash
# 创建软链接
sudo ln -s /etc/nginx/sites-available/openclaw /etc/nginx/sites-enabled/
# 测试配置
sudo nginx -t
# 重启Nginx
sudo systemctl restart nginx
7.4 配置SSL证书(推荐)
使用Let's Encrypt免费证书:
bash
# 安装certbot
sudo apt install certbot python3-certbot-nginx -y
# 自动配置SSL
sudo certbot --nginx -d your-domain.com
# 按提示输入邮箱并同意条款
Certbot会自动配置HTTPS并设置自动续期。
八、常见问题排查
8.1 问题:端口被占用
症状: 启动Gateway时提示端口被占用
排查:
bash
sudo lsof -i :18789
解决方案:
方法1:更改端口
bash
openclaw config set gateway.port 18790
方法2:结束占用进程
bash
# 查看PID
sudo lsof -i :18789
# 结束进程
sudo kill <PID>
8.2 问题:权限不足
症状: 提示权限错误
解决方案:
bash
# 添加用户到docker组(如果使用Docker沙盒)
sudo usermod -aG docker $USER
# 重新登录生效
8.3 问题:服务无法启动
症状: systemctl start 失败
排查步骤:
bash
# 查看详细日志
sudo journalctl -u openclaw-gateway -n 100
# 手动运行查看错误
openclaw gateway --verbose
8.4 问题:网络连接问题
症状: 无法访问外部API
排查:
bash
# 测试DNS解析
ping api.openai.com
# 测试HTTP连接
curl -I https://api.openai.com
# 如果DNS有问题,修改/etc/resolv.conf
sudo nano /etc/resolv.conf
# 添加DNS服务器
nameserver 8.8.8.8
nameserver 114.114.114.114
九、性能优化
9.1 启用缓存
bash
openclaw config set agents.defaults.cache.enabled true
openclaw config set agents.defaults.cache.ttl 3600
9.2 配置内存限制
在systemd服务文件中添加:
ini
[Service]
MemoryLimit=2G
MemoryMax=2G
9.3 配置日志轮转
创建日志轮转配置:
bash
sudo nano /etc/logrotate.d/openclaw
添加:
~/.openclaw/logs/*.log {
daily
rotate 7
compress
delaycompress
missingok
notifempty
create 0644 openclaw openclaw
}
十、安全加固
10.1 运行安全审计
bash
openclaw security audit
10.2 配置Web UI认证
bash
openclaw config set gateway.controlUi.auth.enabled true
openclaw config set gateway.controlUi.auth.username "your_username"
openclaw config set gateway.controlUi.auth.password "your_strong_password"
10.3 启用沙盒
bash
openclaw config set agents.defaults.sandbox.mode "non-main"
10.4 配置文件权限
bash
chmod 600 ~/.openclaw/openclaw.json
chmod 700 ~/.openclaw
十一、监控与维护
11.1 监控服务健康状态
创建监控脚本:
bash
#!/bin/bash
# check-openclaw.sh
if ! systemctl is-active --quiet openclaw-gateway; then
echo "OpenClaw Gateway is not running, restarting..."
systemctl restart openclaw-gateway
echo "OpenClaw Gateway restarted at $(date)" >> /var/log/openclaw-monitor.log
fi
添加到crontab,每5分钟检查一次:
bash
crontab -e
添加:
*/5 * * * * /home/username/scripts/check-openclaw.sh
11.2 定期备份
备份OpenClaw配置和数据:
bash
# 创建备份脚本
nano ~/backup-openclaw.sh
添加:
bash
#!/bin/bash
BACKUP_DIR="/home/backup/openclaw"
DATE=$(date +%Y%m%d_%H%M%S)
mkdir -p $BACKUP_DIR
# 备份配置和数据
tar -czf $BACKUP_DIR/openclaw_$DATE.tar.gz ~/.openclaw
# 删除30天前的备份
find $BACKUP_DIR -name "openclaw_*.tar.gz" -mtime +30 -delete
设置执行权限:
bash
chmod +x ~/backup-openclaw.sh
添加到crontab,每天凌晨2点备份:
0 2 * * * /home/username/backup-openclaw.sh

十二、总结与建议
推荐配置清单
✅ 基础配置:
- Ubuntu 18.04+ 已准备
- Node.js 22+ 已安装
- OpenClaw 已安装
- 初始化向导已完成
- API Key 已配置
✅ 服务化配置:
- systemd服务已安装
- 服务已设置开机自启
- 服务运行状态正常
✅ 网络配置:
- 防火墙已配置
- 必要端口已开放
- (可选)反向代理已配置
- (可选)SSL证书已配置
✅ 安全配置:
- 安全审计已通过
- Web UI认证已启用
- 沙盒已启用
- 文件权限已设置
服务器部署建议
- 使用LTS版本:Ubuntu 20.04/22.04 LTS
- 定期更新系统 :每月执行一次
apt update && apt upgrade - 配置监控告警:监控服务状态、磁盘空间、内存使用
- 定期备份:至少每天备份一次配置和数据
- 使用独立账户:不要使用root账户运行OpenClaw
下一步
恭喜你完成了OpenClaw在Ubuntu系统上的安装部署!
参考资料:
- OpenClaw官方文档: https://docs.openclaw.ai
- Ubuntu官方文档: https://ubuntu.com/server/docs
- systemd官方文档: https://systemd.io/