Skip to main content

Log

In Linux, the kernel uses a ring buffer to keep log messages from when the system starts. This buffer has a set size, and when it's full, new messages replace old ones.

The kernel log buffer is important for keeping the system working well and finding issues. User applications have their own ways of logging, which gives a bigger picture of what the whole system is doing.

Also, applications and services made by users can create their own messages. These are usually handled by different logging systems like rsyslog or systemd-journald.

/dev/log

/dev/log is a Unix domain socket that applications use to send logs to a logging daemon such as rsyslog or systemd-journald.

In a Linux system, /dev/log is often a symbolic link to /run/systemd/journal/dev-log

readlink /dev/log
# /run/systemd/journal/dev-log

ls -l /run/systemd/journal/dev-log
# srw-rw-rw- 1 root root 0 Oct 6 18:09 /run/systemd/journal/dev-log
# s stands for socket

dmesg

The dmesg command is used to display the kernel ring buffer. By default it read the messaged from /dev/kmsg, that provides access to the kernel ring buffer.

dmesg
sudo dmesg --level=emerg,alert,crit,err

journalctl

journalctl is a command-line utility for viewing and querying logs from the systemd journal, a system logging service commonly used in modern Linux distributions

sudo systemctl restart systemd-journald.service
CommandDescription
journalctlall Collected Logs
journalctl -kKernel Logs
journalctl -bCurrent Boot Logs (Including kernel logs)
journalctl -rShow Logs in Reverse Order
journalctl -fLive Systemd Logs
journalctl -b -fMonitor Boot Logs
journalctl -b -p errBoot Logs with priority "err" level
journalctl -u sshd.serviceUnit's Logs
journalctl -u sshd.service -xVerbose
journalctl /usr/bin/firefoxShow logs related to the Firefox executable
sudo journalctl _PID=20220View logs associated with PID 20220
journalctl --flushFlush Journal Logs
sudo journalctl --flush --vacuum-time=1sFlush system Logs, Retain Last 1 Second
sudo journalctl --user --flush --vacuum-time=1sFlush user Logs, Retain Last 1 Second

journalctl Configuration

Journald is configured in /etc/systemd/journald.conf. You can change storage location, log size, log rotation, etc.

nano
[Journal]
Storage=persistent
Compress=yes
SystemMaxUse=100M

Log Level

Log Level refers to the severity or importance of the log messages generated by various components of the system

PLevelDescription
0emergSystem is unusable
1alertAction must be taken immediately
2critCritical conditions
3errError conditions
4warningWarning conditions
5noticeNormal but significant condition
6infoInformational messages
7debugDebug-level messages

systemd-cat

systemd-cat is a command-line utility that allows you to send logs to the systemd journal.

echo "Hello World" | systemd-cat -t "Hello"
journalctl -t Hello

echo "Hello World" | systemd-cat
journalctl -f

Syslog

It is a standard for recording events in a computer system.

rsyslog

rsyslog is a free and open source syslog implementation.

lastlog

The lastlog command displays the last login times and information for all users on the system.
It provides details such as the username, port, and timestamp of the last login for each user. It reads the /var/log/lastlog binary file to retrieve this information.

lastlog
Username Port From Latest
root **Never logged in**
nobody **Never logged in**
dbus **Never logged in**
bin **Never logged in**
mlibre pts/2 127.0.0.1 Sun Aug 20 22:39:47 +0330 2023