journalctl command to query and follow logs effectively.
What you’ll learn
- How to enable persistent on-disk journaling.
- How to verify journal files and disk usage.
- Common
journalctlfilters and usage patterns for troubleshooting.
Understanding systemd-journald
systemd-journald stores logs in a binary, indexed format that supports structured fields (e.g.,_PID=, _COMM=, _SYSTEMD_UNIT=). You can query those fields with journalctl to produce focused, high-signal output for debugging and operational tasks.
Enable persistent journal storage
Many distributions default to in-memory journaling (volatile storage). To keep the journal across reboots, setStorage=persistent in /etc/systemd/journald.conf.
Steps:
- If an
/etc/systemd/journald.confdoesn’t already exist, copy the vendor-provided configuration:
- Edit the file and set
Storage=persistent:
- Reload systemd unit files and restart journald:
/etc/systemd/journald.conf, edit that file directly instead of copying from /usr/lib/....
Verify the journal directory and disk usage
WhenStorage=persistent is enabled, journals are stored under /var/log/journal. Verify presence and usage:
List /var/log to confirm a journal directory:
Common journalctl filters and examples
journalctl supports a rich set of filters. Below is a quick reference of the most commonly used options, followed by examples.
Examples (kept in the original sequence for clarity):
- Entries from the current boot:
- Entries for a specific unit (example: sshd):
- Filter by priority (show errors and worse):
- Show only the last N entries (example N=10):
- Follow logs in real time:
Additional tips and configuration for long-term retention
-
Use structured-field matches:
journalctl _PID=1234orjournalctl _COMM=sshd. -
Limit disk usage and retention by configuring
/etc/systemd/journald.conf:SystemMaxUse=— maximum disk space for all system journals.SystemKeepFree=— amount of disk space to keep free for other uses.SystemMaxFileSize=— maximum size for individual journal files.
-
For targeted troubleshooting of services, prefer:
journalctl -u <unit> -n 100(last 100 lines for a unit)journalctl -u <unit> -f(follow unit logs in real time)
For examinations or deeper troubleshooting, use man journalctl to review all flags and structured-field matching. Familiarity with
journalctl options is valuable for both ops and troubleshooting scenarios.Man page excerpt (reference)
Links and references
- journalctl man page
- systemd.journal-fields(7) — structured journal fields
- systemd-journald configuration:
/etc/systemd/journald.conf
journalctl effectively to inspect and follow system logs.