Table of Contents
- What is the systemd Journal?
- How the systemd Journal Works
- Key Features of the systemd Journal
- Basic
journalctlCommands - Advanced
journalctlUsage - Troubleshooting with the Journal
- Persistent vs. Volatile Storage
- Configuring the Journal
- Security Considerations
- Conclusion
- References
What is the systemd Journal?
The systemd journal is a logging subsystem integrated with systemd that captures and stores log data from multiple sources, including:
- The Linux kernel (via
/dev/kmsg). - Systemd services (stdout/stderr of processes managed by systemd).
- Traditional syslog daemons (e.g.,
rsyslog,syslog-ng). - User-space applications (via
sd_journal_send()API orloggercommand). - Audit events (via
auditd).
Unlike traditional text-based logs (e.g., /var/log/syslog), the journal stores logs in a binary format optimized for efficiency, indexing, and fast querying. It also enriches logs with structured metadata (e.g., timestamps, process IDs, unit names, priorities), making it easier to filter and analyze logs programmatically.
How the systemd Journal Works
Architecture
The journal is managed by the systemd-journald daemon, which runs as a system service (systemd-journald.service). This daemon:
- Listens for log entries from various sources (kernel, services, syslog, etc.).
- Enriches logs with metadata (e.g.,
_PID,_SYSTEMD_UNIT,PRIORITY). - Stores logs in binary files (journal files) on disk or in memory.
- Provides an API for querying logs via
journalctl(the primary user interface).
Data Storage: Binary vs. Text Logs
Traditional logs are stored as plain text in files like /var/log/messages or /var/log/auth.log. The journal, by contrast, uses a binary format with the following benefits:
- Compression: Logs are compressed to save disk space.
- Indexing: Metadata (e.g., timestamps, unit names) is indexed for fast filtering.
- Integrity: Checksums ensure log integrity (prevents tampering).
- Structured Data: Logs include key-value pairs (e.g.,
USER=root,COMMAND=nginx), enabling precise queries.
Journal files are stored in two locations:
- Volatile Storage:
/run/log/journal(in-memory, lost on reboot). - Persistent Storage:
/var/log/journal(disk-based, survives reboots).
Structured Logging
Each journal entry is a collection of key-value fields, where keys are prefixed with underscores (e.g., _SYSTEMD_UNIT=sshd.service) or are standard syslog fields (e.g., PRIORITY=3 for errors). This structure allows you to filter logs by specific attributes (e.g., “show all logs from the nginx service with priority err”).
Key Features of the systemd Journal
- Centralized Logging: Aggregates logs from kernel, services, and applications into a single interface.
- Structured Metadata: Logs include rich context (e.g.,
_PID,_HOSTNAME,_SOURCE_REALTIME_TIMESTAMP). - Fast Querying: Indexed metadata enables filtering by unit, time, priority, or custom fields.
- Real-Time Monitoring: Stream logs live with
journalctl -f(liketail -f). - Persistence: Logs can survive reboots (with persistent storage enabled).
- Security: Supports log sealing (prevents tampering) and access control via Unix groups.
Basic journalctl Commands
journalctl is the command-line tool for querying the journal. Below are essential commands to get started:
1. View All Logs
By default, journalctl displays all logs (newest last), paged with less:
journalctl
2. Filter by Systemd Unit
View logs for a specific systemd unit (e.g., sshd.service):
journalctl -u sshd.service
Add -b to limit logs to the current boot:
journalctl -u sshd.service -b
3. Filter by Time
Use --since and --until to filter logs by time (supports relative or absolute timestamps):
# Logs from the last hour
journalctl --since "1 hour ago"
# Logs between 2 PM and 3 PM on May 20, 2024
journalctl --since "2024-05-20 14:00" --until "2024-05-20 15:00"
4. Filter by Priority
Logs are assigned priorities (0 = emergency, 7 = debug). Use -p to filter by priority:
# Show only errors (priority 3) and higher
journalctl -p err
# Priorities: emerg (0), alert (1), crit (2), err (3), warning (4), notice (5), info (6), debug (7)
5. Follow Live Logs
Stream new logs in real time (like tail -f):
journalctl -f
6. Change Output Format
Use -o to customize output format (e.g., short, verbose, json):
# Show logs with full metadata
journalctl -o verbose
# Output in JSON for parsing (useful for scripts)
journalctl -o json
Advanced journalctl Usage
Filter by PID, UID, or GID
Filter logs by process ID (_PID), user ID (_UID), or group ID (_GID):
# Logs from PID 1234
journalctl _PID=1234
# Logs from user 1000 (replace with your UID)
journalctl _UID=1000
Search for Patterns
Use --grep to filter logs by a keyword or regex pattern:
# Find logs containing "failed"
journalctl --grep "failed"
# Case-insensitive search
journalctl --grep "FAILED" -i
Check Journal Size and Clean Up
View disk usage of journal files:
journalctl --disk-usage
Trim logs to free space (e.g., keep only 1GB or logs from the last 7 days):
# Keep only 1GB of logs
journalctl --vacuum-size=1G
# Keep logs from the last 7 days
journalctl --vacuum-time=7d
Combine Filters
Chain filters to narrow results (e.g., logs from nginx unit, with priority err, in the last hour):
journalctl -u nginx.service -p err --since "1 hour ago"
Troubleshooting with the Journal
The journal is a powerful tool for diagnosing system and application issues. Below are common troubleshooting scenarios:
Service Failures
If a service (e.g., nginx) fails to start, use journalctl -u <unit> to inspect its logs:
# Check why nginx failed to start on the current boot
journalctl -u nginx.service -b -p err
Example output might reveal: “Address already in use” (port 80 is occupied) or “invalid configuration” (syntax error in nginx.conf).
Boot Issues
To diagnose boot problems (e.g., system hangs or crashes), check logs from the current or previous boot:
# Logs from the current boot (critical errors only)
journalctl -b -p emerg
# Logs from the previous boot (use -b -2 for the boot before last)
journalctl -b -1
Network Problems
Debug network issues (e.g., ssh connection failures) by filtering logs for network-related units:
# Check SSH server logs for failed login attempts
journalctl -u sshd.service --grep "Failed password"
# Inspect network manager logs
journalctl -u NetworkManager.service --since "10 minutes ago"
Application Errors
For custom applications, ensure they log to stdout/stderr (systemd captures these by default) or use the sd_journal_send() API. Query their logs with _COMM=<app-name> (command name):
# Logs from an application named "myapp"
journalctl _COMM=myapp
Persistent vs. Volatile Storage
By default, the journal uses volatile storage (/run/log/journal), which resides in memory and is lost on reboot. To retain logs across reboots, enable persistent storage (/var/log/journal):
Enabling Persistent Storage
-
Create the journal directory and set permissions:
sudo mkdir -p /var/log/journal sudo chown root:systemd-journal /var/log/journal sudo chmod 2755 /var/log/journal # Set SGID to inherit group ownership -
Restart
systemd-journaldto apply changes:sudo systemctl restart systemd-journald.service
Logs will now survive reboots.
Configuring the Journal
The journal’s behavior is controlled by /etc/systemd/journald.conf (or drop-in files in /etc/systemd/journald.conf.d/). Key settings in the [Journal] section include:
| Setting | Purpose |
|---|---|
Storage= | volatile (default, /run), persistent (/var/log), or auto (use persistent if /var/log/journal exists). |
Compress= | yes (default) to compress logs. |
MaxRetentionSec= | Max time to retain logs (e.g., 30d for 30 days). |
SystemMaxUse= | Max disk space for system logs (e.g., 5G). |
ForwardToSyslog= | yes to forward logs to traditional syslog (for compatibility). |
Seal= | yes to enable log sealing (cryptographically hash logs to prevent tampering). |
Example journald.conf snippet:
[Journal]
Storage=persistent
Compress=yes
MaxRetentionSec=30d
SystemMaxUse=5G
Seal=yes
After editing, restart systemd-journald:
sudo systemctl restart systemd-journald.service
Security Considerations
-
Access Control: Journal files are owned by
root:systemd-journal. Users in thesystemd-journalgroup can read logs withoutsudo. Add a user to this group with:sudo usermod -aG systemd-journal <username> -
Log Sealing: Enable
Seal=yesinjournald.confto cryptographically sign logs. This prevents tampering by generating hashes for log entries, which can be verified later. -
Sensitive Data: Avoid logging passwords or secrets. Use application-level filtering (e.g.,
LogFilter=yesin systemd service files) to redact sensitive data.
Conclusion
The systemd journal revolutionizes Linux logging with its structured, centralized, and efficient design. By mastering journalctl and understanding journal configuration, you can quickly diagnose system issues, monitor applications, and maintain log integrity. Whether you’re a system administrator or developer, the journal is an essential tool for managing Linux systems.