NUC 无显示器部署 OpenClaw:纯命令行远程管理

华硕 NUC 作为一款迷你主机,非常适合作为无显示器服务器运行 OpenClaw。本文将详细介绍如何在没有显示器的情况下,通过纯命令行远程管理 NUC,部署和运行 OpenClaw 本地 AI 服务。

一、准备工作

1.1 硬件准备

设备 数量 说明
华硕 NUC 1 主机
网线 1 有线网络连接
USB 键盘(可选) 1 初始配置使用
USB 启动盘 1 安装系统使用

1.2 软件准备

软件 版本 用途
Ubuntu Server 22.04 LTS 服务器操作系统
SSH 客户端 最新版本 远程连接
Docker 最新版本 容器运行时
Docker Compose 最新版本 容器编排
OpenClaw 最新版本 本地 AI 服务

二、系统安装(无显示器)

2.1 制作启动盘

# 下载 Ubuntu Server ISO
wget https://releases.ubuntu.com/22.04/ubuntu-22.04.3-live-server-amd64.iso

# 制作启动盘(Linux 环境)
sudo dd if=ubuntu-22.04.3-live-server-amd64.iso of=/dev/sdX bs=4M status=progress

# 制作启动盘(Windows 环境)
# 使用 Rufus 或 balenaEtcher

2.2 配置网络

# 创建 Netplan 配置文件
# 在安装过程中配置网络,或者安装后通过串口配置

# 编辑网络配置
sudo vim /etc/netplan/00-installer-config.yaml

网络配置示例:

network:
  ethernets:
    enp3s0:
      dhcp4: no
      addresses:
        - 192.168.1.100/24
      gateway4: 192.168.1.1
      nameservers:
        addresses: [8.8.8.8, 8.8.4.4]
  version: 2

应用网络配置:

sudo netplan apply

2.3 启用 SSH

# 安装 SSH 服务器
sudo apt update
sudo apt install -y openssh-server

# 启动 SSH 服务
sudo systemctl start ssh
sudo systemctl enable ssh

# 配置 SSH
sudo vim /etc/ssh/sshd_config

SSH 配置示例:

# 端口
Port 22

# 允许 root 登录(可选)
PermitRootLogin yes

# 密码认证
PasswordAuthentication yes

# 公钥认证
PubkeyAuthentication yes

# 禁用空密码
PermitEmptyPasswords no

重启 SSH 服务:

sudo systemctl restart ssh

三、远程连接

3.1 SSH 连接

# 从其他计算机连接 NUC
ssh username@192.168.1.100

# 首次连接会提示确认主机密钥
# 输入 yes 继续

# 输入密码登录

3.2 配置 SSH 密钥认证

# 在客户端生成 SSH 密钥对
ssh-keygen -t rsa -b 4096 -C "your_email@example.com"

# 将公钥复制到 NUC
ssh-copy-id username@192.168.1.100

# 测试密钥认证
ssh username@192.168.1.100

3.3 配置 SSH 别名

# 编辑 SSH 配置文件
vim ~/.ssh/config

SSH 配置文件示例:

Host nuc
    HostName 192.168.1.100
    User username
    Port 22
    IdentityFile ~/.ssh/id_rsa

使用别名连接:

ssh nuc

四、系统配置

4.1 更新系统

# 更新软件包列表
sudo apt update

# 升级系统
sudo apt upgrade -y

# 清理不需要的软件包
sudo apt autoremove -y

4.2 安装必要工具

# 安装常用工具
sudo apt install -y vim git curl wget htop net-tools tmux unzip

# 安装系统监控工具
sudo apt install -y sysstat iotop nethogs

# 安装网络工具
sudo apt install -y iputils-ping traceroute dnsutils

4.3 配置时区

# 查看当前时区
timedatectl

# 设置时区为上海
sudo timedatectl set-timezone Asia/Shanghai

# 启用 NTP 同步
sudo timedatectl set-ntp true

4.4 配置主机名

# 设置主机名
sudo hostnamectl set-hostname nuc-server

# 编辑 hosts 文件
sudo vim /etc/hosts

hosts 文件示例:

127.0.0.1       localhost
127.0.1.1       nuc-server
192.168.1.100   nuc-server

五、Docker 环境部署

5.1 安装 Docker

# 安装 Docker
curl -fsSL https://get.docker.com -o get-docker.sh
sudo sh get-docker.sh

# 启动 Docker 服务
sudo systemctl start docker
sudo systemctl enable docker

# 将当前用户添加到 docker 组
sudo usermod -aG docker $USER

# 重新登录以使组权限生效

5.2 安装 Docker Compose

# 下载 Docker Compose
sudo curl -L "https://github.com/docker/compose/releases/latest/download/docker-compose-$(uname -s)-$(uname -m)" -o /usr/local/bin/docker-compose

