OpenClaw 入门指南:部署、原理与命令速查

OpenClaw 是一个强大的 AI 助手框架,它不仅能接管你的电脑,还能通过聊天软件远程操控。本文整合了三篇优质文章的内容,带你从零开始部署 OpenClaw,了解其核心原理,并掌握常用命令。

一、OpenClaw 是什么?

OpenClaw 的核心优势:

  1. 通用 Agent 能力 – 可以像 Code Agent 一样操作电脑
  2. 多渠道接入 – 支持 Telegram、Discord、WhatsApp 等聊天软件远程控制
  3. 丰富的工具生态 – 内置 40+ 技能,覆盖文件操作、笔记管理、智能家居等场景
  4. 跨会话记忆 – 支持长期记忆,上下文管理更智能

二、腾讯云部署 OpenClaw

2.1 服务器准备

推荐购买腾讯云轻量应用服务器,配置建议:

  • 操作系统:Ubuntu 22.04
  • 地域:选择东京(加速 GitHub 访问)
  • 镜像:选择预装 Docker 的镜像

2.2 安装 Node.js

使用 NVM 安装 Node.js:

curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.7/install.sh | bash
source ~/.bashrc
nvm install --lts

2.3 安装 OpenClaw

npm install -g openclaw-cn@latest

2.4 配置模型

运行配置向导:

openclaw-cn onboard --install-daemon

以 DeepSeek 为例,需要:

  1. 访问 DeepSeek 开放平台注册账号
  2. 创建 API Key
  3. 充值(建议至少 10 元)
  4. 在配置向导中输入 API Key

2.5 配置 Telegram 机器人

  1. 在 Telegram 中 @BotFather
  2. 发送 /newbot 创建机器人
  3. 按提示设置名称和用户名
  4. 获取 Token 并配置到 OpenClaw

重要配置:修改 groupPolicy 为 “open” 让所有群成员都能使用机器人。

三、OpenClaw 核心原理

3.1 架构概览

OpenClaw 的核心组件:

  • Session – 会话管理,不同聊天窗口上下文隔离
  • Workspace – 工作区文件(SOUL.md、USER.md 等)影响 Agent 行为
  • Pi Agent – 核心 Agent 运行时,提供基础工具

3.2 上下文管理

Pi Agent 的上下文组成:

  • Messages:用户输入、模型输出、工具调用结果
  • Tool Definition:适配各模型的函数调用

OpenClaw 的压缩策略:

  1. 上下文溢出自动压缩并重试
  2. 到达阈值触发压缩
  3. 压缩前写入 memory 文件

3.3 工具与技能

内置工具分类:

  • 文件操作:read, write, edit, exec
  • 会话管理:sessions_list, sessions_spawn
  • Web 自动化:web_search, browser
  • 记忆系统:memory_search, memory_get

热门技能:

  • 效率工具:tmux, coding-agent, healthcheck
  • 笔记管理:obsidian, notion, apple-notes
  • 本地生活:weather, food-order

四、常用命令速查

4.1 基础命令

openclaw --version # 查看版本
openclaw --help # 查看帮助
openclaw onboard # 首次配置

4.2 Gateway 管理

openclaw gateway start # 启动服务
openclaw gateway stop # 停止服务
openclaw gateway restart # 重启服务
openclaw gateway status # 查看状态
openclaw gateway install # 安装为系统服务(开机自启)

4.3 配置管理

openclaw config list # 查看所有配置
openclaw config get models # 查看模型配置
openclaw config set # 设置配置项

4.4 模型管理

openclaw models list # 列出已配置模型
openclaw models status # 查看模型状态
openclaw models probe # 探测模型可用性

4.5 频道管理

openclaw channels list # 列出频道
openclaw channels status # 查看状态
openclaw channels login # 登录频道

4.6 技能管理

openclaw skills list # 列出技能
openclaw skills install # 安装技能
openclaw skills update # 更新技能

4.7 故障排除

openclaw doctor # 健康检查
openclaw logs -f # 实时日志
openclaw logs --error # 错误日志

五、最佳实践

5.1 安全建议

# 使用环境变量存储敏感信息
export OPENAI_API_KEY="sk-xxx"
export TELEGRAM_BOT_TOKEN="xxx"
# 限制配置文件权限
chmod 600 ~/.openclaw/config.json

5.2 快捷别名

alias oc='openclaw'
alias ocg='openclaw gateway'
alias ocm='openclaw message send'

5.3 完整启动流程

openclaw config get all # 检查配置
openclaw models probe # 探测模型
openclaw gateway install # 安装服务
openclaw gateway start # 启动服务
openclaw logs -f # 查看日志

