OpenClaw 入门实战第 8 期 – 高级配置 性能优化与安全加固
yaml
models:
default: qwen3.5-plus # 默认模型
fast: qwen2.5-7b # 快速响应场景
code: qwen-coder-plus # 代码专用
reasoning: qwen3.5-plus # 复杂推理
bash
# 临时切换模型
openclaw model set qwen2.5-7b
# 查看当前模型
openclaw model get
# 在 Skill 中指定模型
{
“model”: “qwen-coder-plus”,
“task”: “…”
}
yaml
# config.yaml
concurrency:
max_sessions: 10 # 最大并发会话数
max_subagents: 5 # 最大并发 Subagent 数
timeout_seconds: 300 # 任务超时时间
yaml
# config.yaml
cache:
enabled: true
ttl_seconds: 3600 # 缓存有效期 1 小时
max_size_mb: 512 # 缓存最大大小
bash
openclaw cache stats
openclaw cache clear # 清空缓存
yaml
# memory 配置
memory:
vector_store: lancedb
index_type: IVF_PQ # 向量索引类型
num_partitions: 100 # 分区数(根据数据量调整)
pq_bits: 8 # 乘积量化的位数
bash
# 优化记忆索引
openclaw memory optimize
# 清理过期记忆
openclaw memory prune –older-than 30d
# 查看记忆库大小
openclaw memory stats
yaml
# config.yaml
logging:
level: info # debug/info/warn/error
file_max_size_mb: 100 # 单文件最大大小
file_max_count: 10 # 保留文件数量
rotation: daily # 轮转策略
bash
#!/bin/bash
# ~/.openclaw/workspace/scripts/cleanup-logs.sh
# 清理 7 天前的日志
find ~/.openclaw/logs -name “*.log” -mtime +7 -delete
# 压缩旧日志
find ~/.openclaw/logs -name “*.log” -mtime +3 -exec gzip {} \;
echo “日志清理完成:$(date)”
yaml
# config.yaml
security:
allow_origins: # 允许的访问来源
– http://localhost:18789
– https://your-domain.com
rate_limit:
requests_per_minute: 60 # 每分钟请求限制
burst: 10 # 突发请求数
yaml
security:
ip_whitelist:
– 192.168.1.0/24 # 内网
– 10.0.0.0/8 # 公司网络
– 你的固定 IP
bash
# 生成 API Key
openclaw auth generate-key
# 输出示例
API Key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
yaml
# config.yaml
auth:
enabled: true
api_keys:
– key: sk-xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
name: “生产环境 Key”
permissions: [“read”, “write”, “exec”]
– key: sk-yyyyyyyyyyyyyyyyyyyyyyyyyyyyyyyy
name: “只读 Key”
permissions: [“read”]
yaml
# config.yaml
security:
blocked_commands:
– “rm -rf /”
– “dd if=/dev/zero”
– “mkfs”
– “chmod 777”
require_confirmation:
– “rm -rf *”
– “git push –force”
– “docker rm -f”
yaml
security:
restricted_paths:
– /etc/passwd
– /etc/shadow
– ~/.ssh/*
– ~/.aws/credentials
– /*/secret*
bash
# 加密配置文件
openclaw encrypt config.yaml
# 解密查看
openclaw decrypt config.yaml.enc
yaml
# 使用环境变量存储敏感信息
auth:
api_keys:
– key: ${OPENCLAW_API_KEY}
name: “生产环境”
bash
# 设置环境变量
export OPENCLAW_API_KEY=”sk-xxxxxxxx”
yaml
# config.yaml
audit:
enabled: true
log_file: ~/.openclaw/logs/audit.log
events:
– file_read
– file_write
– command_exec
– auth_attempt
– config_change
bash
# 查看最近 10 条操作记录
tail -n 10 ~/.openclaw/logs/audit.log
# 搜索特定操作
grep “command_exec” ~/.openclaw/logs/audit.log
bash
# 创建新用户
openclaw user create zhangsan
# 设置密码
openclaw user passwd zhangsan
# 查看用户列表
openclaw user list
yaml
# users/zhangsan.yaml
user:
name: zhangsan
email: zhangsan@example.com
role: developer # admin/developer/user
quota:
daily_requests: 1000
max_file_size_mb: 50
max_sessions: 3
permissions:
– read
– write
# 不给 exec 权限
yaml
# config.yaml
workspaces:
default: /root/.openclaw/workspace
users:
zhangsan: /home/zhangsan/openclaw-workspace
lisi: /home/lisi/openclaw-workspace
bash
# 设置目录权限
chown -R zhangsan:zhangsan /home/zhangsan/openclaw-workspace
chmod 700 /home/zhangsan/openclaw-workspace
yaml
# config.yaml
skills:
shared_path: /opt/openclaw/shared-skills
user_path: ~/.openclaw/workspace/skills
load_order:
– shared # 先加载共享技能
– user # 再加载用户技能(可覆盖)
yaml
memory:
shared:
enabled: true
path: /opt/openclaw/shared-memory
access_level: read-only # 用户只读,管理员可写
yaml
# config.yaml
cluster:
enabled: true
node_id: node-1
nodes:
– id: node-1
host: 192.168.1.10
port: 18789
– id: node-2
host: 192.168.1.11
port: 18789
– id: node-3
host: 192.168.1.12
port: 18789
yaml
cluster:
session_stickiness: true # 同一用户会话固定到同一节点
health_check_interval: 30 # 健康检查间隔(秒)
bash
# 实时状态
openclaw status
# 资源使用
openclaw stats
# 会话列表
openclaw sessions list
yaml
# config.yaml
monitoring:
prometheus:
enabled: true
port: 18790
path: /metrics
openclaw_requests_total – 总请求数openclaw_session_active – 活跃会话数openclaw_memory_usage_bytes – 内存使用openclaw_response_time_seconds – 响应时间yaml
# config.yaml
alerts:
– name: “高 CPU 使用率”
condition: “cpu_usage > 80%”
duration: “5m”
notify:
– type: webhook
url: “https://your-webhook/alert”
– type: email
to: “admin@example.com”
– name: “磁盘空间不足”
condition: “disk_usage > 90%”
notify:
– type: feishu
chat_id: “ch_xxxxx”
bash
#!/bin/bash
# ~/.openclaw/workspace/scripts/backup.sh
DATE=$(date +%Y%m%d_%H%M%S)
BACKUP_DIR=”/root/.openclaw/backups”
# 备份配置文件
tar -czf $BACKUP_DIR/config-$DATE.tar.gz \
-C /root/.openclaw/workspace \
*.md *.yaml *.json
# 备份 Skills
tar -czf $BACKUP_DIR/skills-$DATE.tar.gz \
-C /root/.openclaw/workspace \
skills/
# 清理旧备份(保留 7 天)
find $BACKUP_DIR -name “*.tar.gz” -mtime +7 -delete
echo “备份完成:$DATE”
bash
# 查看可用备份
ls -la /root/.openclaw/backups/
# 恢复配置
tar -xzf /root/.openclaw/backups/config-20260417.tar.gz \
-C /root/.openclaw/workspace/
# 验证恢复
openclaw status
bash
# 查看日志
tail -f ~/.openclaw/logs/openclaw.log
# 检查端口占用
lsof -i :18789
# 检查配置语法
openclaw config validate
bash
# 检查子进程限制
ulimit -u
# 查看可用 agents
openclaw agents list
# 检查资源使用
openclaw stats
bash
# 优化索引
openclaw memory optimize
# 清理过期记忆
openclaw memory prune –older-than 30d
# 查看记忆库大小
openclaw memory stats
bash
# 临时启用 debug 模式
export OPENCLAW_LOG_LEVEL=debug
openclaw start
# 或在 config.yaml 中配置
logging:
level: debug
夜雨聆风