# 添加执行权限
sudo chmod +x /usr/local/bin/docker-compose

# 验证安装
docker-compose --version

5.3 配置 Docker

# 创建 Docker 配置目录
sudo mkdir -p /etc/docker

# 编辑 Docker 配置文件
sudo vim /etc/docker/daemon.json

Docker 配置文件:

{
  "registry-mirrors": [
    "https://docker.mirrors.ustc.edu.cn",
    "https://hub-mirror.c.163.com"
  ],
  "log-driver": "json-file",
  "log-opts": {
    "max-size": "10m",
    "max-file": "3"
  },
  "live-restore": true
}

重启 Docker:

sudo systemctl daemon-reload
sudo systemctl restart docker

六、OpenClaw 部署

6.1 创建工作目录

# 创建工作目录
mkdir -p /opt/openclaw
cd /opt/openclaw

# 克隆 OpenClaw 仓库
git clone https://github.com/openclaw/openclaw.git

# 进入项目目录
cd openclaw

# 复制配置文件
cp .env.example .env

# 编辑配置文件
vim .env

OpenClaw 配置示例:

# OpenClaw 配置
OPENCLAW_PORT=8080
OPENCLAW_HOST=0.0.0.0

# 数据库配置
DB_HOST=postgres
DB_PORT=5432
DB_NAME=openclaw
DB_USER=openclaw
DB_PASSWORD=openclaw123

# Redis 配置
REDIS_HOST=redis
REDIS_PORT=6379
REDIS_PASSWORD=

# 数据存储
DATA_PATH=/data/openclaw

# AI 模型配置
AI_MODEL_PATH=/data/openclaw/models
AI_MODEL_TYPE=llama

# 日志配置
LOG_LEVEL=info
LOG_PATH=/var/log/openclaw

6.2 创建 docker-compose.yml

vim docker-compose.yml

docker-compose.yml 内容:

version: '3'

services:
  openclaw:
    image: openclaw/openclaw:latest
    container_name: openclaw
    restart: always
    ports:
      - "8080:8080"
    volumes:
      - /data/openclaw:/data
      - ./.env:/app/.env
    environment:
      - DB_HOST=postgres
      - REDIS_HOST=redis
    depends_on:
      - postgres
      - redis
    networks:
      - openclaw-net

  postgres:
    image: postgres:14
    container_name: openclaw-postgres
    restart: always
    volumes:
      - /data/openclaw/postgres:/var/lib/postgresql/data
    environment:
      - POSTGRES_DB=openclaw
      - POSTGRES_USER=openclaw
      - POSTGRES_PASSWORD=openclaw123
    networks:
      - openclaw-net

  redis:
    image: redis:7-alpine
    container_name: openclaw-redis
    restart: always
    volumes:
      - /data/openclaw/redis:/data
    networks:
      - openclaw-net

networks:
  openclaw-net:
    driver: bridge

6.3 启动 OpenClaw

# 创建数据目录
sudo mkdir -p /data/openclaw

# 启动所有服务
docker-compose up -d

# 查看服务状态
docker-compose ps

# 查看日志
docker-compose logs -f

6.4 验证 OpenClaw

# 测试 API
curl http://localhost:8080/api/health

# 测试版本信息
curl http://localhost:8080/api/version

# 在浏览器中访问
# http://192.168.1.100:8080

七、远程管理

7.1 使用 tmux 进行会话管理

# 安装 tmux
sudo apt install -y tmux

# 创建新会话
tmux new -s openclaw

# 分离会话(Ctrl+b, d)

# 重新连接会话
tmux attach -t openclaw

# 列出所有会话
tmux ls

7.2 使用 screen 进行会话管理

# 安装 screen
sudo apt install -y screen

# 创建新会话
screen -S openclaw

# 分离会话(Ctrl+a, d)

# 重新连接会话
screen -r openclaw

# 列出所有会话
screen -ls

7.3 使用 htop 监控系统

# 安装 htop
sudo apt install -y htop

# 启动 htop
htop

# 快捷键
# F1: 帮助
# F2: 设置
# F3: 搜索
# F4: 过滤
# F5: 树形视图
# F6: 排序
# F9: 杀死进程
# F10: 退出

八、NUC 无显示器远程管理流程

为了确保 NUC 在无显示器环境下稳定运行 OpenClaw,我们需要建立一套完整的远程管理流程。以下是远程管理的流程图:

正常

异常

运行中

未运行

运行中

未运行

运行中

未运行

正常

异常

正常

异常

200

非200

正常

CPU 高

内存高

磁盘高

开始远程管理

检查网络连接

检查 SSH 服务

检查网络配置

网络配置正确?

重启网络服务

修改网络配置

网络恢复?

检查物理连接

物理连接正常?

检查路由器

更换网线

