What it is
A tool for querying and displaying log data from the systemd journal. Use it to inspect system logs, service logs, and kernel messages.
Installation
journalctl is part of systemd and is installed by default on most Linux distributions that use systemd.
Core Concepts
- Journal: The central log storage managed by systemd. It’s a binary, indexed database, not plain text files.
- Units: Refers to systemd units, such as services (
.service), timers (.timer), sockets (.socket), etc. You can filter logs by unit. - Fields: Log entries have structured fields (e.g.,
_PID,_COMM,SYSLOG_IDENTIFIER,MESSAGE).journalctlallows filtering by these fields.
Commands / Usage
Viewing Recent Logs
-
View all logs:
journalctlDisplays all log entries from the current boot, newest first.
-
View logs from the current boot:
journalctl -bShows logs specifically from the current boot session.
-
View logs from the previous boot:
journalctl -b -1Displays logs from the immediately preceding boot session. Use
-b -2for the boot before that, and so on. -
View logs in real-time (like
tail -f):journalctl -fFollows the log output as new entries are added.
-
View logs from a specific boot, follow new entries:
journalctl -b -fCombines viewing logs from the current boot with following new entries.
Filtering Logs
-
Filter by service name:
journalctl -u nginx.serviceShows logs only for the
nginx.serviceunit. -
Filter by multiple service names:
journalctl -u sshd.service -u cron.serviceDisplays logs for both
sshd.serviceandcron.service. -
Filter by kernel messages:
journalctl -kShows only kernel log messages.
-
Filter by process ID (PID):
journalctl _PID=1234Displays logs generated by the process with PID
1234. -
Filter by executable name:
journalctl _COMM=bashShows logs where the executable name is
bash. -
Filter by systemd unit and executable name:
journalctl -u myapp.service _COMM=myappLogs for
myapp.servicewhere the executable is namedmyapp. -
Filter by message content (case-sensitive):
journalctl -g "error"Shows log entries containing the exact string "error".
-
Filter by message content (case-insensitive):
journalctl -G "error"Shows log entries containing "error", ignoring case.
-
Filter by syslog identifier:
journalctl SYSLOG_IDENTIFIER=systemdLogs from the
systemdprocess.
Time-Based Filtering
-
Logs since a specific date and time:
journalctl --since "2023-10-27 10:00:00"Shows logs generated after the specified timestamp.
-
Logs since a relative time:
journalctl --since "1 hour ago"Displays logs from the last hour.
-
Logs until a specific date and time:
journalctl --until "2023-10-27 11:00:00"Shows logs generated before the specified timestamp.
-
Logs between two times:
journalctl --since "2023-10-27 10:00:00" --until "2023-10-27 11:00:00"Displays logs within a specific time range.
-
Logs from today:
journalctl --since today -
Logs from yesterday:
journalctl --since yesterday --until today
Output Formatting
-
Show logs in JSON format:
journalctl -o jsonOutputs each log entry as a JSON object. Useful for programmatic parsing.
-
Show logs in verbose format:
journalctl -o verboseDisplays all available fields for each log entry.
-
Show logs with executable name and PID:
journalctl -o verbose _COMM=sshdCombines verbose output with filtering for
sshd. -
Show logs with only the message field:
journalctl -o catPrints only the message part of the logs, stripping metadata.
-
Show logs with short format (default):
journalctl -o shortThe default, compact format.
Paging and Searching
-
Scroll through logs with
less(default pager):journalctltypically pipes its output tolessby default. Use standardlesscommands:jor arrow down: scroll down one linekor arrow up: scroll up one linespace: scroll down one pageb: scroll up one page/search_term: search forward forsearch_term?search_term: search backward forsearch_termn: go to next matchN: go to previous matchq: quit
-
Disable the pager:
journalctl --no-pagerOutputs directly to standard output without using a pager.
Archiving and Rotating Logs
-
View archived logs from previous rotation:
journalctl --file /var/log/journal/persistent/system.journalInspects a specific journal file. The path might vary.
-
Show logs from all persistent journals:
journalctl --directory /var/log/journal/persistentQueries logs stored in the persistent journal directory.
Other Useful Flags
-
Show only new logs since last check:
journalctl -n 100Shows the last 100 log entries.
-
Show logs with specific fields:
journalctl -o verbose _TRANSPORT=kernelShows verbose output for kernel messages.
Common Patterns
-
Check if a service started correctly:
journalctl -u myapp.service -bView logs for
myapp.servicefrom the current boot to diagnose startup issues. -
Find errors from a specific service in the last hour:
journalctl -u apache2.service --since "1 hour ago" -g "error"Searches
apache2.servicelogs for "error" within the last hour. -
See what happened before a crash:
journalctl -b -1 -n 500View the last 500 log lines from the previous boot session to understand the state before a reboot.
-
Troubleshoot a failing service:
journalctl -u myapp.service -fFollow the logs of a service in real-time to see errors as they occur.
-
View kernel messages related to a specific device:
journalctl -k -g "sd[a-z][0-9]"Searches kernel messages for patterns that look like disk device names.
-
Export logs for debugging:
journalctl -u myapp.service --since "2023-10-27 09:00:00" --until "2023-10-27 10:00:00" -o json > myapp_logs.jsonExports logs for
myapp.servicewithin a specific time range in JSON format.
Gotchas
- Permissions: You might need
sudoto view all logs, especially those from other users or system services.sudo journalctl -u some_service.service - Log Rotation: By default,
journalctlshows logs from the current boot. Use-b -1,--since, or--untilto access historical data. Persistent storage needs to be configured. - Filtering is powerful but can be complex: Understanding
journalctl’s field names (like_PID,_COMM,SYSLOG_IDENTIFIER) is key for precise filtering. Usejournalctl -o verboseto see available fields. - Case Sensitivity: The
-g(grep) flag is case-sensitive. Use-Gfor case-insensitive searches. - Units vs. Executables:
-ufilters by systemd unit name (e.g.,nginx.service), while_COMMfilters by the executable name (e.g.,nginx). A single unit might run multiple executables, or an executable might be run outside of a systemd unit. - Empty Output: If
journalctlreturns nothing, it means there are no matching log entries for your query. Double-check your filters, time ranges, and boot selections. journaldConfiguration: The behavior and storage location of logs depend on thejournald.confconfiguration file (usually/etc/systemd/journald.conf). For instance,Storage=volatilemeans logs are lost on reboot unlessStorage=persistentis set.