📖 Scenario: You are a system administrator who needs to check system logs to troubleshoot issues on a Linux server that uses systemd. You will learn how to use the journalctl command to view logs efficiently.
🎯 Goal: Build a simple script that uses journalctl commands to filter and display systemd logs based on time and priority.
📋 What You'll Learn
Use journalctl to view logs
Filter logs by time range
Filter logs by priority level
Display the filtered logs
💡 Why This Matters
🌍 Real World
System administrators often need to check system logs to find errors or warnings. Using journalctl with filters helps quickly find relevant logs.
💼 Career
Knowing how to use journalctl is essential for Linux system administrators and DevOps engineers to troubleshoot and maintain servers.
Progress0 / 4 steps
1
Create a variable with the systemd log file path
Create a variable called log_file and set it to /var/log/journal which is the default directory for systemd logs.
Linux CLI
Hint
The systemd journal logs are stored in /var/log/journal by default.
2
Set a time filter for logs
Create a variable called time_filter and set it to --since "2024-01-01 00:00:00" to filter logs from January 1, 2024 onwards.
Linux CLI
Hint
The --since option in journalctl filters logs starting from a specific date and time.
3
Add a priority filter for error logs
Create a variable called priority_filter and set it to --priority=err to filter only error level logs.
Linux CLI
Hint
The --priority option filters logs by severity. err means error level logs.
4
Display filtered logs using journalctl
Use the print function to display the command journalctl {time_filter} {priority_filter} as a string, showing the full command that filters logs by time and priority.
Linux CLI
Hint
Use an f-string to combine the filters into the journalctl command string.
Practice
(1/5)
1. What is the primary purpose of the journalctl command in Linux?
easy
A. To edit configuration files for systemd services
B. To start and stop systemd services
C. To view system and service logs managed by systemd
D. To display disk usage statistics
Solution
Step 1: Understand the role of journalctl
The journalctl command is used to access logs collected by systemd's journal service.
Step 2: Compare options with command purpose
Editing configuration files, starting/stopping services, and displaying disk usage are handled by other commands, unrelated to viewing logs.
Final Answer:
To view system and service logs managed by systemd -> Option C
Confusing journalctl with systemctl for service control
Thinking journalctl edits files
Assuming journalctl shows disk info
2. Which of the following is the correct syntax to show the last 10 lines of system logs using journalctl?
easy
A. journalctl -n 10
B. journalctl --last 10
C. journalctl -l 10
D. journalctl -t 10
Solution
Step 1: Recall journalctl option for number of lines
The -n option followed by a number shows that many recent log lines.
Step 2: Check other options for correctness
The other options are not valid journalctl options for limiting output lines.
Final Answer:
journalctl -n 10 -> Option A
Quick Check:
-n = number of lines [OK]
Hint: Use -n to limit lines shown quickly [OK]
Common Mistakes:
Using --last instead of -n
Confusing -l (line wrap) with line count
Using -t which filters by syslog identifier
3. What will be the output of the command journalctl -u sshd.service -n 3?
medium
A. Shows the last 3 log entries related to the sshd service
B. Shows all logs except those from sshd service
C. Shows the first 3 lines of the system log
D. Shows logs from all services except sshd
Solution
Step 1: Understand the -u option
The -u sshd.service option filters logs to only those from the sshd service.
Step 2: Understand the -n 3 option
The -n 3 option limits output to the last 3 log entries.
Final Answer:
Shows the last 3 log entries related to the sshd service -> Option A
Quick Check:
-u sshd + -n 3 = last 3 sshd logs [OK]
Hint: -u filters by service, -n limits lines [OK]
Common Mistakes:
Thinking -u excludes the service logs
Confusing -n with showing first lines
Assuming it shows all logs
4. You run journalctl -u nginx.service --since "2024-01-01" --until "2024-01-02" but get no output, even though you know nginx was active. What is the likely problem?
medium
A. The date format is incorrect; journalctl requires YYYY-MM-DD HH:MM:SS
B. The service name is wrong; it should be nginx without .service
C. The journal is empty because logging is disabled
D. The system clock is set to a different timezone causing no matching logs
Solution
Step 1: Check date format validity
The date format "YYYY-MM-DD" is valid for journalctl; it does not require HH:MM:SS.
Step 2: Consider timezone impact
If the system clock or journal timestamps use a different timezone, the specified range may not match any logs, causing no output.
Step 3: Verify service name and logging
The service name with .service is correct, and nginx is usually logged to the journal, making incorrect service name or disabled logging unlikely.
Final Answer:
The system clock is set to a different timezone causing no matching logs -> Option D
Quick Check:
Timezone mismatch = no logs in range [OK]
Hint: Check timezone if date filters show no logs [OK]
Common Mistakes:
Assuming date format is wrong
Removing .service from unit name
Ignoring system clock timezone
5. You want to monitor live logs from the docker.service as they happen. Which command should you use?
hard
A. journalctl -u docker.service --since now
B. journalctl -u docker.service -f
C. journalctl -u docker.service -n 0
D. journalctl -u docker.service --no-pager
Solution
Step 1: Identify option for live log streaming
The -f option in journalctl streams new log entries live, similar to tail -f.
Step 2: Evaluate other options
journalctl -u docker.service --since now shows logs since now but does not follow live updates. journalctl -u docker.service -n 0 shows zero lines, so no output. journalctl -u docker.service --no-pager disables paging but does not stream live.
Final Answer:
journalctl -u docker.service -f -> Option B
Quick Check:
-f = follow live logs [OK]
Hint: Use -f to follow logs live like tail -f [OK]
Common Mistakes:
Using --since now without -f for live updates
Using -n 0 which shows no lines
Confusing --no-pager with live streaming
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.