检查 Docker 服务

启动 SSH 服务

启动成功?

检查 SSH 配置

重启 SSH 服务

检查 OpenClaw 服务

启动 Docker 服务

启动成功?

检查 Docker 配置

重启 Docker 服务

检查数据库连接

启动 OpenClaw

启动成功?

检查 OpenClaw 配置

重启 OpenClaw

检查 Redis 连接

重启数据库

重启成功?

检查数据库配置

修复数据库配置

检查 API 响应

重启 Redis

重启成功?

检查 Redis 配置

修复 Redis 配置

检查系统资源

重启 OpenClaw

重启成功?

管理完成

优化 CPU

优化内存

清理磁盘

优化成功?

限制资源

优化成功?

重启服务

清理成功?

扩容存储

记录管理日志

生成状态报告

结束

8.1 创建远程管理脚本

#!/bin/bash

LOG_FILE="/var/log/nuc-remote-management.log"
ALERT_EMAIL="admin@example.com"

log() {
    echo "[$(date '+%Y-%m-%d %H:%M:%S')] $1" >> $LOG_FILE
}

send_alert() {
    local subject=$1
    local message=$2
    echo "$message" | mail -s "$subject" $ALERT_EMAIL
}

check_network() {
    log "Checking network connection..."
    ping -c 4 8.8.8.8 > /dev/null 2>&1
    if [ $? -eq 0 ]; then
        log "Network connection is normal"
        return 0
    else
        log "Network connection failed"
        return 1
    fi
}

check_ssh() {
    log "Checking SSH service..."
    if systemctl is-active --quiet ssh; then
        log "SSH service is running"
        return 0
    else
        log "SSH service is not running"
        return 1
    fi
}

check_docker() {
    log "Checking Docker service..."
    if systemctl is-active --quiet docker; then
        log "Docker service is running"
        return 0
    else
        log "Docker service is not running"
        return 1
    fi
}

check_openclaw() {
    log "Checking OpenClaw service..."
    cd /opt/openclaw/openclaw
    if docker-compose ps | grep -q "Up"; then
        log "OpenClaw service is running"
        return 0
    else
        log "OpenClaw service is not running"
        return 1
    fi
}

check_database() {
    log "Checking database connection..."
    if docker exec openclaw-postgres pg_isready -U openclaw; then
        log "Database connection is normal"
        return 0
    else
        log "Database connection failed"
        return 1
    fi
}

check_redis() {
    log "Checking Redis connection..."
    if docker exec openclaw-redis redis-cli ping | grep -q PONG; then
        log "Redis connection is normal"
        return 0
    else
        log "Redis connection failed"
        return 1
    fi
}

