TExcelTAD
Home › Linux Server Administration › Reading Logs with journalctl

Reading Logs with journalctl

Everything a service writes to stdout and stderr, every message from the kernel and from systemd itself, and everything sent to syslog ends up in the journal, run by systemd-journald. The journal is structured: each entry carries fields such as the unit, the PID, the priority and the boot it belongs to, so you can filter precisely instead of grepping through text files. journalctl reads it; it needs a running journald (or journal files), so the transcripts here are sample sessions from an Ubuntu 24.04 server.

The filters you use every day

Question Command
What did this service log? journalctl -u nginx
...recently, newest first, no pager? journalctl -u nginx -n 50 --no-pager / -r
...live? journalctl -u nginx -f
Only errors and worse? journalctl -p err (priorities emerg alert crit err warning notice info debug)
In a time window? journalctl --since "2026-01-15 09:00" --until "2026-01-15 09:30", --since "1 hour ago", --since today
Since the current boot / the previous boot? journalctl -b, journalctl -b -1 (--list-boots lists them)
The kernel log (OOM kills, disk errors)? journalctl -k (like dmesg, but kept across boots)
Several units together? journalctl -u nginx -u inventory
By any field? journalctl _PID=4182, journalctl _UID=1001, journalctl SYSLOG_IDENTIFIER=sshd
Only the message text, for piping? journalctl -u app -o cat
Machine-readable? journalctl -u app -o json (one JSON object per line)

Filters combine: different fields are ANDed, the same field given twice is ORed.

bash
$ journalctl -u ssh -p warning --since "2026-01-15 00:00" --no-pager | tail -3
Jan 15 03:12:44 web1 sshd[2210]: error: kex_exchange_identification: Connection closed by remote host
Jan 15 03:12:51 web1 sshd[2214]: Invalid user oracle from 203.0.113.7 port 50122
Jan 15 03:12:53 web1 sshd[2214]: Connection closed by invalid user oracle 203.0.113.7 port 50122 [preauth]
$ journalctl -b -1 -p err --no-pager | head -2
Jan 14 22:41:09 web1 kernel: Out of memory: Killed process 3180 (java) total-vm:4421336kB, anon-rss:1802244kB, file-rss:0kB, shmem-rss:0kB, UID:1002 pgtables:3880kB oom_score_adj:0
Jan 14 22:41:09 web1 systemd[1]: reports.service: A process of this unit has been killed by the OOM killer.

The second command is the one to remember after an unexpected reboot or crash: errors from the previous boot.

Structured output

-o json shows the fields behind each line. A trimmed entry:

json
{"__REALTIME_TIMESTAMP":"1768471202000000","_HOSTNAME":"web1","PRIORITY":"3","_SYSTEMD_UNIT":"inventory.service","SYSLOG_IDENTIFIER":"inventory","_PID":"4301","MESSAGE":"error: open /etc/inventory/env: permission denied","_BOOT_ID":"6c1e0b4f0d6b4a5f9a2b1f8e3c7d9a10"}

Fields with a leading underscore are added by journald itself and can't be faked by the app; the others come from the sender. Because each entry is one JSON object per line, summaries are easy to script. This model counts errors per unit from a saved journalctl -o json export:

python · try it yourself
import json

export = """\
{"PRIORITY":"6","_SYSTEMD_UNIT":"nginx.service","MESSAGE":"started"}
{"PRIORITY":"3","_SYSTEMD_UNIT":"inventory.service","MESSAGE":"db timeout"}
{"PRIORITY":"4","_SYSTEMD_UNIT":"inventory.service","MESSAGE":"slow query 2.1s"}
{"PRIORITY":"3","_SYSTEMD_UNIT":"inventory.service","MESSAGE":"db timeout"}
{"PRIORITY":"2","_SYSTEMD_UNIT":"backup.service","MESSAGE":"disk full"}
{"PRIORITY":"3","_SYSTEMD_UNIT":"nginx.service","MESSAGE":"upstream timed out"}
"""
NAMES = ["emerg", "alert", "crit", "err", "warning", "notice", "info", "debug"]

counts = {}
for line in export.splitlines():
    e = json.loads(line)
    prio = int(e["PRIORITY"])
    if prio <= 3:                                   # like journalctl -p err
        key = (e["_SYSTEMD_UNIT"], NAMES[prio])
        counts[key] = counts.get(key, 0) + 1

for (unit, level), n in sorted(counts.items(), key=lambda kv: (-kv[1], kv[0])):
    print(f"{n:>3}  {level:<5} {unit}")
  2  err   inventory.service
  1  crit  backup.service
  1  err   nginx.service

Apps get the most out of the journal when they log one event per line to stdout, with a level. systemd reads a <3> prefix (sd-daemon levels) at the start of a line as the priority, so print("<3>db timeout") arrives as an err entry without any logging library.

Persistence and size

On Ubuntu and Debian the journal is persistent (/var/log/journal/). On some distributions it lives in memory (/run/log/journal/) and is lost at reboot until that directory is created. The limits are set in /etc/systemd/journald.conf or, better, a drop-in:

ini
# /etc/systemd/journald.conf.d/50-size.conf
[Journal]
Storage=persistent
SystemMaxUse=1G          # cap on disk usage
MaxRetentionSec=1month   # drop older entries
RateLimitIntervalSec=30s # per-service flood protection:
RateLimitBurst=10000     # at most 10000 messages per 30 s per service
bash
$ journalctl --disk-usage
Archived and active journals take up 812.0M in the file system.
$ sudo journalctl --vacuum-size=500M
Vacuuming done, freed 304.0M of archived journals from /var/log/journal/0f6c….
$ sudo systemctl restart systemd-journald     # after editing journald.conf

When a chatty service hits the rate limit, journald drops messages and logs Suppressed 4210 messages from inventory.service. If you see that line, either the app logs too much or the limit is too low for it.

The journal and log shipping

The journal is local. On a fleet you ship logs to a central system (Loki, Elasticsearch, a cloud logging service) with an agent that reads the journal (Promtail's successor Grafana Alloy, Vector, Fluent Bit, the OpenTelemetry Collector's journald receiver), or forward to rsyslog with ForwardToSyslog=yes. The Observability course builds that pipeline; the fields you filter on here (_SYSTEMD_UNIT, PRIORITY, _HOSTNAME) become the labels you query there.

Traditional text logs in /var/log/ still exist (nginx access logs, auth.log from rsyslog, application files), and they need rotation: Log Files & logrotate.

Test yourself

The server rebooted unexpectedly at night. Which command shows the errors logged before the reboot?

  1. journalctl -b -p err
  2. journalctl -b -1 -p err
  3. dmesg
  4. journalctl -f

Why are fields like _SYSTEMD_UNIT and _PID written with a leading underscore?

  1. They are deprecated
  2. They are trusted fields added by journald, not by the logging program
  3. They are hidden from journalctl
  4. They are only for the kernel

How can a service mark a line on stdout as an error without a logging library?

  1. Print it in red
  2. Prefix it with <3>
  3. Write it to /var/log/error.log
  4. Exit with code 3