Skip to content

Logs and journald

Exercise logs-and-journald-annex.pdf · PDF

A user at Tramuntana Apps says: "The server went down last night." You were asleep. Nobody saw anything. How do you find out what happened — and at what minute?

You cannot ask the machine what it remembers. But it wrote everything down while it happened. In the last session you killed your heartbeat service with kill -9. Do you know the exact second? You do not. Your VM does.

What you will learn

  • Explain what a log is and read one line: when, which machine, who, what
  • Query the systemd journal with journalctl: last lines, live, one unit, one boot, a time window, a priority
  • Write your own test message into the journal with logger
  • Answer "what failed, and when?" with a method instead of scrolling
  • Why there is no /var/log/syslog on Debian 13, even though half the Internet says there is
  • Find events in the Windows Event Viewer and with Get-WinEvent by log, level and Event ID

The machine keeps a diary

A log is a list of short lines, each with the time, written by the system while things happen — and never corrected afterwards.

The word comes from the sea. Sailors measured a ship's speed by throwing a wooden log on a rope over the side and counting the knots that ran out. They wrote the result in the log-book, hour after hour. The book kept the name.

The ship's log-book (we keep it all page)

Think of the system as a ship. Every program on board writes in the log-book: the time, who is writing and what happened. When something goes wrong, the captain does not ask the crew to remember. She opens the book at the right page.

Anatomy of a line

Oct 05 20:53:39 deb-lab heartbeat.sh[4088]: heartbeat from deb-lab: load 6.30
Piece Example Question it answers
Timestamp Oct 05 20:53:39 When?
Host deb-lab On which machine?
Program and PID heartbeat.sh[4088] Who wrote it? (the PID from Processes)
Message heartbeat from ... What happened?

Syslog and the eight levels of bad news

In the early 1980s Eric Allman, at the University of California, Berkeley, was writing the mail server sendmail and needed somewhere to put its messages. He wrote a small helper: syslog. Other programs started using it, and it became the way Unix logs — with no official standard for about twenty years. It was described in RFC 3164 in 2001 and standardised in RFC 5424 in 2009.

Syslog gave every line a severity (also called priority): how bad is it? Those eight levels are still what you filter by today.

No. Name Meaning No. Name Meaning
0 emerg System unusable 4 warning Something is odd
1 alert Act immediately 5 notice Normal but worth noting
2 crit Critical 6 info Ordinary information
3 err An error 7 debug Detail for developers

Careful: lower number = worse

It feels backwards, but 0 is the worst ("the system is unusable") and 7 is the least important. Most lines on a normal day are level 6.

The journal: journald

The journal is the log-book of a systemd machine, kept by the service systemd-journald.

It collects three things in one place:

flowchart LR
    subgraph SRC ["Who writes"]
        direction TB
        K["<b>Kernel</b><br>hardware, drivers"]
        S["<b>Services</b><br>everything they print<br>(your heartbeat's echo)"]
        P["<b>Programs</b><br>sending to syslog<br>(sudo, logger)"]
    end
    SRC -->|"lines"| J["<b>systemd-journald</b><br>the ship's clerk"]
    J -->|"stores, indexed"| F["<b>/var/log/journal/</b><br>binary files"]
    F -->|"you ask"| Q["<b>journalctl</b><br>the reader"]

The journal is stored in a binary, indexed format, so cat shows rubbish. You do not open it — you ask it, with journalctl.

Careful: there is no /var/log/syslog on Debian 13

Many tutorials tell you to read /var/log/syslog. On Debian 13 that file does not exist: since Debian 12, the rsyslog package that wrote it is no longer installed by default. The journal holds the same lines. (Ubuntu still writes the file, which is why so many tutorials assume it.)

journalctl basics

journalctl opens a pager: arrows and Page Down to move, G (capital) to jump to the end, q to leave.

Who may read the journal?

$ journalctl -n 5
Hint: You are currently not seeing messages from other users and the system.
      Users in groups 'adm', 'systemd-journal' can see all messages.

A normal user sees only their own messages. The log of the system can reveal who logged in and what they ran, so it is protected — the permissions of the previous sessions guarding the evidence. From here on, use sudo journalctl.

The options you will use every day

