#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 thesshunit (some systems name itsshd).-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 50and-f: print the last 50 lines, or follow new entries live.--no-pager: print straight to standard output for piping.
$ 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 (
|): Sendsstdoutof the left command intostdinof the right command. - Redirection (
>,>>,2>):>creates or overwrites a file,>>appends to a file, and2>/dev/nullhides permission-denied noise onstderr. - Quoting: Double quotes (
"$target") expand Bash variables, while single quotes ('{print $11}') protect$from Bash so tools likeawkreceive the literal field variable.
- Read logscat or zcat /var/log/auth.log*
- Filter linesgrep -E "Failed password|Accepted"
- Extract fieldawk '{print $(NF-3)}'
- Count and ranksort | uniq -c | sort -nr
Small variables and loops also help when checking multiple archived logs without typing repetitive commands:
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:
grepsearches line by line. Use-ifor case-insensitive matching,-Efor extended regular expressions (such as"Failed|Accepted"),-vto exclude noisy lines, andzgrepto search.gzarchives seamlessly.awksplits each line into whitespace-separated fields ($1,$2, ...,$NFfor 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.
$ 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.55Notice 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
awkexpressions: Writingawk "{print $1}"causes Bash to expand$1beforeawkruns, printing the entire line ($0) instead of field 1. Always wrapawkprograms in single quotes. - Calling
uniq -cwithoutsort: Unsorted input leaves matching lines scattered, producing broken counts. - Skipping rotated
.gzlogs: Searching only/var/log/auth.logmisses older activity stored inauth.log.1orauth.log.2.gz; usezgreporzcat. - 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.