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.
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.
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
08:16:02 sshd: Accepted publickey for learner from 198.51.100.7grep prints lines that match a pattern. It's the fastest way to answer "did this happen, and when?".
| Command | Does |
|---|---|
grep -n "Failed password" sample-auth.log | Matching lines with line numbers |
grep -c "Failed password" sample-auth.log | Counts matching lines |
grep -i "invalid user" FILE | Case-insensitive |
grep -v "CRON" FILE | Lines 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.
1. Show every "Failed password" line with line numbers.
Check your understanding0 of 3 correct
Quiz score: 0 of 3
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.