You want Command
The last 5 lines sudo journalctl -n 5
Follow new lines live (like tail -f) sudo journalctl -f
Only one service sudo journalctl -u heartbeat
Only this boot sudo journalctl -b
The boot before this one sudo journalctl -b -1
The list of boots sudo journalctl --list-boots
A time window sudo journalctl --since "12:30" --until "12:35"
The last 10 minutes sudo journalctl --since "10 min ago"
Level err and worse sudo journalctl -p err
One tag (program name) sudo journalctl -t sudo
Lines containing a word sudo journalctl -g "Permission denied"
How much space it takes sudo journalctl --disk-usage

Watch the details. A time with a space needs quotes: --since "10 min ago". -u takes the unit name (heartbeat), not the script name (heartbeat.sh). Long options have two dashes: --since, never -since.

Write in the log-book yourself

logger sends a line to the journal, as any program would. Open two terminals. In the first, sudo journalctl -f. In the second:

logger -t tramuntana "hello from marta"

The line appears in the first terminal without you touching it:

Oct 05 21:10:36 deb-lab tramuntana[5176]: hello from marta

The -t gave the line its tag, the place where a service would put its name. You can also choose the priority:

logger -p user.err -t tramuntana "disk almost full (test)"
sudo journalctl -t tramuntana -p err

The second command shows the "disk almost full" line, but not the "hello": that one was level 6 (info), and -p err shows only level 3 and worse (0–3).

A method: when, who, how bad

A VM that has been on for a week holds tens of thousands of lines. Scrolling through them is not a plan. Three questions cut the pile:

Question Option Example
When did it happen? --since, --until, -b --since "09:00" --until "09:15"
Who wrote it (which unit or program)? -u, -t -u heartbeat
How bad is it? -p -p warning
A word you expect -g (or a pipe to grep) -g "Permission denied"

They combine. Here is the line of your kill -9 from the last session, to the second:

$ sudo journalctl -u heartbeat --since yesterday -g "killed|restart"
Oct 05 20:53:50 deb-lab systemd[1]: heartbeat.service: Main process exited, code=killed, status=9/KILL
Oct 05 20:53:50 deb-lab systemd[1]: heartbeat.service: Scheduled restart job, restart counter is at 1.

The exact time; the signal (9, SIGKILL); and who wrote it: systemd[1], not the script — the script was dead, it could not write anything.

Careful: the cause is usually above the error

Once you find the error line, widen the window by a minute on each side and read what happened just before. The error line says "it broke". The cause is often one or two lines higher.

Two more hunting tips. If you search with -g and a capital letter, the search becomes case-sensitive — all lower case matches both. And if the event happened before the last reboot, -b (this boot only) will not find it: use --since yesterday.

Who used sudo?

Every sudo — failed or successful — is written down. Type a wrong password three times, then ask:

$ sudo journalctl -t sudo --since "5 min ago"
sudo[4484]: pam_unix(sudo:auth): authentication failure; logname=... uid=1000 euid=0 tty=... ruser=marta rhost=  user=marta
sudo[4484]:    marta : 3 incorrect password attempts ; PWD=/home/marta ; USER=root ; COMMAND=/usr/bin/ls /root

Who, from which folder, as which user, and the exact command. That is why only root and the groups adm and systemd-journal may read the journal.

What is still in /var/log

Some programs keep their own text files next to the journal. Those you read with less and grep, as in Unit 4:

File Content
/var/log/apt/history.log Every apt install, remove and upgrade: date, command line, packages
/var/log/dpkg.log The same, package by package, with the second
/var/log/journal/ The journal itself (binary: use journalctl)
grep -A1 "Start-Date" /var/log/apt/history.log | tail -4

This shows the last two apt commands, each under its date.

Careful: logs are not kept for ever

systemd keeps the journal under a size limit by deleting the oldest entries, so logs never fill the disk. The price: a log from six months ago may be gone. Windows does the same with its event logs.

Windows: Event Viewer

Windows keeps its diary too. Press Win+R and type eventvwr.msc, then open Windows Logs on the left.

Linux Windows
The diary The journal (systemd-journald) The event logs
Sections Filter by unit: -u System (Windows and its services), Application (programs), Security (logons, audits)
How bad Priority 0–7 Level: Critical, Error, Warning, Information
Who wrote it Program name and PID Source (provider)
Kind of event The text of the message Event ID: one number per kind of event
Tool journalctl Event Viewer, Get-WinEvent

