Skip to main content
RunBook Academy

LinuxVIII · Logging and journaldJournal queries

journalctl — querying the systemd journal

Foundation⏱ ~12 minbashjournalctl

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

Not yet marked complete on this device.

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

Read-only / Safejournalctl -u
$ journalctl -u sshd --since '1 hour ago' --no-pager
Aug  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

Read-only / Safejournalctl -p
$ journalctl -p err --since '1 hour ago' --no-pager
Aug  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 admin

Illustrative output

Time filters

Read-only / Safetime filters
$ journalctl --since "2026-08-09 11:00" --until "2026-08-09 12:00" --no-pager; journalctl -b; journalctl -b -1
...

Illustrative output

FilterMeaning
--since "1 hour ago"Last hour
--since "2026-08-09"Since midnight on that date
--until "2026-08-09 12:00"Until that time
-bSince the current boot
-b -1Since the previous boot
--since yesterdaySince 00:00 yesterday

Following in real time

Read-only / Safefollow
$ journalctl -u sshd -f
...

Illustrative output

Field filters

The journal stores structured fields, not just text. You can filter by any field:

Read-only / Safefield filters
$ journalctl _UID=1000 --since '1 hour ago' --no-pager; journalctl _PID=12345 --no-pager
...

Illustrative output

FieldMeaning
_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:

Read-only / Safecombined filters
$ journalctl -u sshd -p err --since '1 hour ago' SYSLOG_IDENTIFIER=sshd --no-pager
...

Illustrative output

Read-only / SafeOR
$ journalctl -u sshd + _UID=33 --since '1 hour ago' --no-pager
...

Illustrative output

Output formats

Read-only / Safeoutput 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

FormatUse
shortDefault; ISO date without year
short-isoFull ISO 8601
short-preciseISO with microseconds
jsonOne JSON object per line
json-prettyPretty-printed JSON
catJust the message, no metadata
with-unitLike default but with [unit] prefix on each line

Exporting for analysis

Read-only / Safeexport
$ 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

  1. Always combine filters. -u sshd -p err is faster and more useful than -u sshd
  2. **Use --no-pager in scripts.** Paging breaks automation
  3. **Export with -o json** when piping to jq or a SIEM
  4. **Use -f only interactively.** Following in scripts is rarely useful
  5. **Check --list-boots after a reboot** to find previous-session logs
  6. **Tune SystemMaxUse** in /etc/systemd/journald.conf for retention tuning
  7. **Verify with --verify** in CI that the journal is recording what you expect

Knowledge check

Knowledge check · 3 questions

  1. Q1. What does `journalctl -u sshd -p err` show?

  2. 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.

  3. Q3. Which of the following are correct journalctl practices? Select all that apply.

Passing score: 75%. Answers are checked in this browser.