Skip to content

Linux Foundations / Manage a Linux host ยท Step 41 of 50

Lesson: System LoggingSwitch
โ† Linux Foundations

System administration

System Logging

Learn where Linux log messages come from, where they are kept and how to read them.

Logs are the system's diary. Services, the kernel and login tools record what they did and what went wrong. When something breaks, they are usually the first place to look.

Who writes the logs

Most Linux distributions today use systemd, whose logging service is systemd-journald. It collects messages from the kernel, from services' standard output and error, and from the traditional syslog interface. It stores them in a binary journal.

Many systems also run a syslog daemon, commonly rsyslog, which writes plain text files under /var/log. Some distributions install it by default and others rely on the journal alone. Some programs, such as web servers, write their own files.

Reading the journal

$ journalctl -n 3
Jan 05 16:29:01 web01 CRON[4573]: pam_unix(cron:session): session closed for user root
Jan 05 16:30:50 web01 dhclient[700]: XMT: Solicit on eth0, interval 119360ms.
Jan 05 16:31:04 web01 systemd[1]: Started nginx.service.

Each line shows time, host, the program with its process ID, and the message. Useful filters:

  • journalctl -u nginx shows one unit
  • journalctl -b shows the current boot
  • journalctl -f follows new messages
  • journalctl -p err shows errors and worse
  • journalctl --since "1 hour ago" limits by time

Without root or membership of a suitable group (such as adm or systemd-journal), you may see only your own messages. Use sudo if output seems thin.

Is the journal kept?

Whether the journal survives a reboot depends on configuration. With the common Storage=auto setting, it is saved to disk if /var/log/journal exists, and otherwise kept in memory under /run/log/journal.

List the boots the journal knows about:

$ journalctl --list-boots

Text files

$ ls /var/log

You can read text logs with less, follow them with tail -f and search them with grep. Names differ between distributions, and later lessons cover the main ones.

Try it

Run journalctl -b -p warning and read a few lines. Then run ls -l /var/log and see which files changed recently. Check whether /var/log/journal exists on your system.

Check your understanding

System Logging

All 4 sections ยท no timer

Unlimited retries

Questions from every section are combined into one assessment. Answer them all correctly to complete the article.

Your completion saves in this browser. Sign in to sync it to your account.