参考资料

node-gyp xcode-select: error: tool ‘xcodebuild’ requires Xcode, but active developer directory ‘/Library/Developer/CommandLineTools’ is a command line tools instance

这个错误可能是由两个原因引起的。

 

xcode

重装 xcode

$ sudo rm -rf /Library/Developer/CommandLineTools
$ xcode-select --install

参见 https://github.com/nodejs/node-gyp/issues/569

node-gyp

重装 node-gyp。例如直接用 nvm 装一个新版的 node 即可。

CTRL Z 与 Linux jobs

平常测试的时候,经常要开多个终端感觉有点不方便,特别是一些不支持自定义的远程 webssh。最近用 EMACS 的时候,接触到了 ctrl+z 这个东西,随手记录一下。

通常的应用程序可以通过 ctrl+z 切换到后台,例如很常见的一个场景,启动一个 http server,然后用 curl 命令(或者 ab 之类的命令)去请求它测试一下。这个时候就可以使用 ctrl+z 切换后台来节省开多个终端的时间:

$ node index
# ctrl+z
^Z
[1]  + 26762 suspended  node index

启动之后直接 ctrl+z 就可以切换到后台。然后可以通过 jobs 来查看后台程序

$ jobs
[1]  + suspended  node index

切换到后台之后,之后就可以做一些别的操作。等操作结束后,可以重新将其从后台转到前台来:

$ fg
[1]  + 26762 continued  node index
# ... 应用日志

这样就可以勉强不用开多个终端了。直接结束前台的程序可以使用 ctrl+c。

这里有一个简单的前台程序和后台程序的概念。通常的后台程序启动方式是:

node index &

多个后台任务可以通过 %n 来选择恢复某个到前台。例如:

$ fg %1

就可以恢复 jobs 显示的编号为 1 的任务。恢复到前台之后可以通过 ctrl+c 结束任务。也可以通过 kill 来直接杀死后台任务:

$ kill %1

DNS 查询之 10 个 dig 详细例子

本文索引:

  1. 简单的 dig 用法 (理解输出)
  2. 仅列出结果段
  3. 查询 MX 记录
  4. 查询 NS 记录
  5. 查看所有 DNS 记录
  6. 使用 +short 查看精简输出
  7. 使用 -x 进行 DNS 反向查询
  8. @dnsserver 指定解析域名的 NDS 服务器
  9. 批量 DNS 查询
  10. 使用 $HOME/.digrc 文件来设置默认项

*nix 上的 dig 命令是专门用来挖取域名信息的。本文详细列举了 10 个例子来解析 dig 命令。

1. 简单的 dig 用法 (理解输出)

当你给 dig 命令传一个域名时,默认情况下它会返回该域名的 A 记录 (查询到的站点的 ip 地址),如下例所示。

在本例中,结果段( ANSWER SECTION )中 显示了 redhat.com 的 A 记录。

$ dig redhat.com

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62863
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 3

;; QUESTION SECTION:
;redhat.com.                    IN      A

;; ANSWER SECTION:
redhat.com.             37      IN      A       209.132.183.81

;; AUTHORITY SECTION:
redhat.com.             73      IN      NS      ns4.redhat.com.
redhat.com.             73      IN      NS      ns3.redhat.com.
redhat.com.             73      IN      NS      ns2.redhat.com.
redhat.com.             73      IN      NS      ns1.redhat.com.

;; ADDITIONAL SECTION:
ns1.redhat.com.         73      IN      A       209.132.186.218
ns2.redhat.com.         73      IN      A       209.132.183.2
ns3.redhat.com.         73      IN      A       209.132.176.100

;; Query time: 13 msec
;; SERVER: 209.144.50.138#53(209.144.50.138)
;; WHEN: Thu Jan 12 10:09:49 2012
;; MSG SIZE  rcvd: 164

dig 命令的输出由以下几个部分组成:

  • 头部(Header): 这里显示了 dig 命令的版本,以及使用的全局选项(+cmd),和一些附加的头信息。
  • 查询段(QUESTION SECTION):dig 命令查询的输入域名。例如我们运行 “dig redhat.com”,那么默认配置的 dig 命令就会去取 redhat.com 的 A 记录。而 “redhat.com. IN A” 就是在暗示我们取的是该域名的 A 记录。
  • 结果段(ANSWER SECTION):查询到的结果。“redhat.com. 37 IN A 209.132.183.81” 即 redhat.com 的 A 记录 ip 地址为 209.132.183.81。
  • 来源段(AUTHORITY SECTION):返回该结果段的授信 DNS 域名服务器。实际上这里就是 redhat.com 这个域名的 DNS 解析服务器。
  • 附加段(ADDITIONAL SECTION):这里列出了来源段中 DNS 服务器的地址。
  • 统计段(底部内容):这里列出一些 dig 命令的统计信息,包括查询花了多长时间等。