check_api() {
    log "Checking OpenClaw API..."
    local response=$(curl -s -o /dev/null -w "%{http_code}" http://localhost:8080/api/health)
    if [ "$response" = "200" ]; then
        log "API is responding normally"
        return 0
    else
        log "API is not responding (HTTP $response)"
        return 1
    fi
}

check_system_resources() {
    log "Checking system resources..."
    
    # 检查 CPU 使用率
    local cpu_usage=$(top -bn1 | grep "Cpu(s)" | awk '{print $2}' | cut -d'%' -f1)
    log "CPU Usage: ${cpu_usage}%"
    
    if (( $(echo "$cpu_usage > 80" | bc -l) )); then
        log "High CPU usage detected: ${cpu_usage}%"
        send_alert "High CPU Usage" "CPU usage is ${cpu_usage}% on NUC"
    fi
    
    # 检查内存使用率
    local mem_usage=$(free | grep Mem | awk '{printf("%.0f"), $3/$2 * 100.0}')
    log "Memory Usage: ${mem_usage}%"
    
    if [ $mem_usage -gt 80 ]; then
        log "High memory usage detected: ${mem_usage}%"
        sync && echo 3 > /proc/sys/vm/drop_caches
        send_alert "High Memory Usage" "Memory usage is ${mem_usage}% on NUC"
    fi
    
    # 检查磁盘使用率
    local disk_usage=$(df -h /data | tail -1 | awk '{print $5}' | cut -d'%' -f1)
    log "Disk Usage: ${disk_usage}%"
    
    if [ $disk_usage -gt 80 ]; then
        log "High disk usage detected: ${disk_usage}%"
        docker system prune -f
        send_alert "High Disk Usage" "Disk usage is ${disk_usage}% on NUC"
    fi
}

restart_ssh() {
    log "Restarting SSH service..."
    sudo systemctl restart ssh
    if systemctl is-active --quiet ssh; then
        log "SSH service restarted successfully"
        return 0
    else
        log "Failed to restart SSH service"
        return 1
    fi
}

restart_docker() {
    log "Restarting Docker service..."
    sudo systemctl restart docker
    if systemctl is-active --quiet docker; then
        log "Docker service restarted successfully"
        return 0
    else
        log "Failed to restart Docker service"
        return 1
    fi
}

restart_openclaw() {
    log "Restarting OpenClaw service..."
    cd /opt/openclaw/openclaw
    docker-compose restart
    sleep 30
    if docker-compose ps | grep -q "Up"; then
        log "OpenClaw service restarted successfully"
        return 0
    else
        log "Failed to restart OpenClaw service"
        return 1
    fi
}

restart_database() {
    log "Restarting database..."
    docker restart openclaw-postgres
    sleep 10
    if docker exec openclaw-postgres pg_isready -U openclaw; then
        log "Database restarted successfully"
        return 0
    else
        log "Failed to restart database"
        return 1
    fi
}

restart_redis() {
    log "Restarting Redis..."
    docker restart openclaw-redis
    sleep 5
    if docker exec openclaw-redis redis-cli ping | grep -q PONG; then
        log "Redis restarted successfully"
        return 0
    else
        log "Failed to restart Redis"
        return 1
    fi
}

main() {
    log "=== NUC Remote Management Started ==="
    
    check_network
    if [ $? -ne 0 ]; then
        log "Network check failed, attempting to fix..."
        sudo systemctl restart networking
        sleep 10
        check_network
    fi
    
    check_ssh
    if [ $? -ne 0 ]; then
        log "SSH check failed, restarting..."
        restart_ssh
    fi
    
    check_docker
    if [ $? -ne 0 ]; then
        log "Docker check failed, restarting..."
        restart_docker
    fi
    
    check_openclaw
    if [ $? -ne 0 ]; then
        log "OpenClaw check failed, restarting..."
        restart_openclaw
    fi
    
    check_database
    if [ $? -ne 0 ]; then
        log "Database check failed, restarting..."
        restart_database
    fi
    
    check_redis
    if [ $? -ne 0 ]; then
        log "Redis check failed, restarting..."
        restart_redis
    fi
    
    check_api
    if [ $? -ne 0 ]; then
        log "API check failed, restarting OpenClaw..."
        restart_openclaw
    fi
    
    check_system_resources
    
    log "=== NUC Remote Management Completed ==="
}

main

添加执行权限:

sudo chmod +x /usr/local/bin/nuc-remote-management.sh

配置定时任务:

crontab -e

# 添加每 10 分钟执行一次管理
*/10 * * * * /usr/local/bin/nuc-remote-management.sh

九、故障排查

9.1 网络连接问题

# 检查网络接口
ip addr show

# 检查网络连接
ping 8.8.8.8

# 检查 DNS 解析
nslookup google.com

# 重启网络服务
sudo systemctl restart networking

# 检查路由表
ip route show

9.2 SSH 连接问题

# 检查 SSH 服务状态
sudo systemctl status ssh

# 查看 SSH 日志
sudo journalctl -u ssh -n 50

# 重启 SSH 服务
sudo systemctl restart ssh

# 检查 SSH 配置
sudo sshd -t

# 检查防火墙
sudo ufw status

9.3 Docker 服务问题

# 检查 Docker 服务状态
sudo systemctl status docker

# 查看 Docker 日志
sudo journalctl -u docker -n 50

# 重启 Docker 服务
sudo systemctl restart docker

# 检查 Docker 版本
docker --version

# 测试 Docker
docker run hello-world

9.4 OpenClaw 服务问题

# 检查 OpenClaw 服务状态
cd /opt/openclaw/openclaw
docker-compose ps

# 查看 OpenClaw 日志
docker-compose logs -f

# 重启 OpenClaw 服务
docker-compose restart

# 重新构建 OpenClaw
docker-compose up -d --build

# 检查容器资源使用
docker stats

十、总结

通过以上步骤,我们成功实现了 NUC 无显示器部署 OpenClaw,并通过纯命令行远程管理。

10.1 主要优势

  1. 成本低廉:无需购买显示器
  2. 节省空间:NUC 体积小,易于放置
  3. 易于管理:通过 SSH 远程管理
  4. 稳定可靠:Linux 系统稳定
  5. 节能环保:功耗低,适合长期运行

10.2 注意事项

  1. 定期检查网络连接
  2. 定期备份重要数据
  3. 定期更新系统和软件
  4. 定期监控系统资源
  5. 定期清理日志和缓存

10.3 后续扩展

可以进一步扩展功能:

  • 添加 Web 管理界面
  • 添加监控告警系统
  • 添加自动化运维脚本
  • 添加内网穿透服务
  • 添加更多 AI 模型

希望这篇教程能帮助大家成功实现 NUC 无显示器部署 OpenClaw,并通过纯命令行远程管理!

在这里插入图片描述

Logo

有“AI”的1024 = 2048,欢迎大家加入2048 AI社区

更多推荐