LinuxVIII · Logging and journaldJournal queries
journalctl — querying the systemd journal
What you'll learn
- Filter journal entries by unit, time, priority, and arbitrary fields
- Follow a service's log in real time
- Export journal entries in text or JSON for further analysis
- Verify the journal is recording what you expect
Prerequisites
Verified against Ubuntu 24.04 LTS · Debian 12 (Bookworm) · RHEL 9.x · Rocky Linux 9.x · AlmaLinux 9.x · Linux kernel 6.1 LTS / 6.6 LTS · systemd 255+ · OpenSSH 8.7p1 (RHEL 9) / 9.6p1 (Ubuntu 24.04) · nftables 1.0.x · chrony 4.x · Pacemaker 2.1.x · Corosync 3.1.x · 2026-08-09
journalctl is the diagnostic tool you will reach for more often
than any other on a systemd-managed host. Mastering its filters
turns a 30-minute diagnostic into a 30-second query.
The basic queries
$ journalctl -u sshd --since '1 hour ago' --no-pagerAug 9 11:23:45 host sshd[12345]: Accepted publickey for alice from 10.0.0.5 port 51234 ssh2
Aug 9 11:24:12 host sshd[12345]: pam_unix(sshd:session): session opened for user alice by (uid=0)
...Illustrative output
$ journalctl -p err --since '1 hour ago' --no-pagerAug 9 11:42:01 host kernel: Out of memory: Killed process 12345 (myapp) total-vm:...
Aug 9 11:45:23 host myapp[12345]: ERROR database connection refused
Aug 9 11:50:01 host sshd[23456]: error: maximum authentication attempts exceeded for adminIllustrative output
Time filters
$ journalctl --since "2026-08-09 11:00" --until "2026-08-09 12:00" --no-pager; journalctl -b; journalctl -b -1...Illustrative output
| Filter | Meaning |
|---|---|
--since "1 hour ago" | Last hour |
--since "2026-08-09" | Since midnight on that date |
--until "2026-08-09 12:00" | Until that time |
-b | Since the current boot |
-b -1 | Since the previous boot |
--since yesterday | Since 00:00 yesterday |
Following in real time
$ journalctl -u sshd -f...Illustrative output
Field filters
The journal stores structured fields, not just text. You can filter by any field:
$ journalctl _UID=1000 --since '1 hour ago' --no-pager; journalctl _PID=12345 --no-pager...Illustrative output
| Field | Meaning |
|---|---|
_UID= | User ID |
_GID= | Group ID |
_PID= | Process ID |
_BOOT_ID= | Boot ID |
_SYSTEMD_UNIT= | systemd unit name |
_COMM= | Process name (comm) |
_EXE= | Executable path |
_HOSTNAME= | Hostname |
_TRANSPORT= | How the message arrived (syslog, journal, stdout, kernel) |
_KERNEL_DEVICE= | Kernel device (for kernel messages) |
Combining filters
Filters combine as AND:
$ journalctl -u sshd -p err --since '1 hour ago' SYSLOG_IDENTIFIER=sshd --no-pager...Illustrative output
$ journalctl -u sshd + _UID=33 --since '1 hour ago' --no-pager...Illustrative output
Output formats
$ journalctl -u sshd --since '1 hour ago' -o short-iso --no-pager; journalctl -u sshd --since '1 hour ago' -o json --no-pager | head -1...Illustrative output
| Format | Use |
|---|---|
short | Default; ISO date without year |
short-iso | Full ISO 8601 |
short-precise | ISO with microseconds |
json | One JSON object per line |
json-pretty | Pretty-printed JSON |
cat | Just the message, no metadata |
with-unit | Like default but with [unit] prefix on each line |
Exporting for analysis
$ journalctl -u sshd --since '1 hour ago' --no-pager > /tmp/sshd.log; journalctl -u sshd --since '1 hour ago' -o json --no-pager | jq -r '.MESSAGE'...Illustrative output
Production patterns
- Always combine filters.
-u sshd -p erris faster and more useful than-u sshd - **Use
--no-pagerin scripts.** Paging breaks automation - **Export with
-o json** when piping to jq or a SIEM - **Use
-fonly interactively.** Following in scripts is rarely useful - **Check
--list-bootsafter a reboot** to find previous-session logs - **Tune
SystemMaxUse** in/etc/systemd/journald.conffor retention tuning - **Verify with
--verify** in CI that the journal is recording what you expect
Knowledge check
Knowledge check · 3 questions
Q1. What does `journalctl -u sshd -p err` show?
Q2. To read the log of the boot that ended in a crash, you run `journalctl -b -1` after the host has come back up.
Q3. Which of the following are correct journalctl practices? Select all that apply.
Passing score: 75%. Answers are checked in this browser.