2. 仅列出结果段

大多数情况下,你只需要查看结果段(ANSWER SECTION)的内容。所以,我们可以关掉其他部分的显示内容。

  • +nocomments – 关闭注释行
  • +noauthority – 关闭来源段
  • +noadditional – 关闭附加段
  • +nostats – 关闭统计段
  • +noanswer – 关闭结果段 (Emmm, 我想你应该不会这样)

所以,下例的命令仅仅只显示了结果段的信息:

$ dig redhat.com +nocomments +noquestion +noauthority +noadditional +nostats

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com +nocomments +noquestion +noauthority +noadditional +nostats
;; global options: +cmd
redhat.com.             9       IN      A       209.132.183.81

不想带那么多关闭项?你可以使用 +noall 来关闭所有的内容(包括结果段),然后再加上 +answer 来开启结果段的输出。

$ dig redhat.com +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com +noall +answer
;; global options: +cmd
redhat.com.             60      IN      A       209.132.183.81

3. 查询 MX 记录

$ dig redhat.com MX +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com MX +noall +answer
;; global options: +cmd
redhat.com.             513     IN      MX      5 mx1.redhat.com.
redhat.com.             513     IN      MX      10 mx2.redhat.com.

你也可以使用 -t 参数指定一个类型来查询 MX 记录。例如

$ dig -t MX redhat.com +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t MX redhat.com +noall +answer
;; global options: +cmd
redhat.com.             489     IN      MX      10 mx2.redhat.com.
redhat.com.             489     IN      MX      5 mx1.redhat.com.

4. 查询 NS 记录

$ dig redhat.com NS +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com NS +noall +answer
;; global options: +cmd
redhat.com.             558     IN      NS      ns2.redhat.com.
redhat.com.             558     IN      NS      ns1.redhat.com.
redhat.com.             558     IN      NS      ns3.redhat.com.
redhat.com.             558     IN      NS      ns4.redhat.com.

也可以用 -t 指定 NS 类型。

$ dig -t NS redhat.com +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t NS redhat.com +noall +answer
;; global options: +cmd
redhat.com.             543     IN      NS      ns4.redhat.com.
redhat.com.             543     IN      NS      ns1.redhat.com.
redhat.com.             543     IN      NS      ns3.redhat.com.
redhat.com.             543     IN      NS      ns2.redhat.com.

也可以用 -t 指定 NS 类型。

5. 查看所有 DNS 记录

$ dig redhat.com ANY +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com ANY +noall +answer
;; global options: +cmd
redhat.com.             430     IN      MX      5 mx1.redhat.com.
redhat.com.             430     IN      MX      10 mx2.redhat.com.
redhat.com.             521     IN      NS      ns3.redhat.com.
redhat.com.             521     IN      NS      ns1.redhat.com.
redhat.com.             521     IN      NS      ns4.redhat.com.
redhat.com.             521     IN      NS      ns2.redhat.com.

或使用 -t

$ dig -t ANY redhat.com  +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -t ANY redhat.com +noall +answer
;; global options: +cmd
redhat.com.             367     IN      MX      10 mx2.redhat.com.
redhat.com.             367     IN      MX      5 mx1.redhat.com.
redhat.com.             458     IN      NS      ns4.redhat.com.
redhat.com.             458     IN      NS      ns1.redhat.com.
redhat.com.             458     IN      NS      ns2.redhat.com.
redhat.com.             458     IN      NS      ns3.redhat.com.

6. 使用 +short 查看精简输出

$ dig redhat.com +short
209.132.183.81

你也可以指定记录类型:

$ dig redhat.com ns +short
ns2.redhat.com.
ns3.redhat.com.
ns1.redhat.com.
ns4.redhat.com.

7. 使用 -x 进行 DNS 反向查询

$ dig -x 209.132.183.81

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> -x 209.132.183.81
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 62435
;; flags: qr rd ra; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 3

;; QUESTION SECTION:
;81.183.132.209.in-addr.arpa.   IN      PTR

;; ANSWER SECTION:
81.183.132.209.in-addr.arpa. 600 IN     PTR     www.redhat.com.

