Module 1 · Linux Essentials for Security · Lesson 5 of 10

Searching logs with grep

Find failed logins in a synthetic auth log, count them correctly, and pull out the sources.

In this lesson · about 22 minutes

  • Search a log with grep, line numbers and counts
  • Build a pipeline to extract and count source addresses
  • Explain the difference between matching lines and events

Whiteboard · watch and try

Whiteboard: a conveyor belt for evidence

Press Play. The tutor builds the classic "who is attacking us?" search one piece at a time.

6 steps
Press Play and the tutor will talk you through it while writing on the board. Pause any time to write on the board yourself.

Tip: press Pause, then use the pen to work along on the board. Space plays and pauses; Ctrl+Z undoes your last mark.

Try it · watch it happen

A log search, one stage at a time

Press Play to watch the diary shrink stage by stage. Then type your own search word.

auth.log|
grep|
awk|
sort|
uniq -c|
sort -rn
11 lines after: cat auth.log
08:00:04 sshd: Server listening on 0.0.0.0 port 2208:00:41 CRON: session opened for user root08:00:58 sudo: admin : COMMAND=/usr/bin/apt update08:01:12 sshd: Failed password for root from 203.0.113.4508:01:15 sshd: Failed password for root from 203.0.113.4508:01:19 sshd: Failed password for invalid user admin from 203.0.113.4508:05:00 CRON: session closed for user root08:15:40 sshd: Failed password for learner from 198.51.100.708:16:02 sshd: Accepted publickey for learner from 198.51.100.708:30:11 sshd: Failed password for invalid user test from 203.0.113.4508:31:40 sshd: Failed password for root from 203.0.113.45
Step 1 of 6Here is the whole diary: 11 lines of everything that happened. Too much to read by eye on a real server, where there could be a million lines.
grep -i "Accepted" auth.log → 1 matching line
08:16:02 sshd: Accepted publickey for learner from 198.51.100.7

grep prints lines that match a pattern. It's the fastest way to answer "did this happen, and when?".

CommandDoes
grep -n "Failed password" sample-auth.logMatching lines with line numbers
grep -c "Failed password" sample-auth.logCounts matching lines
grep -i "invalid user" FILECase-insensitive
grep -v "CRON" FILELines that do not match
find ~/lab -type f -name "*.log"Find log files under a folder

Quote your patterns. Without quotes, the shell splits Failed password into two arguments and grep searches for "Failed" in a file called "password". Quotes also stop the shell expanding * before grep sees it.

Pipelines

The | sends one command's output into the next. A classic pattern for "who is attacking?":

grep "Failed password" sample-auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn

grep finds the lines, awk prints the IP field (fourth from the end), sort | uniq -c counts each unique value, and sort -rn puts the biggest first.

Hands-on lab (simulated terminal)

Practice challenge 03: failed logins

A synthetic log, sample-auth.log, is in your lab folder. It contains made-up events with documentation IP addresses. Never share real authentication logs.

Step 1 of 4. Type each command and press Enter. Nothing runs on your computer.

Practice challenge 03: failed logins

1. Show every "Failed password" line with line numbers.

learner@ubuntu-lab:~/lab$

Check your understanding0 of 3 correct

Question 1

1.Why does grep -c sometimes undercount events?

Question 2

2.Why quote the pattern in grep "Failed password" FILE?

Question 3

3.In the pipeline sort | uniq -c, why sort first?

Quiz score: 0 of 3

Tip: answer every quiz question correctly first.

Noviq Academy is free and for learning. Every terminal here is simulated and every log is synthetic. Practise on a disposable VM with a snapshot, never on a production server. Ubuntu is a trademark of Canonical Ltd.; this course is independent of Canonical.