Skip to main content
Linux Logs (/var/log) & Bash Essentials
Lesson 5 of 10

Linux Logs (/var/log) & Bash Essentials

Locate and read Linux system and authentication logs in /var/log and the systemd journal, then filter, extract, and count security events with Bash pipelines.

Intermediate 26 min Linux & Windows
Authorised environments only — The techniques described here are used only in training labs and on systems you own or have written permission to test.

Learning objectives

  • Identify where Debian-based and RHEL-based systems store authentication, kernel, and service logs
  • Query and filter systemd service events by unit, priority, and time range with journalctl
  • Combine Bash pipes, stream redirection, and quoting to process text safely
  • Extract IP addresses and counts from log files using grep, awk, sort, and uniq

Recommended first: Linux Terminal & FHS Directory StructureLinux Filesystem Ownership & rwx PermissionsLinux Users, Groups & Sudo BasicsLinux Processes, Signals & systemd Services

#Why this matters

When a systemd service fails, an attacker brute-forces SSH, or a forensics challenge asks what ran on a compromised box, the evidence lives in logs. Reading thousands of raw lines by eye is slow and error-prone. Combining Linux log locations with small Bash pipelines turns noisy logs into clear answers in seconds.

#Where Linux stores logs

Most distributions keep human-readable logs under /var/log/ while also running systemd-journald to capture structured service output. Files under /var/log/ are rotated on a schedule so disks do not fill up, leaving archived files such as auth.log.1 and compressed auth.log.2.gz.

Log path Distro family Typical contents
/var/log/auth.log Debian / Ubuntu SSH logins, sudo invocations, PAM failures
/var/log/secure RHEL / Fedora Equivalent authentication and sudo events
/var/log/syslog or /var/log/messages Debian / RHEL General daemon and system messages
/var/log/kern.log Debian / Ubuntu Kernel notices, hardware, and host firewall drops
/var/log/nginx/ Cross-distro Web server access.log and error.log

#Querying the systemd journal

The journalctl utility queries the binary systemd journal directly, which is especially useful when tracing services managed by systemctl:

  • -u ssh: show entries only for the ssh unit (some systems name it sshd).
  • -p warning: show warnings and higher-severity messages (err, crit, alert, emerg).
  • --since "2026-10-12 01:00:00" --until "2026-10-12 03:00:00": bound the search window.
  • -n 50 and -f: print the last 50 lines, or follow new entries live.
  • --no-pager: print straight to standard output for piping.
Inspecting recent SSH warnings in the journal
$ sudo journalctl -u ssh -p warning --since "2 hours ago" --no-pager
Oct 12 02:13:41 bastion sshd[2410]: Failed password for invalid user admin from 192.0.2.44 port 51902 ssh2
Oct 12 02:13:44 bastion sshd[2412]: Failed password for deploy from 192.0.2.44 port 51910 ssh2

#Bash pipes, redirection, and quoting

Every Linux command opens three standard streams: stdin (descriptor 0), stdout (descriptor 1), and stderr (descriptor 2). Bash lets you wire those streams together cleanly:

  • Pipe (|): Sends stdout of the left command into stdin of the right command.
  • Redirection (>, >>, 2>): > creates or overwrites a file, >> appends to a file, and 2>/dev/null hides permission-denied noise on stderr.
  • Quoting: Double quotes ("$target") expand Bash variables, while single quotes ('{print $11}') protect $ from Bash so tools like awk receive the literal field variable.
  1. Read logscat or zcat /var/log/auth.log*
  2. Filter linesgrep -E "Failed password|Accepted"
  3. Extract fieldawk '{print $(NF-3)}'
  4. Count and ranksort | uniq -c | sort -nr

Small variables and loops also help when checking multiple archived logs without typing repetitive commands:

bash
target_user="deploy"
for logfile in /var/log/auth.log*; do
    echo "=== $logfile ==="
    sudo zgrep -c "Accepted.*for $target_user" "$logfile" 2>/dev/null
done

#Filtering and counting with grep and awk

Two text tools handle most log triage:

  • grep searches line by line. Use -i for case-insensitive matching, -E for extended regular expressions (such as "Failed|Accepted"), -v to exclude noisy lines, and zgrep to search .gz archives seamlessly.
  • awk splits each line into whitespace-separated fields ($1, $2, ..., $NF for the last field; use -F: for colon-delimited files).

In standard sshd lines (Failed password for deploy from 192.0.2.44 port 51910 ssh2), counting from the end with $(NF-3) safely extracts the source IP whether or not invalid user appears earlier in the line.

Summarising failed SSH attempts by source IP
$ sudo grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head -n 3
   1420 192.0.2.44
     18 198.51.100.12
      3 10.10.10.55

Notice why sort comes right before uniq -c: uniq only collapses adjacent duplicate lines. Sorting first groups identical IPs together, uniq -c counts each group, and sort -nr ranks the highest counts first.

#Common mistakes

  • Double-quoting awk expressions: Writing awk "{print $1}" causes Bash to expand $1 before awk runs, printing the entire line ($0) instead of field 1. Always wrap awk programs in single quotes.
  • Calling uniq -c without sort: Unsorted input leaves matching lines scattered, producing broken counts.
  • Skipping rotated .gz logs: Searching only /var/log/auth.log misses older activity stored in auth.log.1 or auth.log.2.gz; use zgrep or zcat.
  • Overwriting evidence with >: Using > instead of >> silently truncates an existing output file.

#Summary

Linux records host and security events across plain-text files in /var/log/ (such as auth.log, syslog, and nginx logs) and the binary systemd journal queried via journalctl. Using Bash standard streams, pipes, single quoting, grep/zgrep, awk, sort, and uniq -c, you can turn raw logs into ranked summaries of suspicious activity. Always check rotated archives and permissions before assuming an event is absent.

Knowledge check

Sign in to take the test, complete lessons and save your progress. Log in

Practice: test your knowledge