osquery on Linux servers: install it, ask useful questions, and trust the answers
osquery lets you ask a Linux server questions in SQL: who can log in, what is listening, which binaries are setuid. Install it from the official repository, run the queries worth knowing, avoid the cases where an empty answer is wrong, and schedule it so the answers are kept.
osquery turns a running system into SQL tables. Users, SSH keys, listening ports, processes, packages and sudo rules each become a table you can query and join. It started at Facebook, it’s open source, and it runs on Linux, macOS and Windows. For a small team with a few Linux servers it answers the questions an auditor or a customer questionnaire asks (who can log in, what’s exposed, what changed) with one query instead of five commands per server. osquery on GitHub ↗
It has two programs. osqueryi is an interactive shell for asking questions now. osqueryd is a daemon that runs queries on a schedule and logs the results. The two don’t talk to each other unless you connect them, which matters later. osquery doesn’t block anything or send alerts by itself; it answers questions and writes logs.
Install it from the official repository
The osquery project publishes signed packages for x86_64 and arm64. On Debian and Ubuntu, add the key and the repository, then install: Installing osquery on Linux ↗
sudo mkdir -p /etc/apt/keyrings
curl -fsSL https://pkg.osquery.io/deb/pubkey.gpg | sudo tee /etc/apt/keyrings/osquery.asc >/dev/null
echo "deb [signed-by=/etc/apt/keyrings/osquery.asc] https://pkg.osquery.io/deb deb main" | sudo tee /etc/apt/sources.list.d/osquery.list
sudo apt update && sudo apt install osqueryThe project’s own page uses add-apt-repository for the second step. Writing the file with tee does the same thing and works on minimal Debian installs, where that command isn’t present. On RHEL, Rocky, Alma and Fedora:
curl -fsSL https://pkg.osquery.io/rpm/GPG | sudo tee /etc/pki/rpm-gpg/RPM-GPG-KEY-osquery >/dev/null
sudo curl -fsSL -o /etc/yum.repos.d/osquery.repo https://pkg.osquery.io/rpm/osquery-rpm.repo
sudo dnf install osqueryCheck it worked with osqueryi --version. The package installs osqueryi, osqueryd and osqueryctl, a set of example query packs in /opt/osquery/share/osquery/packs/, and a systemd unit. It doesn’t start the daemon: you do that once there’s a config.
Ask the questions worth asking
Start the shell as root with sudo osqueryi. .tables lists every table and .schema authorized_keys shows a table’s columns. You can also pass a query as an argument, which is handy in scripts: sudo osqueryi --json "SELECT …". These are the queries that answer the usual security questions on a server. Every table and column here is in osquery’s table specs. osquery table specs ↗
Who has a login shell, and who is in the sudo group (wheel on RHEL):
SELECT username, uid, shell, directory FROM users
WHERE shell NOT LIKE '%nologin' AND shell NOT LIKE '%false';
SELECT u.username, g.groupname FROM users u
JOIN user_groups ug ON ug.uid = u.uid
JOIN groups g ON g.gid = ug.gid
WHERE g.groupname IN ('sudo', 'wheel', 'admin');Every SSH key on every account. Note the join with users; the next section explains why you can’t leave it out:
SELECT u.username, k.key_file, k.algorithm, k.comment
FROM users u JOIN authorized_keys k USING (uid);This shows which keys exist, not when each was last used. For that, match each key’s fingerprint against the login log, as in our authorized_keys audit. SSH authorized_keys audit →
What is listening on anything other than loopback, and which program owns it:
SELECT l.port, l.protocol, l.address, p.name, p.path
FROM listening_ports l JOIN processes p USING (pid)
WHERE l.address NOT IN ('127.0.0.1', '::1') AND l.port != 0;protocol is a number: 6 is TCP and 17 is UDP. An address of 0.0.0.0 or :: means every interface. If you’d rather paste ss -tulpn output and get each port explained in plain English, our open ports explainer does that in the browser. Open ports explainer →
Sudo rules, setuid binaries, running programs whose file has been deleted, and the installed version of the packages you care about:
SELECT source, header, rule_details FROM sudoers;
SELECT path, username, permissions FROM suid_bin;
SELECT pid, name, cmdline FROM processes WHERE on_disk = 0;
SELECT name, version FROM deb_packages WHERE name IN ('openssh-server', 'sudo');
-- RHEL: SELECT name, version, release FROM rpm_packages WHERE name IN ('openssh-server', 'sudo');A process with on_disk = 0 is running from a file that no longer exists. That happens legitimately for a while after a package upgrade, until the service restarts, but malware that deletes itself after starting looks the same. Check the name and command line before you worry, and restart the service if it’s just an old binary.
When an empty answer is wrong
osquery answers with what it could read. Three things make it return less than the truth without failing the query.
First, some tables read only the current user’s files unless you join users. authorized_keys and shell_history are marked this way in the source. SELECT * FROM authorized_keys run as root returns root’s keys only, and osquery prints a warning that the table “returns data based on the current user by default, consider JOINing against the users table”. In the shell you see the warning. In a scheduled query it goes to a log nobody reads. Always join users, as in the query above. authorized_keys table spec ↗
Second, run it as root. An ordinary user can’t read other users’ home directories or follow /proc/<pid>/exe for their processes, so keys, paths and process owners go missing from the answer. The daemon runs as root under systemd. The shell runs as whoever starts it.
Third, tables whose names end in _events (process_events, file_events, socket_events) are turned off by default and stay empty until you enable events with --disable_events=false and the specific event source. Enabling them is a bigger step: on busy servers the osquery docs recommend masking systemd-journald-audit.socket so audit records don’t flood the journal. Start with the state tables above, which need none of this. osquery command-line flags ↗ Installing osquery on Linux ↗
Keep the answers with osqueryd
Asking once tells you about today. The daemon asks on a schedule and writes the results to /var/log/osquery/, so you can show when a key appeared or a port opened. Put this in /etc/osquery/osquery.conf: osquery configuration ↗
{
"options": {
"host_identifier": "hostname",
"schedule_splay_percent": 10
},
"schedule": {
"listening_ports": {
"query": "SELECT l.port, l.protocol, l.address, p.name, p.path FROM listening_ports l JOIN processes p USING (pid) WHERE l.address NOT IN ('127.0.0.1', '::1') AND l.port != 0;",
"interval": 300
},
"suid_bin": {
"query": "SELECT path, username, permissions FROM suid_bin;",
"interval": 3600
},
"ssh_keys_daily": {
"query": "SELECT u.username, k.key_file, k.algorithm, k.comment FROM users u JOIN authorized_keys k USING (uid);",
"interval": 86400,
"snapshot": true
},
"sudoers_daily": {
"query": "SELECT source, header, rule_details FROM sudoers;",
"interval": 86400,
"snapshot": true
}
}
}There are two kinds of results. By default a query is differential: the first run logs every row as “added”, and later runs log only rows added or removed since the run before, in osqueryd.results.log. A new key or a newly opened port shows up as one line. With "snapshot": true the full result is written every time, to osqueryd.snapshots.log. A daily snapshot of SSH keys and sudo rules is a record of who had access on each day, which is the kind of evidence an access review asks for. osquery logging ↗
Check the config parses, then start the daemon:
sudo osqueryd --config_check --config_path /etc/osquery/osquery.conf && echo config OK
sudo systemctl enable --now osqueryd
systemctl status osqueryd --no-pagerThe scheduler runs each query when the clock reaches a multiple of its interval (plus the splay), not at start-up, so the first listening_ports rows appear within about five minutes and the daily snapshots can take up to a day. To see results sooner while testing, give a query a short interval and change it back afterwards.
sudo tail -n 3 /var/log/osquery/osqueryd.results.log
sudo ls -l /var/log/osquery/Check the schedule is really running
osqueryd runs queries in a worker process watched by a watchdog. By default the worker may use 200 MB of memory and 10% CPU (sustained for 12 seconds). If a query pushes it over, the watchdog restarts the worker and denylists that query for 24 hours. The query simply stops producing results, which looks exactly like nothing changing. The Debian package’s systemd unit also caps the whole service at CPUQuota=20%. osquery debugging: watchdog ↗
Look for the watchdog in the status log:
sudo grep -h 'stopping:' /var/log/osquery/osqueryd.INFOLines such as “osqueryd worker (8368) stopping: Memory limits exceeded” mean a query was too heavy. Then ask the daemon itself which queries ran. Because osqueryi and osqueryd don’t share state, connect the shell to the daemon’s socket first:
sudo osqueryi
osquery> .connect /var/osquery/osquery.em
[*]osquery> SELECT name, executions, last_executed, denylisted FROM osquery_schedule;Every query should have executions above zero and denylisted 0. A query with denylisted 1 needs to be made cheaper (fewer columns, a WHERE clause, a longer interval) before it will run again. Profile a new query in osqueryi before you schedule it across servers.
Where osquery fits
osquery tells you what the system looks like each time it asks. It doesn’t record who ran a command between two runs, and it doesn’t compare file contents against a known-good copy. auditd records who did what, and AIDE tells you which files changed since the last check. Together the three cover state, actions and file changes on one server. auditd rules that matter → AIDE file integrity monitoring →
Across many servers, the logs have to leave each machine and someone has to read them. That means shipping the results to a log system you already run, or using a fleet manager. osquery’s README lists several (Fleet is the best known) and says plainly that the project doesn’t endorse or test them. Our Wazuh alternatives guide covers what running Fleet takes. Wazuh alternatives, including osquery with Fleet →
If you’d rather not write and maintain the queries yourself, Vigilhold, our host agent for Linux servers, watches SSH, processes, network connections and file changes on every server from one console. On the Business plan its compliance reports, mapped to SOC 2 and CIS among others, give your auditor evidence; certification still comes from the auditor. See Vigilhold →
The short version
| Step | Check it worked |
|---|---|
| Install from pkg.osquery.io | osqueryi --version prints a version |
| Ask questions as root | sudo osqueryi; keys and paths for every user appear |
| Join users for per-user tables | No “consider JOINing against the users table” warning |
| Schedule queries in osquery.conf | osqueryd --config_check passes; rows appear in /var/log/osquery/ |
| Snapshot keys and sudo rules daily | osqueryd.snapshots.log has one entry per day |
| Watch the watchdog | osquery_schedule shows executions above 0 and denylisted 0 |