The new idea is the Event ID: every kind of event has a number, so you search for the number instead of for words. Two worth knowing by heart, in the Security log:

Event ID Meaning
4624 An account was successfully logged on
4625 An account failed to log on

In the System log, the source Service Control Manager writes the events about services — starting, stopping, changing the startup type.

Careful: an Event ID is not a PID

A PID identifies one running process. An Event ID identifies a kind of event: every failed logon on every Windows machine is 4625.

In Event Viewer, use Filter Current Log… in the right panel: tick Critical and Error, or type an Event ID. The same from PowerShell — run it as administrator, because the Security log needs it:

Get-WinEvent -LogName System -MaxEvents 5
Get-WinEvent -FilterHashtable @{ LogName='Security'; Id=4624 } -MaxEvents 3

Events come newest first. -FilterHashtable filters while reading, which is much faster than reading everything and piping it to Where-Object.

The Exercise sheet takes you through the whole hunt: your own line, yesterday's kill, a failed sudo, the reboots, the last apt install, and a failed Windows logon found by its number.

Key takeaways

  • A log is a diary of short timestamped lines: when, which host, who (program and PID), what.
  • Severity goes from 0 (emerg, worst) to 7 (debug); -p err means level 3 and worse.
  • On Debian 13 the journal (systemd-journald) keeps everything; you ask it with journalctl — and there is no /var/log/syslog.
  • Do not scroll: cut the pile with when (--since, -b), who (-u, -t) and how bad (-p), then read the lines above the error.
  • logger writes your own line; sudo attempts are logged; only root, adm and systemd-journal can read the whole journal.
  • Windows: Event Viewer and Get-WinEvent, with System/Application/Security logs, levels and Event IDs (4624 logon, 4625 failed logon).

Quick check

1. Which command shows only the lines written by the service heartbeat?
  • journalctl -p heartbeat
  • journalctl -u heartbeat
  • journalctl -f heartbeat
  • journalctl -b heartbeat

-u filters by unit. -p filters by priority, -f follows live and -b picks a boot.

2. What does journalctl -p err show?
  • Only lines of priority 3
  • Lines of priority 3 to 7
  • Only lines containing the word error
  • Lines of priority 3 and everything worse, 0 to 3

In syslog levels, a lower number is worse. -p err shows err (3) plus crit, alert and emerg. To search for a word you would use -g.

3. You want to watch new log lines appear while you test something. Which option do you use?
  • -f
  • -n
  • -b
  • -r

-f means follow, like tail -f: new lines appear as they are written. Leave with Ctrl+C.

4. On Debian 13, where do you find yesterday's messages of a service?
  • In /var/log/syslog
  • In /var/log/messages
  • In the journal, with journalctl
  • In the unit file of the service

Since Debian 12, rsyslog is not installed by default, so there is no /var/log/syslog. Everything goes to the journal, which you read with journalctl.

5. Why does plain journalctl, without sudo, show you so little?
  • The journal is empty until the first reboot
  • A normal user only sees their own messages
  • journalctl only works over SSH
  • The journal only keeps the last five lines

The journal reveals logins and commands, so it is protected. Only root and the groups adm and systemd-journal see all messages. The hint at the top says so.

6. On a Windows PC, where do you look for failed logons?
  • The System log, Event ID 41
  • The Security log, Event ID 4625
  • The Application log, any Error
  • The Security log, Event ID 4624

Logons are recorded in the Security log. 4625 is "an account failed to log on"; 4624 is a successful logon.

7. A colleague says the heartbeat service stopped yesterday around 09:10, and the machine was rebooted after that. Pick the two commands that will help.
  • sudo journalctl -u heartbeat --since yesterday
  • sudo journalctl -b -1 -u heartbeat
  • sudo journalctl -b -u heartbeat
  • cat /var/log/journal

The event happened before the reboot, so -b (this boot only) misses it. A time window that starts yesterday and so crosses the reboot, or the previous boot (-b -1), both find it. The journal is binary: cat cannot read it.

Next: four services of Tramuntana Apps are broken and you only have two commands. Time to put status and logs to work — The case of the failed service.