Ubuntu 22.04 安装 osquery 5.23.1 指南
Ubuntu 22.04 从零安装 osquery 5.23.1:官方 apt 仓库/手动 deb/tar.gz 三方案、升级降级、osquery.conf 与 pack 配置详解、osqueryctl 校验、systemd 托管与调度结果落盘验证,含排障实录
Ubuntu 22.04 安装 osquery 5.23.1 指南
目标:在 Ubuntu 22.04 服务器上从零安装 osquery 5.23.1,覆盖官方 apt 仓库在线安装、手动 .deb 包安装、官方 tar.gz 包部署共三种方案,并完成 osqueryd 的配置编写、
osqueryctl config-check校验、systemd 托管与调度结果落盘验证。全部命令与回显已在 Ubuntu 22.04.5 LTS(x86_64)实测通过。
一、环境信息
本章列出实测环境的完整基准信息,读者按此环境对照可最大程度复现文中全部回显。
- 操作系统:Ubuntu 22.04.5 LTS(Jammy Jellyfish),内核
6.8.0-138-generic(osqueryi实测kernel_info表回显,见 4.4 节) - 架构:x86_64(amd64)
- osquery 版本:5.23.1-1.linux(官方 deb 仓库当前 Latest 稳定版;5.24.0 于 2026-10-09 发布为 Pre-release,生产环境不建议使用)
- 软件源:
https://pkg.osquery.io/deb(suite 名为deb,组件main) - 安装产物大小:deb 包约 32MB(amd64)、tar.gz 包约 81MB(x86_64)
- 安装路径(deb/rpm 包布局):
- 可执行体:
/usr/bin/osqueryi、/usr/bin/osqueryd、/usr/bin/osqueryctl(均为指向/opt/osquery/bin/下真实二进制的符号链接) - 配置目录:
/etc/osquery/(deb 包安装后为空,需自行创建配置文件,见第八章) - 运行数据:
/var/osquery/(RocksDB 数据库、pidfile) - 日志目录:
/var/log/osquery/ - systemd 单元:
/usr/lib/systemd/system/osqueryd.service
- 可执行体:
- 验证方式:
osqueryi --version/osqueryd --version/dpkg -s osquery
二、前置条件
安装前确认以下三项,避免中途报错反复。
- 系统权限:具备 root 账户或
sudo免密能力。osquery 需要读取/proc与系统文件,osqueryd 以 root 常驻运行。 - 基础依赖(apt 仓库方案需要,手动 .deb/tar.gz 方案不依赖):
ca-certificates、curl、gnupg。官方源走 HTTPS,需系统时间正确且证书链完整:
apt-get update && apt-get install -y ca-certificates curl gnupg
- 网络可达性:
https://pkg.osquery.io(CloudFront 分发,国内网络通常可达;若不通,可改用 7.2 节的离线 .deb 包方式)。
验证基础依赖就绪:
dpkg -s ca-certificates curl gnupg 2>/dev/null | grep -E '^(Package|Status):'
实测回显(本机已装齐时每个包输出 Status: install ok installed,节选):
Status: install ok installed
Package: curl
Status: install ok installed
Package: gnupg
Status: install ok installed
三、版本选型说明
osquery 版本号遵循 X.Y.Z 语义(X 大版本 / Y 小版本 / Z 补丁版本,官方 README 说明)。选型依据(实查日期 2026-10-18):
| 版本 | 发布状态 | 发布时间 | 是否建议采用 |
|---|---|---|---|
| 5.24.0 | Pre-release(预发布) | 2026-10-09 | 否,测试/尝鲜用 |
| 5.23.1 | Latest(最新稳定) | 2026-06-24 | 是,本篇基准 |
| 5.23.0 / 5.22.1 / 5.21.0 | 历史稳定版 | 2026-06-24 及更早 | 降级回退用 |
关键差异解读:deb 官方仓库会保留近期多个历史版本(实测
apt-cache madison osquery可见 5.23.1/5.23.0/5.22.1/5.21.0),因此生产上默认装仓库 Candidate 版(5.23.1),出问题时可按 4.5 节降级。
Ubuntu 22.04 适配性:官方 deb 仓库的 deb/main 分支同时提供 amd64 与 arm64 两种架构的包,jammy(22.04)与 noble(24.04)均直接支持;查询体系无发行版差异,本篇全部命令同样适用于 Ubuntu 24.04。
四、方案 A:官方 apt 仓库在线安装
4.1 方案说明
官方维护的 apt 仓库(pkg.osquery.io/deb,suite 名 deb)是 Ubuntu/Debian 系的推荐安装方式:一次配置源后,apt-get install/apt-get upgrade 即可完成安装与后续升级,GPG 签名保证包完整性。本章完成「导入签名密钥 → 配置源 → 安装 → 验证 → 升级/降级」全流程。
4.2 配置官方软件源
第一步,下载官方 GPG 公钥并转换为 keyring 二进制格式(Debian 系新版 apt 要求 signed-by 引用 dearmor 后的 keyring):
curl -fsSL https://pkg.osquery.io/deb/pubkey.gpg | gpg --yes --dearmor -o /usr/share/keyrings/osquery-archive-keyring.gpg
验证 keyring 已生成(约 2.2KB 二进制文件):
ls -l /usr/share/keyrings/osquery-archive-keyring.gpg
实测回显:
-rw-r--r-- 1 root root 2208 Oct 18 08:09 /usr/share/keyrings/osquery-archive-keyring.gpg
第二步,写入源定义文件。注意 suite 名是 deb(不是发行版代号),这是 osquery 仓库的固定写法:
echo 'deb [arch=amd64 signed-by=/usr/share/keyrings/osquery-archive-keyring.gpg] https://pkg.osquery.io/deb deb main' > /etc/apt/sources.list.d/osquery.list
第三步,刷新索引:
apt-get update
实测回显(节选,osquery 源的两行是关键):
Get:3 https://pkg.osquerypackages.com/deb deb InRelease [102 kB]
Get:6 https://pkg.osquerypackages.com/deb deb/main amd64 Packages [27.1 kB]
Fetched 129 kB in 5s (26.0 kB/s)
Reading package lists...
⚠️ 排障 1:若
apt-get update报NO_PUBKEY 97A80C63C9D8B80B,说明 keyring 未正确 dearmor(直接把 ASCII 公钥当 keyring 用),按本节第一步重新 dearmor 即可;97A80C63C9D8B80B正是官方签名密钥指纹的尾段。 ⚠️ 排障 2:非交互环境下gpg --dearmor若报gpg: cannot open '/dev/tty': No such device or address,是因为目标文件已存在、gpg 试图交互确认覆盖——加--yes参数(如上命令)或先rm -f旧文件再执行。
4.3 安装 osquery
apt-get install -y osquery
实测回显(首尾节选,包体 33MB 下载耗时与网络相关):
Get:1 https://pkg.osquerypackages.com/deb deb/main amd64 osquery amd64 5.23.1-1.linux [33.0 MB]
Fetched 33.0 MB in 2min 21s (235 kB/s)
Selecting previously unselected package osquery.
(Reading database ... 5208 files and directories currently installed.)
Preparing to unpack .../osquery_5.23.1-1.linux_amd64.deb ...
Unpacking osquery (5.23.1-1.linux) ...
Setting up osquery (5.23.1-1.linux) ...
4.4 安装结果验证
三个工具版本逐一验证:
osqueryi --version
osqueryd --version
实测回显:
osqueryi version 5.23.1
osqueryd version 5.23.1
包元数据验证(确认安装来源与状态):
dpkg -s osquery | grep -E '^(Package|Version|Status|Architecture):'
实测回显:
Package: osquery
Status: install ok installed
Architecture: amd64
Version: 5.23.1-1.linux
用 osquery 自身查询内核与系统信息,确认运行时正常(这也是安装成功的最直观验证):
osqueryi --json "SELECT path, version FROM kernel_info;"
osqueryi --json "SELECT name, version, arch FROM os_version;"
实测回显:
[
{"path":"/boot/vmlinuz-6.8.0-138-generic","version":"6.8.0-138-generic"}
]
[
{"name":"Ubuntu","version":"22.04.5 LTS (Jammy Jellyfish)","arch":"x86_64"}
]
说明:deb 包安装后
/etc/osquery/与/var/log/osquery/均为空目录——官方不附带默认配置文件,osqueryd 启动需要自行编写配置,完整步骤见第八章;/usr/share/osquery/下仅有 certs/lenses 等辅助资源,不含 packs 示例。
4.5 升级与降级
查看已装版本、仓库候选版本与全部可用版本:
apt-cache policy osquery
apt-cache madison osquery
实测回显:
osquery:
Installed: 5.23.1-1.linux
Candidate: 5.23.1-1.linux
Version table:
*** 5.23.1-1.linux 500
500 https://pkg.osquery.io/deb deb/main amd64 Packages
100 /var/lib/dpkg/status
5.23.0-1.linux 500
osquery | 5.23.1-1.linux | https://pkg.osquery.io/deb deb/main amd64 Packages
osquery | 5.23.0-1.linux | https://pkg.osquery.io/deb deb/main amd64 Packages
osquery | 5.22.1-1.linux | https://pkg.osquery.io/deb deb/main amd64 Packages
osquery | 5.21.0-1.linux | https://pkg.osquery.io/deb deb/main amd64 Packages
日常升级(跟随仓库 Candidate):
apt-get update && apt-get install -y osquery
指定版本降级(示例:回退到 5.22.1):
apt-get install -y --allow-downgrades osquery=5.22.1-1.linux
osqueryi --version
实测回显(Unpacking ... over ... 说明是原地降级替换):
Preparing to unpack .../osquery_5.22.1-1.linux_amd64.deb ...
Unpacking osquery (5.22.1-1.linux) over (5.23.1-1.linux) ...
Setting up osquery (5.22.1-1.linux) ...
osqueryi version 5.22.1
再升级回最新稳定版:
apt-get install -y osquery
osqueryi --version
实测回显:
Unpacking osquery (5.23.1-1.linux) over (5.22.1-1.linux) ...
Setting up osquery (5.23.1-1.linux) ...
osqueryi version 5.23.1
⚠️ 注意:跨小版本降级时若 RocksDB 数据格式不兼容,osqueryd 首次启动可能迁移或重建
/var/osquery/osquery.db;降级前建议停服务并备份该目录。
五、方案 B:手动 .deb 包安装
5.1 方案说明
不配置 apt 源、直接下载官方 .deb 包用 dpkg -i 安装,适合:无法访问 pkg.osquery.io 但能访问下载站点的网络、需要把包先下载到内网再分发(离线安装)、或版本需要精确锁定的场景。缺点是升级需要手动重复下载。官方 .deb 包有两个获取渠道,内容完全一致(同一构建产物、同一 SHA256):
- 官方包站点:
https://pkg.osquery.io/deb/osquery_5.23.1-1.linux_amd64.deb - GitHub Releases 资产:
https://github.com/osquery/osquery/releases/download/5.23.1/osquery_5.23.1-1.linux_amd64.deb
💡 命名提示:5.x 的 deb 文件名 revision 段是
1.linux(带点),写作osquery_5.23.1-1_linux_amd64.deb(下划线)会 404——这是新装用户最常踩的下载坑。
5.2 下载 .deb 包与校验
curl -fsSL -o /tmp/osquery_5.23.1-1.linux_amd64.deb https://pkg.osquery.io/deb/osquery_5.23.1-1.linux_amd64.deb
ls -lh /tmp/osquery_5.23.1-1.linux_amd64.deb
sha256sum /tmp/osquery_5.23.1-1.linux_amd64.deb
实测回显:
-rw-r--r-- 1 root root 32M Oct 18 08:21 /tmp/osquery_5.23.1-1.linux_amd64.deb
1431a9a6394657eba3cdd3476a462a6fe9721fc3664a70f23efcf7e37816787a /tmp/osquery_5.23.1-1.linux_amd64.deb
校验值 1431a9a6... 与官方下载页 osquery.io/downloads 公布的 amd64 deb 包 SHA256 一致(官方下载页实查于 2026-10-18),比对无误后再安装。
5.3 dpkg 安装
dpkg -i /tmp/osquery_5.23.1-1.linux_amd64.deb
实测回显(deb 包无第三方依赖,dpkg -i 直接完成):
(Reading database ... 6141 files and directories currently installed.)
Preparing to unpack .../osquery_5.23.1-1.linux_amd64.deb ...
Unpacking osquery (5.23.1-1.linux) ...
Setting up osquery (5.23.1-1.linux) ...
⚠️ 若
dpkg -i中途报依赖缺失(极少发生),执行apt-get -f install修复后重试。
5.4 安装结果验证
osqueryi --version && osqueryd --version
ls -ld /etc/osquery /var/log/osquery
实测回显:
osqueryi version 5.23.1
osqueryd version 5.23.1
drwxr-xr-x 2 root root 4096 Jun 24 17:17 /etc/osquery
drwxr-xr-x 2 root root 4096 Jun 24 17:17 /var/log/osquery
与方案 A 产物完全一致(同样的 /opt/osquery/bin 布局与空配置目录),后续配置与 systemd 托管章节通用。
六、方案 C:官方 tar.gz 包部署
6.1 方案说明
官方同时发布免包管理的 tar.gz 产物(内含完整 FHS 目录树),解压即用,不写入 dpkg 数据库,适合:无包管理器的极简环境、多版本并存测试、或希望把 osquery 放在独立前缀下的场景。代价是升级/卸载全靠手工,且不自带 systemd 单元(可参照第八章 unit 内容自写)。
6.2 下载解压
curl -fsSL -o /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz https://pkg.osquery.io/linux/osquery-5.23.1_1.linux_x86_64.tar.gz
ls -lh /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz
sha256sum /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz
实测回显:
-rw-r--r-- 1 root root 81M Oct 18 08:28 /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz
0f37a478a1dbda24b67c81551e32d734b392c5a2f5deb156bf1c41ca204cfa67 /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz
0f37a478... 与官方下载页公布的 x86_64 tar.gz SHA256 一致。查看包内结构后解压到根目录:
tar -tzf /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz | head -8
实测回显:
usr/
usr/bin/
usr/bin/osqueryi
usr/bin/osqueryd
usr/bin/osqueryctl
var/
var/osquery/
var/log/
tar -xzf /tmp/osquery-5.23.1_1.linux_x86_64.tar.gz -C /
6.3 验证
ls -l /usr/bin/osqueryi /usr/bin/osqueryctl
osqueryi --version
实测回显(/usr/bin 下是符号链接,真实二进制在 /opt/osquery/bin/):
lrwxrwxrwx 1 1001 1001 27 Jun 24 17:18 /usr/bin/osqueryctl -> /opt/osquery/bin/osqueryctl
lrwxrwxrwx 1 1001 1001 25 Jun 24 17:18 /usr/bin/osqueryd -> /opt/osquery/bin/osqueryd
lrwxrwxrwx 1 1001 1001 25 Jun 24 17:18 /usr/bin/osqueryi -> /opt/osquery/bin/osqueryd
osqueryi version 5.23.1
💡 说明:tar.gz 产物自带示例配置
/opt/osquery/share/osquery/osquery.example.conf与 packs 目录(deb 包不含);在无任何配置时执行osqueryctl status会明确提示该路径,可直接拷贝为/etc/osquery/osquery.conf起步。无配置时执行osqueryctl status的实测回显:
No config file found at /etc/osquery/osquery.conf
Additionally, no flags file or config override found at /etc/osquery/osquery.flags
See '/opt/osquery/share/osquery/osquery.example.conf' for an example config.
七、方案对比与选择建议
7.1 三方案对照表
| 维度 | 方案 A:apt 仓库 | 方案 B:手动 .deb | 方案 C:tar.gz |
|---|---|---|---|
| 是否需配置源 | 是(keyring+list 文件) | 否 | 否 |
| 是否需联网 | 安装时需 | 下载时需(可离线分发) | 下载时需(可离线分发) |
| GPG 校验 | apt 自动 | 手动比对 SHA256 | 手动比对 SHA256 |
| 升级方式 | apt-get upgrade | 重新下载 + dpkg -i | 重新解压覆盖 |
| 降级/锁版本 | apt-get install osquery=<版本> | 下载对应版本包 | 下载对应版本解压 |
| systemd 单元 | 自带 osqueryd.service | 自带 | 不自带,需自写 |
| dpkg 数据库登记 | 是 | 是 | 否 |
| 示例配置 | 无(/etc/osquery 为空) | 无 | 自带 example.conf |
| 适用场景 | 长期运行的服务器(推荐) | 内网分发/锁定版本 | 极简环境/多版本并存 |
7.2 选择建议
- 可联网的长期服务器:方案 A,一次配源后续免维护。
- 内网隔离或需要先审查再分发:方案 B,SHA256 校验后
dpkg -i。 - 临时验证、多版本共存:方案 C,随解随用随删。
八、osqueryd 配置与 systemd 托管
osqueryi 装完即可用;osqueryd(守护进程)必须先有配置才能产出有价值的监控数据。本章按 deb 包自带的 systemd 单元约定,完成「配置文件编写 → 校验 → 托管 → 结果落盘验证」全流程。
8.1 配置文件布局
deb 包自带 systemd 单元与环境文件,先看清约定再动手:
cat /etc/default/osqueryd
实测回显(deb 包自带):
FLAG_FILE="/etc/osquery/osquery.flags"
CONFIG_FILE="/etc/osquery/osquery.conf"
LOCAL_PIDFILE="/var/osquery/osqueryd.pidfile"
PIDFILE="/var/run/osqueryd.pidfile"
逐项解说:
FLAG_FILE:启动 flag 文件(命令行参数持久化),unit 的ExecStartPre会自动创建空文件。CONFIG_FILE:主配置 JSON 路径。LOCAL_PIDFILE/PIDFILE:osqueryd 先在/var/osquery写 pid,ExecStartPre 把它移动到/var/run,避免权限问题。
cat /usr/lib/systemd/system/osqueryd.service
实测回显(deb 包自带,全文):
[Unit]
Description=osquery daemon
After=network.target syslog.service
[Service]
TimeoutStartSec=0
EnvironmentFile=/etc/default/osqueryd
ExecStartPre=/bin/sh -c "if [ ! -f $FLAG_FILE ]; then touch $FLAG_FILE; fi"
ExecStartPre=/bin/sh -c "if [ -f $LOCAL_PIDFILE ]; then mv $LOCAL_PIDFILE $PIDFILE; fi"
ExecStart=/opt/osquery/bin/osqueryd \
--flagfile $FLAG_FILE \
--config_path $CONFIG_FILE
Restart=on-failure
KillMode=control-group
KillSignal=SIGTERM
TimeoutStopSec=15
CPUQuota=20%
[Install]
WantedBy=multi-user.target
逐项解说:Restart=on-failure 表示异常退出自动拉起;CPUQuota=20% 是官方内置的资源保护——osqueryd 单核占用上限 20%,防止查询反噬业务;ExecStart 直接使用 /opt/osquery/bin/osqueryd 真实二进制并传 flagfile 与 config 两个入口参数。deb 包同时提供 osqueryctl 辅助命令({clean|config-check|start|stop|status|restart},osqueryctl 不带参数时输出该用法)。
8.2 编写 osquery.flags 与 osquery.conf
先创建 flag 文件。plugin 类参数(config_plugin/logger_plugin/logger_path/database_path)属于 CLI-only flag,必须写在这里,写在配置 JSON 的 options 里会被忽略(实测警告见 8.3 节排障):
cat > /etc/osquery/osquery.flags <<'FL'
--config_plugin=filesystem
--logger_plugin=filesystem
--logger_path=/var/log/osquery
--database_path=/var/osquery/osquery.db
FL
逐项解说:config_plugin=filesystem 从本地 JSON 文件读配置;logger_plugin=filesystem 结果以 JSON 行落盘;logger_path 结果日志目录;database_path RocksDB 数据库位置(osqueryd 用于存储事件缓存与查询状态)。
再创建主配置文件(schedule 周期查询 + pack 查询包):
mkdir -p /etc/osquery/packs
cat > /etc/osquery/osquery.conf <<'OC'
{
"options": {
"utc": "true",
"schedule_splay_percent": 10,
"disable_events": "false"
},
"schedule": {
"osquery_info": {
"query": "SELECT * FROM osquery_info;",
"interval": 60,
"removed": false
},
"listening_ports": {
"query": "SELECT * FROM listening_ports;",
"interval": 120
},
"system_info": {
"query": "SELECT hostname, cpu_brand, physical_memory FROM system_info;",
"interval": 300
},
"suid_binaries": {
"query": "SELECT path, username, groupname, permissions FROM suid_bin;",
"interval": 600
}
},
"packs": {
"osquery-monitoring": "/etc/osquery/packs/osquery-monitoring.conf"
}
}
OC
逐项解说:
options.utc:日志时间戳用 UTC(默认 true)。options.schedule_splay_percent:调度散列抖动百分比,多台机器同配置时把各查询的实际执行时间打散 ±10%,避免同时执行造成查询风暴。options.disable_events=false:开启事件订阅(audit/inotify/BPF 事件表可用)。schedule:四条周期查询,键名即查询名(将出现在结果日志的name字段);interval单位秒;removed: false表示不输出「消失行」的差异记录(对 osquery_info 这类单行快照表,差异模式意义不大)。packs:按文件引用查询包,pack 内查询与 schedule 语义相同,便于按主机角色拆分维护。
创建 pack 文件:
cat > /etc/osquery/packs/osquery-monitoring.conf <<'PK'
{
"queries": {
"osquery_meta": {
"query": "SELECT pid FROM osquery_info;",
"interval": 120,
"description": "osquery daemon heartbeat"
},
"new_suid_bin": {
"query": "SELECT path, username FROM suid_bin;",
"interval": 900,
"snapshot": false
}
}
}
PK
逐项解说:pack 以 queries 为根;description 仅作注释用途;snapshot: false(显式声明)表示差异模式——new_suid_bin 只在新出现或消失 SUID 文件时产生结果行,是典型的低噪声安全监控查询。
8.3 配置校验(osqueryctl config-check)
osqueryctl config-check
echo "exit=$?"
实测回显(配置正确时无任何输出、退出码 0):
exit=0
⚠️ 排障实录(CLI-only flag 误放位置):若把
--config_plugin/--logger_plugin/--database_path写进osquery.conf的options,config-check会通过但出现如下实测警告——这些参数不会生效,必须移入 flag 文件:W1018 08:20:00.419523 972 options.cpp:106] The CLI only flag --config_plugin set via config file will be ignored, please use a flagfile or pass it to the process at startup W1018 08:20:00.419556 972 options.cpp:106] The CLI only flag --logger_plugin set via config file will be ignored, please use a flagfile or pass it to the process at startup W1018 08:20:00.419575 972 options.cpp:106] The CLI only flag --database_path set via config file will be ignored, please use a flagfile or pass it to the process at startup把三项移入
/etc/osquery/osquery.flags后重跑config-check,警告消失(实测回显即本节开头的exit=0)。
8.4 systemd 托管:enable、start 与状态验证
systemctl enable osqueryd
实测回显:
Executing: /usr/lib/systemd/systemd-sysv-install enable osqueryd
Created symlink /etc/systemd/system/multi-user.target.wants/osqueryd.service → /usr/lib/systemd/system/osqueryd.service.
systemctl start osqueryd
sleep 4
systemctl is-active osqueryd
实测回显:
active
systemctl status osqueryd --no-pager -l
实测回显(输出节选(实测首部),CGroup 进程树略):
● osqueryd.service - osquery daemon
Loaded: loaded (/usr/lib/systemd/system/osqueryd.service; enabled; preset: enabled)
Active: active (running) since Sun 2026-10-18 08:20:17 UTC; 4s ago
Process: 1215 ExecStartPre=/bin/sh -c if [ ! -f $FLAG_FILE ]; then touch $FLAG_FILE; fi (code=exited, status=0/SUCCESS)
Process: 1217 ExecStartPre=/bin/sh -c if [ -f $LOCAL_PIDFILE ]; then mv $LOCAL_PIDFILE $PIDFILE; fi (code=exited, status=0/SUCCESS)
Main PID: 1219 (osqueryd)
Tasks: 14 (limit: 5753)
Memory: 15.3M (peak: 16.7M)
CPU: 46ms
验证要点:
Active: active (running)且两个ExecStartPre均status=0/SUCCESS;常驻后 osqueryd 内存约 15MB(峰值 16.7MB),符合官方轻量 Agent 定位。osqueryd 实际以「watcher + worker」双进程运行(Tasks 14),worker 异常时 watcher 负责重建。
用 osqueryctl status 可获得等价状态视图(内部即转发 systemctl):
osqueryctl status
实测回显(输出节选(实测首部)):
● osqueryd.service - osquery daemon
Loaded: loaded (/usr/lib/systemd/system/osqueryd.service; enabled; preset: enabled)
Active: active (running) since Sun 2026-10-18 08:20:53 UTC; 36s ago
Process: 1396 ExecStartPre=/bin/sh -c if [ ! -f $FLAG_FILE ]; then touch $FLAG_FILE; fi (code=exited, status=0/SUCCESS)
Process: 1398 ExecStartPre=/bin/sh -c if [ -f $LOCAL_PIDFILE ]; then mv $LOCAL_PIDFILE $PIDFILE; fi (code=exited, status=0/SUCCESS)
Main PID: 1400 (osqueryd)
8.5 调度结果落盘验证
daemon 启动后先确认日志目录产物:
ls -la /var/log/osquery/
实测回显(INFO 日志按启动时间滚动命名,results.log 为结果日志):
total 16
drwxr-xr-x 2 root root 4096 Oct 18 08:20 .
drwxr-xr-x 1 root root 4096 Oct 18 08:19 ..
lrwxrwxrwx 1 root root 34 Oct 18 08:20 osqueryd.INFO -> osqueryd.INFO.20261018-082017.1222
-rw-r--r-- 1 root root 703 Oct 18 08:20 osqueryd.INFO.20261018-082017.1222
-rw-r----- 1 root root 0 Oct 18 08:20 osqueryd.results.log
查看 systemd 侧启动日志(确认版本与事件发布器状态):
journalctl -u osqueryd -n 4 --no-pager --output=short-precise
实测回显:
Oct 18 08:20:53.345452 srv-ops-01 osqueryd[1403]: I1018 08:20:53.345361 1403 eventfactory.cpp:156] Event publisher not enabled: BPFEventPublisher: Publisher disabled via configuration
Oct 18 08:20:53.345452 srv-ops-01 osqueryd[1403]: I1018 08:20:53.345444 1403 eventfactory.cpp:156] Event publisher not enabled: auditeventpublisher: Publisher disabled via configuration
Oct 18 08:20:53.345452 srv-ops-01 osqueryd[1403]: I1018 08:20:53.345451 1403 eventfactory.cpp:156] Event publisher not enabled: inotify: Publisher disabled via configuration
Oct 18 08:20:53.345452 srv-ops-01 osqueryd[1403]: I1018 08:20:53.345456 1403 eventfactory.cpp:156] Event publisher not enabled: syslog: Publisher disabled via configuration
说明:这四条
Event publisher not enabled是 5.23.1 的默认信息级日志——事件发布器需在配置中显式开启(配合options.disable_events=false与对应--enable_*flag),默认不订阅不影响 schedule 周期查询。
等待一个调度周期(osquery_info interval 60 秒)后查看结果落盘:
grep 'osquery_info' /var/log/osquery/osqueryd.results.log | tail -1
实测回显(单行 JSON,过长节选首部):
{"name":"osquery_info","hostIdentifier":"srv-ops-01","calendarTime":"Sun Oct 18 08:21:45 2026 UTC","unixTime":1792311705,"epoch":0,"counter":1,"numerics":false,"columns":{"build_distro":"centos7","build_platform":"linux","config_hash":"dbe4036c96f9df02c1ce47ab2da5415403ea50e2","config_valid":"1","extensions":"active","instance_id":"dda22b7d-960e-4c1c-9bde-2ffa247fd7f1","pid":"1403",...},"action":"added"}
grep 'system_info' /var/log/osquery/osqueryd.results.log | tail -1
实测回显:
{"name":"system_info","hostIdentifier":"srv-ops-01","calendarTime":"Sun Oct 18 08:22:19 2026 UTC","unixTime":1792311739,"epoch":0,"counter":0,"numerics":false,"columns":{"cpu_brand":"Intel(R) Core(TM) i7-14700KF","hostname":"srv-ops-01","physical_memory":"33607307264"},"action":"added"}
验证要点:
name与配置中的查询键一致、columns是查询结果的列值、config_valid: "1"说明配置加载成功。至此「配置 → 托管 → 调度 → 落盘」闭环完成。
九、常见问题(FAQ)
Q1:osqueryd 服务显示 active,但 osqueryd.results.log 一直为空、调度不执行,怎么回事?
实测排障:把 osquery.conf 改成非法 JSON 后 systemctl restart osqueryd,服务仍 active,journalctl 出现解析错误:
E1018 08:29:01.912009 1621 config.cpp:968] updateSource failed to parse config, of source: /etc/osquery/osquery.conf and content: { broken json
W1018 08:29:01.912137 1621 init.cpp:760] Error reading config: Error parsing the config JSON
原因:osqueryd 对配置解析失败采取「告警但继续运行」策略——进程活着但 schedule/packs 全部未加载。定位手段:看 journalctl 中 config.cpp 的 updateSource failed to parse config 行(会附上原始内容),或平时改配置后先跑 osqueryctl config-check 再重启。修复:改正 JSON 语法后 systemctl restart osqueryd,恢复后 results.log 恢复写入(config_valid 字段恢复为 "1")。
Q2:手动再起一个 osqueryd 报 Pidfile busy / RocksDB LOCK 冲突?
服务已运行时手动执行 osqueryd,实测报:
E1018 08:29:29.652535 1956 init.cpp:520] osqueryd Pidfile check failed: Pidfile::Error::Busy
若手动实例绕开 pidfile(换 --pidfile),还会撞上数据库锁:
I1018 08:29:38.160914 1967 rocksdb.cpp:181] Rocksdb open failed (5:0) IO error: While lock file: /var/osquery/osquery.db/LOCK: Resource temporarily unavailable
原因:同一台机器同一时间只允许一个 osqueryd 实例(pidfile 与 RocksDB 锁双重互斥)。解决:确认 systemctl is-active osqueryd 为 active 就不要再手动起 daemon;调试用 osqueryi(只读查询、不占锁)。
Q3:命令行传了不存在的 flag 就起不来,但 flagfile 里写了却没事?
实测对照:命令行直接传未知 flag,osqueryd 立即报错退出:
ERROR: unknown command line flag 'bad-flag-nonsense'
同样的 flag 写进 --flagfile 指向的文件,osqueryd 正常启动(该 flag 被忽略)。这是 gflags 两种传参方式的容错差异。因此 systemd 托管下修改 /etc/osquery/osquery.flags 不会导致服务起不来,但会出现「flag 静默不生效」的隐蔽问题——写完 flagfile 后可用 osqueryi --flagfile /etc/osquery/osquery.flags --version 做一次加载验证(未知 flag 在 osqueryi 下同样被忽略,重点确认文件可读、语法无空格错误)。
Q4:查询 listening_ports 时 pid 列都是 -1,JOIN processes 关联不上进程?
实测现象:SELECT port, pid, fd FROM listening_ports WHERE port = 80; 返回 pid: -1, fd: -1,而 nginx 明明在监听。原因:osquery 通过 socket inode 匹配 /proc/<pid>/fd/ 下的符号链接定位进程;当安全模块(如 AppArmor 的默认限制策略)阻止读取其他进程的 fd 链接时,匹配失败、pid 落为 -1(ss -tlnp 的 Process 列同样为空,可交叉验证)。解决:调整安全模块策略允许该读取(例如将 osqueryd 服务纳入对应的本地安全配置例外);在无此类限制的标准服务器上,同一查询可正常返回 pid 与进程名。
Q5:apt-get update 报 The repository 'https://pkg.osquery.io/deb deb InRelease' is not signed?
这是 4.2 节排障 1 的完整现象:keyring 没有经过 gpg --dearmor(或路径与 signed-by 不一致)。按 4.2 节命令重新生成 keyring 并核对 /etc/apt/sources.list.d/osquery.list 中 signed-by= 指向的路径即可。
Q6:为什么 /etc/osquery/ 是空的?osqueryd 没有默认配置吗?
是官方刻意行为:deb 包不内置默认配置(避免以不适合所有环境的默认值静默启动)。起步可参考 /opt/osquery/share/osquery/osquery.example.conf(tar.gz 产物自带;deb 用户可直接按第八章 8.2 节手工编写,内容更精简可控)。
十、卸载与清理
10.1 卸载 deb 包(方案 A/B)
systemctl stop osqueryd 2>/dev/null
apt-get remove -y osquery
实测回显:
(Reading database ... 6396 files and directories currently installed.)
Removing osquery (5.23.1-1.linux) ...
验证已卸载且二进制清理干净:
which osqueryi || echo 'osqueryi 已卸载'
ls /opt/osquery 2>/dev/null || echo '/opt/osquery 已清理'
实测回显:
osqueryi 已卸载
/opt/osquery 已清理
彻底清除配置与运行数据(含 dpkg 配置残留):
dpkg --purge osquery
rm -rf /var/log/osquery /var/osquery /etc/osquery
10.2 清理软件源(方案 A)
rm -f /etc/apt/sources.list.d/osquery.list /usr/share/keyrings/osquery-archive-keyring.gpg
apt-get update
10.3 清理 tar.gz 方案(方案 C)
rm -rf /opt/osquery /usr/bin/osqueryi /usr/bin/osqueryd /usr/bin/osqueryctl \
/var/osquery /var/log/osquery /etc/osquery
十一、参考链接
- osquery 官方主页与文档:https://osquery.io
- 官方下载页(版本与各平台包 SHA256,查询日期 2026-10-18):https://osquery.io/downloads
- GitHub Releases(5.23.1 为 Latest,5.24.0 为 Pre-release,查询日期 2026-10-18):https://github.com/osquery/osquery/releases
- osquery deb 仓库根(suite
deb):https://pkg.osquery.io/deb - 官方 deb 包(amd64):https://pkg.osquery.io/deb/osquery_5.23.1-1.linux_amd64.deb
- 官方 tar.gz 包(x86_64):https://pkg.osquery.io/linux/osquery-5.23.1_1.linux_x86_64.tar.gz
- GitHub Releases 资产清单(5.23.1 全平台安装包):https://github.com/osquery/osquery/releases/expanded_assets/5.23.1
- 官方配置文档(Configuration):https://osquery.readthedocs.io/en/latest/deployment/configuration/
- Schema 表手册:https://osquery.io/schema
- 本组件同系列文档:《osquery 5.23.1 介绍与选型指南》、《osquery 5.23.1 查询语言/DSL 详解》