Easier to read with cream paper, a tint, more contrast or larger text? Choose your colours and text, once, for every page.

Free lesson

Finding a fault from a log

After this lesson: I can find a fault from a log and fix it.

45 min lesson9 commandsEveryday Linux commands lab: the thirty commands engineers type from memory

The price list that vanished

A small online shop has a page that shows its price list. One morning the page says "File not found".

The owner is sure the file is still there. Is she right? Guessing will not tell you. The machine keeps a record of what went wrong, if you know where to look.

By the end of this lesson

Note

I can find a fault from a log and fix it.

Key words

WordWhat it meansExample
Faultsomething wrong that stops a system working
Symptomwhat a user sees when there is a fault
Loga file where a program writes what it did, with the time
Journalthe log systemd keeps for every servicejournalctl
Status codethe number a web server answers with404

The cells students fill in are left blank in this preview.

Before you start

Note

Press Start the lab on the bar at the top and wait for the machine. This lab has a log that grows every two seconds and a small web server on port 8000. Press the Runs in order.

Read the log before you guess

A log line starts with the date and time. Then comes a level, such as INFO for normal work or ERROR for a problem. Then comes a message.

The newest lines are at the end, so tail shows them. grep -c ERROR counts the error lines. A pipe into tail -n 2 shows only the two newest errors.

The three newest lines

Command
tail -n 3 /var/log/test.log

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

How many errors so far?

Command
grep -c ERROR /var/log/test.log

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

The two newest errors

Command
grep ERROR /var/log/test.log | tail -n 2

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Worked example: four steps to a fix

StepAskAt the shop
1 SymptomWhat does the user see?The page says File not found
2 EvidenceWhat does the log say?The server says code 404
3 CauseWhy did that happen?Nobody may read the file
4 Fix and checkChange one thing, then test againchmod 644, then load the page

The cells students fill in are left blank in this preview.

Make the fault, then find it

The lab's web server shares a file, report.csv. The first Run breaks it on purpose: chmod 000 takes every right away.

Then work like somebody who was not told. curl -sI asks the server for the file and shows only its answer. journalctl -u lab-web reads the server's own log.

Break it on purpose

Command
sudo chmod 000 /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

What does the server answer?

Command
curl -sI http://localhost:8000/report.csv | head -n 1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Read the server's own log

Command
sudo journalctl -u lab-web -n 2 --no-pager -o cat

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Is the file really gone?

Command
ls -l /srv/lab-web

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

A common mistake

Warning

Many people believe an error message word for word. "File not found" sounded like a missing file, but the file was there. A message says what the program thinks went wrong. Check the real thing, here with ls -l, before you fix anything.

Fix it: let everybody read it

Command
sudo chmod 644 /srv/lab-web/report.csv

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Check the fix

Command
curl -sI http://localhost:8000/report.csv | head -n 1

Shown here, not run. Create a free account to press Run and type it into your own lab’s terminal.

Going further

Try a second fault. Run sudo systemctl stop lab-web yourself, then forget you did.

Now find it with the four steps. curl -sS http://localhost:8000/hello.txt shows the symptom. systemctl is-active lab-web and the journal give the evidence. Write down the cause and the fix, then fix it and check.

About this lab and its source: Everyday Linux commands lab: the thirty commands engineers type from memory

Teaching a class? Set this module for your students with a teacher guide, marked assignments and a lab for every student.