;; AUTHORITY SECTION:
183.132.209.in-addr.arpa. 248   IN      NS      ns2.redhat.com.
183.132.209.in-addr.arpa. 248   IN      NS      ns1.redhat.com.
183.132.209.in-addr.arpa. 248   IN      NS      ns3.redhat.com.
183.132.209.in-addr.arpa. 248   IN      NS      ns4.redhat.com.

;; ADDITIONAL SECTION:
ns1.redhat.com.         363     IN      A       209.132.186.218
ns2.redhat.com.         363     IN      A       209.132.183.2
ns3.redhat.com.         363     IN      A       209.132.176.100

;; Query time: 35 msec
;; SERVER: 209.144.50.138#53(209.144.50.138)
;; WHEN: Thu Jan 12 10:15:00 2012
;; MSG SIZE  rcvd: 193

简要版:

$ dig -x 209.132.183.81 +short
www.redhat.com.

8. @dnsserver 指定解析域名的 NDS 服务器

默认情况下 dig 会使用你本机上 /etc/resolv.conf 文件中定义的 DNS  服务器。

如果你想使用不同的 DNS 服务器来执行查询,可以通过 @dnsserver 来在命令行指定。

$ dig @ns1.redhat.com redhat.com

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> @ns1.redhat.com redhat.com
; (1 server found)
;; global options: +cmd
;; Got answer:
;; ->>HEADER<<- opcode: QUERY, status: NOERROR, id: 20963
;; flags: qr aa rd; QUERY: 1, ANSWER: 1, AUTHORITY: 4, ADDITIONAL: 4
;; WARNING: recursion requested but not available

;; QUESTION SECTION:
;redhat.com.                    IN      A

;; ANSWER SECTION:
redhat.com.             60      IN      A       209.132.183.81

;; AUTHORITY SECTION:
redhat.com.             600     IN      NS      ns1.redhat.com.
redhat.com.             600     IN      NS      ns4.redhat.com.
redhat.com.             600     IN      NS      ns3.redhat.com.
redhat.com.             600     IN      NS      ns2.redhat.com.

;; ADDITIONAL SECTION:
ns1.redhat.com.         600     IN      A       209.132.186.218
ns2.redhat.com.         600     IN      A       209.132.183.2
ns3.redhat.com.         600     IN      A       209.132.176.100
ns4.redhat.com.         600     IN      A       209.132.188.218

;; Query time: 160 msec
;; SERVER: 209.132.186.218#53(209.132.186.218)
;; WHEN: Thu Jan 12 10:22:11 2012
;; MSG SIZE  rcvd: 180

9. 批量 DNS 查询

你可以通过创建一个文件存储多行域名来告诉 dig 批量查询多个域名。

首先我们可以创建一个简单的 names.txt 文件,里面包含两个准备批量查询的域名。

$ vi names.txt
redhat.com
centos.org

接下来,我们可以通过 -f 标志来告诉 dig 读取该文件进行批量查询:

$ dig -f names.txt MX +noall +answer
redhat.com.             600     IN      MX      10 mx2.redhat.com.
redhat.com.             600     IN      MX      5 mx1.redhat.com.
centos.org.             3600    IN      MX      10 mail.centos.org.

当然你也可以通过命令行来进行该操作:

$ dig redhat.com mx +noall +answer centos.org ns +noall +answer

; <<>> DiG 9.7.3-RedHat-9.7.3-2.el6 <<>> redhat.com mx +noall +answer centos.org ns +noall +answer
;; global options: +cmd
redhat.com.             332     IN      MX      10 mx2.redhat.com.
redhat.com.             332     IN      MX      5 mx1.redhat.com.
centos.org.             3778    IN      NS      ns3.centos.org.
centos.org.             3778    IN      NS      ns4.centos.org.
centos.org.             3778    IN      NS      ns1.centos.org.

上例中同时查询了 redhat.com 的 MX 记录以及 centos.org 的 NS 记录。

10. 使用 $HOME/.digrc 文件来设置默认项

你可能不想每次执行 dig 命令都带上一串选线,甚至 +short 也会觉得麻烦。这个时候你可以将这些选项配置到 .digrc 文件中,之后每次 dig 调用时,默认就会带上这些选项。配置如下选项:

$ cat $HOME/.digrc
+noall +answer

那么在进行 dig 操作时你可以省略这些不写了:

$ dig redhat.com
redhat.com.             60      IN      A       209.132.183.81

$ dig redhat.com MX
redhat.com.             52      IN      MX      5 mx1.redhat.com.
redhat.com.             52      IN      MX      10 mx2.redhat.com.

原文地址:https://www.thegeekstuff.com/2012/02/dig-command-examples