8.4. Working with the Journal

systemd-journald collects records from three sources: every unit's stdout and stderr, the /dev/log socket that classic syslog callers write to, and the native journal API that carries structured fields. Records are stored in indexed binary files with full metadata - unit, PID, boot, priority - which is what makes the filtering below fast and reliable.

The journal's access rules apply immediately: until Section 8.4.3 adds john to the systemd-journal group, the records of other users and of the system as a whole are readable only by root. Grant that access now if you want to follow along from john's account - as root on client1:

# usermod -aG systemd-journal john

The command is silent. Group membership takes effect in new sessions, so john opens a fresh login (over SSH, as in Chapter 20) before reading the journal. The filters below are shown from that session; running them as root produces the same records.

8.4.1. Filtering by Unit, Boot, Time, and Priority

journalctl is the single reading tool; the options used here:

Option Selects
-u UNIT records generated under a unit
-t TAG records carrying a syslog identifier
-b [N] the current boot, or the Nth previous boot
--since, --until a time range ("10 min ago", "2026-11-19 09:00:00")
-p P records at priority P or worse
-n N the last N records (newest last; combined with -g, the N newest matches, newest first)
-g PATTERN records whose message matches a pattern; a lowercase pattern matches case-insensitively
-o FORMAT output format: short, short-precise, json-pretty, export
-f follow new records as they arrive

Read the heartbeat's recent records:

$ journalctl -u handbook-heartbeat.service -n 5 --no-pager
Nov 19 09:00:41 client1 handbook-heartbeat[65531]: heartbeat 2026-11-19T09:00:41+0000
Nov 19 09:00:42 client1 systemd[1]: Stopping handbook-heartbeat.service - Handbook heartbeat service...
Nov 19 09:00:46 client1 handbook-heartbeat[65531]: stopping
Nov 19 09:00:46 client1 systemd[1]: handbook-heartbeat.service: Deactivated successfully.
Nov 19 09:00:46 client1 systemd[1]: Stopped handbook-heartbeat.service - Handbook heartbeat service.

Every record made about and by the unit appears: the application's own lines and the manager's lifecycle messages. Boots partition the journal; list them as root (the boot listing reads all boots, not just the accessible slice):

# journalctl --list-boots --no-pager
IDX BOOT ID                          FIRST ENTRY                 LAST ENTRY
  0 b1a896bcffd84283bb0806857c472fbb Thu 2026-11-19 05:43:30 UTC Thu 2026-11-19 09:01:34 UTC

One boot recorded so far on this machine. -b restricts any query to a boot: -b alone means the current boot, -b -1 the previous one - with persistent storage (Section 8.4.2) the listing grows one row per boot, and journalctl -b -1 becomes the standard "what happened before the crash" query of Chapter 7. Time windows compose with any other filter:

$ journalctl --since "10 min ago" -u handbook-heartbeat.service -n 3 --no-pager
Nov 19 09:00:46 client1 handbook-heartbeat[65531]: stopping
Nov 19 09:00:46 client1 systemd[1]: handbook-heartbeat.service: Deactivated successfully.
Nov 19 09:00:46 client1 systemd[1]: Stopped handbook-heartbeat.service - Handbook heartbeat service.

Priorities follow syslog levels: emerg, alert, crit, err, warning, notice, info, debug. A threshold query shows everything at that level or worse:

$ journalctl -p warning -b -n 5 --no-pager
Nov 19 09:01:18 client1 systemd[1]: handbook-notready.service: start operation timed out. Terminating.
Nov 19 09:01:18 client1 systemd[1]: handbook-notready.service: Failed with result 'timeout'.
Nov 19 09:01:18 client1 systemd[1]: Failed to start handbook-notready.service - Handbook readiness timeout demonstration.
Nov 19 09:01:25 client1 systemd-ssh-generator[65879]: Failed to query local AF_VSOCK CID: Cannot assign requested address
Nov 19 09:01:25 client1 (sd-exec-[65870]: /usr/lib/systemd/system-generators/systemd-ssh-generator failed with exit status 1.

The first three lines are the readiness timeout from Section 8.3.4, now visible as the warnings they are. The last two come from this virtual machine's SSH socket generator probing for a VSOCK address that the guest does not have - environment noise, retained because warnings worth keeping and warnings worth ignoring look exactly the same until read. Pattern matching searches message texts:

$ journalctl -u handbook-heartbeat.service -g "reloaded" --no-pager
Nov 19 10:33:33 client1 handbook-heartbeat[78475]: reloaded interval=5s
Nov 19 10:33:33 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 10:34:52 client1 handbook-heartbeat[79214]: reloaded interval=2s
Nov 19 10:34:52 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 10:51:02 client1 handbook-heartbeat[85294]: reloaded interval=5s
Nov 19 10:51:02 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 10:52:49 client1 handbook-heartbeat[86035]: reloaded interval=2s
Nov 19 10:52:49 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 11:27:13 client1 handbook-heartbeat[91404]: reloaded interval=5s
Nov 19 11:27:13 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 11:27:52 client1 handbook-heartbeat[91608]: reloaded interval=2s
Nov 19 11:27:52 client1 systemd[1]: Reloaded handbook-heartbeat.service - Handbook heartbeat service.

Every reload of the heartbeat across the whole journal, application lines and manager lines interleaved by timestamp. The four earlier pairs are previous runs of the same two exercises on this system; the two newest are the reloads of Sections 8.2.1 and 8.3.2. Output formats trade human readability for precision. The default short format drops sub-second precision; short-precise keeps it:

$ journalctl -u handbook-heartbeat.service -n 1 -o short-precise --no-pager
Nov 19 09:00:46.017303 client1 systemd[1]: Stopped handbook-heartbeat.service - Handbook heartbeat service.

The json-pretty format shows what a record actually contains:

$ journalctl -u handbook-heartbeat.service -n 1 -o json-pretty --no-pager
{
    "SYSLOG_FACILITY" : "3",
    "_BOOT_ID" : "b1a896bcffd84283bb0806857c472fbb",
    "SYSLOG_IDENTIFIER" : "systemd",
    "_PID" : "1",
    "_SYSTEMD_UNIT" : "init.scope",
    "__REALTIME_TIMESTAMP" : "1795078846020132",
    "INVOCATION_ID" : "c064dd69124c4d58a255bb01db86c98b",
    "_COMM" : "systemd",
    "_SYSTEMD_SLICE" : "-.slice",
    "MESSAGE_ID" : "9d1aaa27d60140bd96365438aad20286",
    "MESSAGE" : "Stopped handbook-heartbeat.service - Handbook heartbeat service.",
    "UNIT" : "handbook-heartbeat.service",
    "_RUNTIME_SCOPE" : "system",
    "JOB_TYPE" : "stop",
    "_TRANSPORT" : "journal",
    "PRIORITY" : "6",
    "_UID" : "0",
    "__SEQNUM" : "45267",
    "_HOSTNAME" : "client1"
}

Selected output; several more fields were omitted. Field names beginning with an underscore are collected by systemd-journald itself - the timestamp, the boot, the transport - while the others come from the message: UNIT names the unit the record is about, and PRIORITY is the numeric syslog level. Any field can also filter directly, for example journalctl UNIT=handbook-heartbeat.service.

8.4.2. Persistent Storage, Retention, and Disk Usage

On this Debian system the journal is persistent: /var/log/journal exists, so records survive reboots and accumulate until a limit is reached. With Storage=auto - the default - journald keeps the directory it finds and falls back to volatile storage under /run/log/journal if it does not. Check the current footprint as root:

# journalctl --disk-usage
Archived and active journals take up 30M in the file system.

The total is the sum of the per-machine and per-user journal files under /var/log/journal. Intervals matter as much as bytes, so policy belongs in a configuration drop-in rather than a one-off cleanup. Contents of /etc/systemd/journald.conf.d/50-handbook.conf:

[Journal]
SystemMaxUse=64M
SystemMaxFileSize=8M
MaxRetentionSec=2week

Create the drop-in directory, save the file, and make the daemon pick the settings up:

# install -d -m 755 /etc/systemd/journald.conf.d
# systemctl restart systemd-journald

Both silent. SystemMaxUse= caps total journal storage, and SystemMaxFileSize= splits it into archived segments that can be pruned individually; journald enforces the caps by deleting the oldest archived files as new data arrives. MaxRetentionSec= adds a time bound - records older than two weeks are dropped regardless of size. The same file format as every other systemd drop-in applies: keys under [Journal], merged over /etc/systemd/journald.conf and the vendor defaults.

An immediate trim is done with a vacuum, which removes the oldest archived files until a target is met:

# journalctl --vacuum-size=4M
Vacuuming done, freed 0B of archived journals from /var/log/journal.
Deleted archived journal /var/log/journal/2f296ddb676141cd878f55748e26134a/user-1000@bd46656abd4c44c188cfcbf189b12903-0000000000007737-0006609cacfe9756.journal (2.9M).
Deleted archived journal /var/log/journal/2f296ddb676141cd878f55748e26134a/system@bd46656abd4c44c188cfcbf189b12903-00000000000093ce-0006609d04caa75c.journal (3.1M).
Vacuuming done, freed 6M of archived journals from /var/log/journal/2f296ddb676141cd878f55748e26134a.
Vacuuming done, freed 0B of archived journals from /run/log/journal.

Two archived files - one per-user, one system - were deleted, freeing 6 MB. The result:

# journalctl --disk-usage
Archived and active journals take up 24M in the file system.

--vacuum-time=, the sibling option, trims by age instead of size. Two maintenance commands complete the toolkit: --sync asks journald to flush its write buffers to disk (silent, useful before unmounting or capturing), and --verify checksums the stored files:

# journalctl --sync
# journalctl --verify
PASS: /var/log/journal/2f296ddb676141cd878f55748e26134a/user-1000.journal
PASS: /var/log/journal/2f296ddb676141cd878f55748e26134a/system.journal
PASS: /var/log/journal/2f296ddb676141cd878f55748e26134a/user-1001.journal

Every stored file passed - the listing shows the active files only, because the vacuum just removed the two archived ones. The user-UID.journal names show the other half of the access model: each user's journal records live in their own files, which is what lets a user read their own records without being able to read anyone else's.

8.4.3. Journal Access Controls and Exporting Diagnostic Records

What john saw before the group change is worth capturing precisely, because it is what any unprivileged account sees by default:

$ journalctl -u handbook-heartbeat.service -n 1 --no-pager
-- No entries --
Hint: You are currently not seeing messages from other users and the system.
      Users in groups 'adm', 'systemd-journal' can see all messages.
      Pass -q to turn off this notice.

No records - the query succeeded, and the journal said exactly why the result is empty. Two groups unlock the rest: systemd-journal, granted in the introduction to Section 8.4, and adm, the classic Debian group for log readers. Verify the membership took effect in the new session:

$ id
uid=1001(john) gid=1001(john) groups=1001(john),999(systemd-journal)

The same query now returns the system records:

$ journalctl -u handbook-heartbeat.service -n 20 --no-pager
Nov 19 09:00:32 client1 systemd[1]: handbook-heartbeat.service: Main process exited, code=killed, status=9/KILL
Nov 19 09:00:32 client1 systemd[1]: handbook-heartbeat.service: Failed with result 'signal'.
Nov 19 09:00:34 client1 systemd[1]: handbook-heartbeat.service: Scheduled restart job, restart counter is at 1.
Nov 19 09:00:34 client1 systemd[1]: Starting handbook-heartbeat.service - Handbook heartbeat service...
Nov 19 09:00:35 client1 handbook-heartbeat[65531]: starting pid=65531 interval=5s
Nov 19 09:00:36 client1 systemd[1]: Started handbook-heartbeat.service - Handbook heartbeat service.
Nov 19 09:00:36 client1 handbook-heartbeat[65531]: heartbeat 2026-11-19T09:00:36+0000
Nov 19 09:00:41 client1 handbook-heartbeat[65531]: heartbeat 2026-11-19T09:00:41+0000
Nov 19 09:00:42 client1 systemd[1]: Stopping handbook-heartbeat.service - Handbook heartbeat service...

Selected output; eleven further records were omitted. For a diagnostic hand-off, export the records rather than pasting a terminal. In john's workspace, dump the unit's records in the export serialization - a binary-safe stream that journalctl can read back:

$ cd ~/debian-handbook/services-logs
$ journalctl -u handbook-heartbeat.service -o export --since "30 min ago" > heartbeat.export

The redirection writes the stream to a file; nothing appears in the terminal. Confirm what landed:

$ wc -l heartbeat.export
4928 heartbeat.export
$ grep -a -m1 "^__CURSOR=" heartbeat.export
__CURSOR=s=bd46656abd4c44c188cfcbf189b12903;i=9872;b=b1a896bcffd84283bb0806857c472fbb;m=2aa042a75;t=6609d191a72cd;x=b7dd87452d925a72
$ grep -a -m1 "^MESSAGE=" heartbeat.export
MESSAGE=Starting handbook-heartbeat.service - Handbook heartbeat service...

The file interleaves FIELD=value lines with binary framing - the -a flag tells grep to treat it as text anyway. For a human-readable bundle, compress a formatted listing instead:

$ journalctl -u handbook-heartbeat.service -o short-precise --since "30 min ago" | gzip > heartbeat-journal.txt.gz

Another silent redirection; the pipeline sends the formatted listing into gzip, which writes the compressed stream. Check the result:

$ gzip -l heartbeat-journal.txt.gz
         compressed        uncompressed  ratio uncompressed_name
               1843               14238  87.2% heartbeat-journal.txt

Fourteen kilobytes of text compressed to under two. Both artifacts stay in the workspace until deleted; nothing outside ~/debian-handbook was touched.