Inspect logs with journalctl
In the previous steps, you learned about the kernel ring buffer and how to view its contents using dmesg
. While dmesg
is great for kernel messages, modern Linux systems use a more comprehensive logging system called systemd-journald
. The journalctl
command is the primary tool for interacting with the systemd-journald
journal.
systemd-journald
collects log messages from various sources, including the kernel (like the messages you saw with dmesg
), system services, applications, and even standard output and standard error of processes. It stores these logs in a structured, indexed format, making it easier to search and filter messages.
Let's explore the journalctl
command.
Open your terminal if it's not already open.
Type the following command and press Enter:
journalctl
This command will display all the log messages collected by systemd-journald
. Similar to dmesg
, the output can be very long and is typically piped to a pager like less
automatically.
-- Journal begins at Tue 2024-07-23 10:00:00 UTC, ends at Tue 2024-07-23 10:30:00 UTC. --
Jul 23 10:00:01 hostname systemd[1]: Starting Network Manager...
Jul 23 10:00:02 hostname kernel: Linux version 5.15.0-xx-generic (...)
Jul 23 10:00:03 hostname systemd[1]: Started Network Manager.
Jul 23 10:00:04 hostname systemd[1]: Starting OpenSSH server daemon...
...
You can use the arrow keys to scroll and press q
to exit the pager.
journalctl
has many options for filtering logs. Here are a few examples:
To see only kernel messages (similar to dmesg
):
journalctl -k
To see logs from a specific service, for example, the SSH service:
journalctl -u ssh.service
To see logs since a specific time, for example, since "today":
journalctl --since "today"
To see the most recent logs and follow new messages as they arrive (like tail -f
):
journalctl -f
Press Ctrl+C
to exit the journalctl -f
command.
journalctl
is a powerful tool for system administration and troubleshooting. It provides a centralized location for logs and flexible options for viewing and filtering them.
Experiment with some of the journalctl
options in your terminal to see how they affect the output.
Click Continue to complete this lab.