Sub topic 3
The systemd Journal
This chapter assumes that you can use the Linux command line, edit files with sudo, use basic systemctl commands, and understand the /var/log directory.
The systemd journal is a structured log store managed by systemd-journald. It collects messages from several sources:
- The Linux kernel.
- Services started by systemd.
- Programs that write to the system logging interface.
- Standard output and standard error from many systemd services.
- The
loggercommand and applications using the syslog interface.
Unlike a plain text log file, the journal stores fields with each message. A record can include the message text, timestamp, hostname, process ID, service name, user ID, and systemd unit. The journal is indexed, so you can filter by these fields without scanning every line manually.
Reading journal entries
Use journalctl to read the journal. Without options, it displays all entries available to your user. Reading the complete journal usually requires sudo.
sudo journalctlUseful filters include:
# Messages from the current boot
sudo journalctl -b
# Messages from the previous boot
sudo journalctl -b -1
# Messages for one systemd unit
sudo journalctl -u ssh.service
# Follow new messages as they arrive
sudo journalctl -f
# Show messages with warning priority or higher
sudo journalctl -p warning
# Show messages from the last hour
sudo journalctl --since "1 hour ago"The -u option filters by systemd unit. This is usually more reliable than searching for a service name in unstructured text. The -b option selects a boot. A boot number is relative, so -b -1 means the boot before the current one.
You can combine filters. For example, this command follows warning and more severe messages from the web service:
sudo journalctl -u nginx.service -p warning -fPress Ctrl+C to stop following the journal.
Journal storage and permissions
The journal uses binary files rather than ordinary text files. On systems with persistent storage, these files normally live below /var/log/journal. On systems using volatile storage, they live below /run/log/journal and may disappear when the system reboots.
Whether old boots are available therefore depends on both storage configuration and retention limits. Check the journal's disk usage with:
sudo journalctl --disk-usageAccess is also controlled. The root user can read all entries. Members of groups such as systemd-journal may receive broader access, depending on the distribution. Avoid granting this access casually because logs can contain usernames, request data, and other sensitive information.
Relating journald to other logging tools
A service's messages may appear in the journal even when the service does not write a file. A separate syslog daemon, such as rsyslog, can read messages from journald and write them to traditional files or forward them to another server. This creates a pipeline rather than two unrelated copies.
Use journalctl -u service-name first for systemd-managed services. Use file searches when the application has its own log path, and check the rsyslog configuration when messages must be forwarded centrally.
Exercise
Which command shows messages from the previous system boot for the ssh service?
Show answer
sudo journalctl -b -1 -u ssh.service