Linux journalctl command
What is journalctl?
journalctl is a powerful tool in Linux for querying and displaying systemd logs. As part of the systemd ecosystem, it provides centralized log management, replacing the traditional syslog service.
Core features
- Binary log storage: Logs are stored in binary format to improve retrieval efficiency
- Structured logging: Supports additional metadata and structured log fields
- Real-time monitoring: Can track log changes in real time
- Multiple filtering methods: Supports filtering by time, service, priority, and other criteria
Basic Syntax
The basic command format of journalctl is as follows:
journalctl [选项] [匹配条件...]
Overview of common options
| Option | Description |
|---|---|
-b |
Display logs from the current boot |
-f |
Follow logs (similar to tail -f) |
-k |
Display only kernel messages |
-u |
Display logs for a specified unit |
-n |
Display the most recent n log entries |
--since |
Display logs after a specified time |
--until |
Display logs before a specified time |
Common operation examples
1. View the complete system log
Example
journalctl
2. View logs from the current boot
Example
journalctl
-b
3. Monitor new logs in real time
Example
journalctl
-f
4. View logs for a specific service
Example
journalctl
-u
nginx.service
5. Query by time range
Example
journalctl
--since
"2023-01-01 00:00:00"
--until
"2023-01-02 12:00:00"
6. View logs at the error level
Example
journalctl
-p
err
Log priority filtering
journalctl supports filtering by log priority; the priorities are defined as follows:
| Precedence | Value | Description |
|---|---|---|
| emerg | 0 | Emergency |
| alert | 1 | Requires immediate action |
| crit | 2 | Critical error |
| err | 3 | General error |
| warning | 4 | Warning message |
| notice | 5 | Conditions that need attention |
| info | 6 | General information |
| debug | 7 | Debug information |
Usage example:
Example
Display logs at the error level and above
journalctl
-p
err
Display logs at the warning level and above
journalctl
-p
warning
Advanced usage
1. Show the disk space used by logs
Example
journalctl
--disk-usage
2. Clean up old logs
Example
Keep logs from the last 2 days
journalctl
--vacuum-time
=2d
Limit logs to a maximum of 500MB
journalctl
--vacuum-size
=500M
3. Output in JSON format
Example
journalctl
-o
json
4. Display complete field information
Example
journalctl
-o
verbose
5. Filter by a specific field
Example
Display logs for a specific process ID
journalctl
_PID
=
1234
Display logs for a specific user
journalctl
_UID
=
1000
Practical tips
1. Combined queries
Example
Query error logs for the nginx service since yesterday
journalctl
-u
nginx.service
--since
yesterday
-p
err
2. Page through the output
Example
journalctl
|
less
3. Export logs to a file
Example
journalctl
--since
"2023-01-01"
journal.log
4. View kernel ring buffer messages
Example
journalctl
-k
5. View system boot process logs
Example
journalctl
-b0
|
grep
"Starting"
Common Problem Solutions
Problem 1: Incomplete log display
Solution:
Example
Increase the output line limit
journalctl
--no-pager
Problem 2: How to view rotated old logs?
Solution:
Example
View all logs (including archived ones)
journalctl
-a
Problem 3: How to view logs at a specific point in time?
Solution:
Example
Time queries precise to the second
journalctl
--since
"2023-01-01 12:00:00"
--until
"2023-01-01 12:05:00"
Summary flowchart

Linux Command Reference