ubuntu, updated

journalctl extras

Beyond the basics: persistent logs, previous boots, output formats, field filters, size limits, and writing your own log entries.

The basics (-u, -f, -p, --since) are on Searching logs. This page has the rest.

Logs from previous boots

WhatCommand
List all boots in the journaljournalctl --list-boots
Previous bootjournalctl -b -1
Two boots agojournalctl -b -2
The end of the previous boot (why did it crash or reboot?)journalctl -b -1 -e
Who triggered a reboot?journalctl -b -1 -g "reboot|shutdown|power"

On Ubuntu the journal is persistent (kept in /var/log/journal/), so old boots are available. If --list-boots only shows one, create the folder: sudo mkdir -p /var/log/journal && sudo systemctl restart systemd-journald.

Filter by field

Every journal entry has fields. Filter on any of them:

WhatCommand
By programjournalctl _COMM=sshd
By executable pathjournalctl /usr/sbin/cron
By PIDjournalctl _PID=1234
By userjournalctl _UID=1000
By syslog tag (logger -t)journalctl -t backup
Kernel onlyjournalctl -k (= _TRANSPORT=kernel)
Several values = ORjournalctl -u nginx -u php8.3-fpm
Show all values a field hasjournalctl -F _SYSTEMD_UNIT
Explain messages (where available)journalctl -xe

Output formats

FormatCommandUse
Short with full timestampsjournalctl -o short-isocomparing with other logs
Microsecondsjournalctl -o short-precisetiming problems
Message onlyjournalctl -o catgrepping, counting
All fields of each entryjournalctl -o verbosefinding fields to filter on
JSON (one per line)journalctl -o jsonwith jq
No pager, no truncated linesjournalctl --no-pagerscripts, copy-paste
UTC timesjournalctl --utc
# Units that logged errors today, counted
journalctl -p err --since today -o json | jq -r '._SYSTEMD_UNIT // "kernel"' | sort | uniq -c | sort -rn

Size and retention

WhatCommand
How much space is used?journalctl --disk-usage
Keep max. 500 MB nowsudo journalctl --vacuum-size=500M
Delete entries older than 30 dayssudo journalctl --vacuum-time=30d
Check the journal filesjournalctl --verify

Permanent limits in a drop-in file:

[Journal]
SystemMaxUse=1G          # max. total size on disk
MaxRetentionSec=90day    # delete older entries

sudo systemctl restart systemd-journald applies it.

Write your own entries

From scripts and cronjobs:

logger -t backup "Backup finished, 2.3 GB"           # info
logger -t backup -p user.err "Backup FAILED"         # as error
journalctl -t backup --since today                   # read them back

Services started by systemd don’t need this: everything they print goes to the journal automatically.

Send logs elsewhere

To collect logs centrally, rsyslog (installed) can forward everything to a syslog server:

*.* @@logs.example.com:514

@@ = TCP, @ = UDP. Then sudo systemctl restart rsyslog.

§ home
~/knubertus/home/
1,1 Top