Something broke at half past two. You know roughly when, you’ve got a vague idea which service, and somewhere in /var/log or the journal is the one line that explains it. Between you and that line: a few hundred megabytes of text, four rotated copies of the same file with different compression, and a systemd journal that doesn’t answer to grep at all.
This is how I search logs when it matters. Not every flag grep has, just the ones that find things, in the order I reach for them, with the traps that waste time marked out. Everything was run on Ubuntu 24.04 (GNU grep 3.11, ripgrep 14) and Red Hat Enterprise Linux 9.7 (grep 3.6, ripgrep 15 from EPEL), because the two put logs in different files with different timestamp formats, and a command that works on one can return nothing on the other.
First, know where the logs are
The file names are the first thing that differs between the families, and it trips up anyone who moves between them:
| What | Ubuntu / Debian | RHEL / Alma / Rocky / Oracle |
|---|---|---|
| Logins, sudo, SSH, PAM | /var/log/auth.log |
/var/log/secure |
| General system messages | /var/log/syslog |
/var/log/messages |
| Kernel | /var/log/kern.log |
in messages, or journalctl -k |
| Package manager | /var/log/dpkg.log, /var/log/apt/ |
/var/log/dnf.log, dnf.rpm.log |
| Cron | in syslog |
/var/log/cron |
/var/log/mail.log |
/var/log/maillog |
|
| Read permission | group adm |
usually root only |
Ubuntu puts your first user in adm at install time, so you can read auth.log without sudo. RHEL doesn’t, so you’ll be typing sudo a lot, and if you get “Permission denied” from grep on a log file, that’s why.
The second difference is the timestamp, and it decides how you search by time later on:
# Ubuntu 24.04: RFC 3339, microseconds, timezone offset
2026-02-09T17:37:32.383416+05:00 web1 sshd[6645]: Failed password for root from 127.0.0.1 port 48018 ssh2
# RHEL 9: classic syslog
Feb 9 17:37:16 app1 sudo[19349]: pam_unix(sudo:session): session opened for user root(uid=0) by (uid=1000)
Ubuntu switched rsyslog to the ISO format in 24.04. If you have scripts that grep for ^Feb 9, they’ve been silently returning nothing since the upgrade.
Both distros also write everything to the systemd journal, which is a separate binary store with its own search tool. Ubuntu keeps it on disk across reboots; RHEL keeps it in RAM by default and throws it away at shutdown. On a RHEL box, the flat files are the only history you have unless you’ve made the journal persistent.
grep: the flags that actually find things
There’s a lot of grep. Here’s the subset that handles nearly every log search I do.
grep "Failed password for" /var/log/auth.log | tail -3
2026-02-09T17:38:41.557158+05:00 web1 sshd[6846]: Failed password for invalid user git from 127.0.0.1 port 33670 ssh2
2026-02-09T17:38:44.623118+05:00 web1 sshd[6854]: Failed password for invalid user git from 127.0.0.1 port 33678 ssh2
2026-02-09T17:38:49.081585+05:00 web1 sshd[6862]: Failed password for invalid user git from 127.0.0.1 port 33692 ssh2
grep -c "Failed password for" /var/log/auth.log
24
grep -n "Failed password for" /var/log/auth.log | head -1
648:2026-02-09T17:37:32.383416+05:00 web1 sshd[6645]: Failed password for root from 127.0.0.1 port 48018 ssh2
-c counts instead of printing, which is the first thing to do on a big file: it tells you whether you’re about to get 24 lines or 24,000. -n gives line numbers, so you can jump straight there in less with 648g.
grep -i "FAILED PASSWORD FOR" /var/log/auth.log | wc -l
24
grep -w "sshd" /var/log/auth.log | wc -l; grep "sshd" /var/log/auth.log | wc -l
82
88
-i ignores case. -w matches whole words only; the six extra lines without it were sshd-session, which is a different process on newer OpenSSH and often exactly the thing you’re trying to separate.
Context is what turns a match into an explanation. -B lines before, -A lines after, -C both:
grep -B1 -A2 "Failed password for invalid user pi" /var/log/auth.log | head -4
2026-02-09T17:38:30.221271+05:00 web1 sshd[6820]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=127.0.0.1
2026-02-09T17:38:32.401654+05:00 web1 sshd[6820]: Failed password for invalid user pi from 127.0.0.1 port 59808 ssh2
2026-02-09T17:38:33.154405+05:00 web1 sshd[6820]: Connection closed by invalid user pi 127.0.0.1 port 59808 [preauth]
2026-02-09T17:38:33.305456+05:00 web1 sshd[6829]: Invalid user pi from 127.0.0.1 port 59822
Now you can see the whole exchange: PAM refused, sshd logged it, the client dropped, the next attempt started. That’s the pattern of a scanner, not a person.
Several patterns at once, and excluding noise, which is at least half of log searching:
grep -E "Failed password|Invalid user" /var/log/auth.log | wc -l
53
grep "sshd\[" /var/log/auth.log | grep -vE "pam_unix|Connection (from|closed)|Received disconnect|Disconnected" | tail -3
2026-02-09T17:38:52.416887+05:00 web1 sshd[6885]: Server listening on 0.0.0.0 port 22.
2026-02-09T17:38:52.417011+05:00 web1 sshd[6885]: Server listening on :: port 22.
2026-02-09T17:38:52.429188+05:00 web1 sshd[6885]: Received signal 15; terminating.
-E turns on extended regex so | means “or”. -v inverts, and chaining a positive grep into a -v grep with a list of known-boring patterns is how you get from 500 lines to the five that matter. Keep that exclusion list somewhere; it grows.
Pulling out just the interesting part
-o prints only the matched text rather than the whole line. Combined with sort | uniq -c | sort -rn it turns a log into a report:
grep "Failed password for" /var/log/auth.log | grep -oE "user \S+" | sort | uniq -c | sort -rn
3 user ubuntu
3 user test
3 user pi
3 user oracle
3 user git
3 user admin
grep "Failed password for" /var/log/auth.log | grep -oE "from [0-9.]+" | sort | uniq -c | sort -rn | head -3
24 from 127.0.0.1
Who they tried, and from where. On a real internet-facing box the second list is your block list. If you want the value without the label, -P gives you Perl regex and \K means “start the match here”:
grep -oP "Failed password for (invalid user )?\K\S+" /var/log/auth.log | sort | uniq -c | sort -rn
3 ubuntu
3 test
3 root
3 pi
3 oracle
3 git
3 asif
3 admin
That one catches both “Failed password for root” and “Failed password for invalid user admin” and gives you the username either way. Once you have the list, the SSH configuration guide covers shutting the door.
Searching by time
“It broke around half past five” is the most common starting point, and it’s where the timestamp format matters. Anchor the pattern to the start of the line with ^ so you’re matching the timestamp and not some number inside a message.
Ubuntu, ISO timestamps. Everything between 17:30 and 17:39:
grep "^2026-02-09T17:3" /var/log/auth.log | grep -c sshd
45
RHEL, syslog timestamps. Everything from 17:05 to 17:15 in messages:
sudo grep -E "^Feb 9 17:(0[5-9]|1[0-5])" /var/log/messages | wc -l
1041
Note the two spaces before a single-digit day: Feb 9, not Feb 9. The syslog format right-pads the day, and that space costs people twenty minutes at least once.
For an exact window with a start and end, sed prints between two matching lines:
sed -n '/^2026-02-09T17:37:32/,/^2026-02-09T17:38:49/p' /var/log/auth.log | grep -c sshd
106
The catch with the sed range: if the end timestamp doesn’t exist in the file exactly, it prints to the end. Use a prefix that’s guaranteed to occur, like the minute rather than the second.
And honestly, for time windows the journal is just better, because it understands time instead of matching text:
journalctl -u ssh --since "2026-02-09 17:33:32" --until "2026-02-09 17:33:34" --no-pager | wc -l
27
journalctl --since "30 min ago" -g "Failed password" -u ssh --no-pager | wc -l
24
Rotated and compressed logs
The current file is a fraction of the history. logrotate has been shuffling the rest:
ls -l /var/log/auth.log*
-rw-r----- 1 syslog adm 73393 Feb 9 17:34 /var/log/auth.log
-rw-r----- 1 syslog adm 763 Feb 8 02:24 /var/log/auth.log.1
-rw-r----- 1 syslog adm 2516 Jun 8 2025 /var/log/auth.log.2.gz
-rw-r----- 1 syslog adm 362 Jun 5 2025 /var/log/auth.log.3.gz
-rw-r----- 1 syslog adm 2072 Apr 13 2025 /var/log/auth.log.4.gz
.1 is last week’s, uncompressed (that’s delaycompress in the logrotate config, so a process still holding the file open doesn’t lose writes). .2.gz and older are gzipped. Plain grep on a gzip file gives you this:
grep "session opened" /var/log/auth.log.2.gz
$
Nothing. Not an error, just no matches, because it’s searching compressed bytes. zgrep takes the same flags and decompresses on the way through:
zgrep -c "session opened" /var/log/auth.log*.gz
/var/log/auth.log.2.gz:46
/var/log/auth.log.3.gz:2
/var/log/auth.log.4.gz:23
It handles uncompressed files too, so you can point it at the whole set. The trap is ordering: auth.log* expands alphabetically, which puts the current file first and the oldest last, backwards from how you want to read them. ls -tr sorts by modification time, oldest first:
ls -tr /var/log/auth.log*
/var/log/auth.log.4.gz
/var/log/auth.log.3.gz
/var/log/auth.log.2.gz
/var/log/auth.log.1
/var/log/auth.log
zgrep -h "COMMAND=/usr/bin/apt" $(ls -tr /var/log/auth.log*) | head -3
2025-04-02T10:18:39.938829+05:00 web1 sudo: asif : TTY=pts/0 ; PWD=/home/asif ; USER=root ; COMMAND=/usr/bin/apt install net-tools
2025-06-07T11:33:42.790137+05:00 web1 sudo: asif : TTY=pts/2 ; PWD=/mnt/d/projects/project12 ; USER=root ; COMMAND=/usr/bin/apt-get update
2025-06-07T11:33:45.493411+05:00 web1 sudo: asif : TTY=pts/2 ; PWD=/mnt/d/projects/project12 ; USER=root ; COMMAND=/usr/bin/apt-get install -y docker.io
Ten months of “what did anyone install with sudo”, in order, in one line. -h suppresses the filename prefix so the output is just log lines you can pipe onward.
For other compression there’s a family: xzgrep, bzgrep, lzgrep, zstdgrep. Ubuntu ships most of them; RHEL 9 minimal ships only the gzip one. Check with ls /usr/bin/*grep. And if you’d rather not remember any of them, zcat file.gz | grep pattern always works, as does ripgrep’s -z, below.
Searching everything under /var/log
When you don’t know which file, -r recurses. Filter by filename with --include or you’ll be searching binary login records and the journal:
sudo grep -r "Failed password for" /var/log
grep: /var/log/journal/b725f5cb.../user-1000.journal: binary file matches
grep: /var/log/journal/b725f5cb.../system.journal: binary file matches
/var/log/auth.log:2026-02-09T17:37:32.383416+05:00 web1 sshd[6645]: Failed password for root from 127.0.0.1 port 48018 ssh2
...
sudo grep -r "Out of memory" /var/log --include="kern.log*" --include="syslog*"
That “binary file matches” line is grep telling you it found the pattern in a file it thinks isn’t text, and it won’t print the line. Journal files, wtmp, btmp, lastlog and anything .gz all trigger it. For the journal, use journalctl. For wtmp use last. For gzip use zgrep. If you’re certain the file is text with a stray control character in it, grep -a forces it to be treated as text.
Recursive grep also doesn’t look inside the .gz files at all; it just reports them as binary. For a real “search all the history” pass, ripgrep is the better tool, and that’s coming.
journalctl: searching the journal
Both distros write to the journal as well as (or on RHEL, instead of, once it’s persistent) the flat files. The journal isn’t text, so grep is useless on it, but journalctl has its own search and it’s more precise because entries carry structured fields.
journalctl -u ssh -g "Failed password" --no-pager | tail -2
Feb 9 17:38:44 web1 sshd[6854]: Failed password for invalid user git from 127.0.0.1 port 33678 ssh2
Feb 9 17:38:49 web1 sshd[6862]: Failed password for invalid user git from 127.0.0.1 port 33692 ssh2
-g is grep (a regex, over the message text). -u ssh restricts to one service, which is the thing flat-file grep can’t do without relying on a process name appearing in the line. On RHEL the unit is sshd, not ssh; that one difference accounts for a lot of “journalctl shows nothing”.
There’s an undocumented-feeling rule about case that you should know:
journalctl -u ssh -g "failed password" --no-pager | wc -l # all lowercase
25
journalctl -u ssh -g "Failed Password" --no-pager | wc -l # mixed case
2
If the pattern is entirely lowercase, matching is case-insensitive. If it contains any uppercase, it’s case-sensitive. The 25 versus 2 above is the same log; “Failed Password” with a capital P only matched two unrelated lines. Set --case-sensitive=false explicitly when it matters, or just type lowercase.
The filters that stack with -g:
journalctl -p err -b # errors and worse, this boot
journalctl -k -g "oom|Out of memory" # kernel messages only
journalctl _COMM=sshd --since "1 hour ago" -o cat
journalctl -u nginx --since "10:00" --until "10:30"
journalctl -u ssh -r -n 20 -o short-iso # newest first, ISO timestamps
-o cat drops the timestamp and hostname so the output is just message text, ready for the same sort | uniq -c treatment as a flat file. And -o json gives you every field, which with jq means you can do the extraction properly instead of with regex on a rendered line:
journalctl -u ssh -g "Failed password" -o json -n1 --no-pager | jq '{MESSAGE, _PID, _COMM, PRIORITY, _HOSTNAME}'
{
"MESSAGE": "Failed password for invalid user git from 127.0.0.1 port 33692 ssh2",
"_PID": "6862",
"_COMM": "sshd",
"PRIORITY": "6",
"_HOSTNAME": "web1"
}
journalctl -o json -g "Failed password" --no-pager \
| jq -r '.MESSAGE | capture("user (?<u>\\S+) from (?<ip>\\S+)") | "\(.ip) \(.u)"' \
| sort | uniq -c | sort -rn | head -3
3 127.0.0.1 ubuntu
3 127.0.0.1 test
3 127.0.0.1 pi
The fields starting with underscore (_PID, _COMM, _UID) are added by journald from the kernel and can’t be spoofed by the logging process. Useful when you’re not sure whether to trust what a log line says about itself. The full journalctl article has the rest of the flags, including why RHEL loses the journal on reboot and how to stop it.
ripgrep: faster, and it reads gzip
ripgrep (rg) is a grep replacement written for speed. apt install ripgrep on Ubuntu; on RHEL it’s in EPEL, so dnf install epel-release ripgrep. Same basic syntax, a few changes that matter for logs:
rg -c "Failed password for" /var/log/auth.log
24
rg -z -c "authentication failure" /var/log/auth.log*
/var/log/auth.log:24
/var/log/auth.log.4.gz:1
-z searches inside gzip, bzip2, xz, lz4, zstd and brotli files, using whichever decompressor is installed. One flag, all the rotated history, no zgrep/xzgrep guessing. Notice it only listed files with matches; ripgrep doesn’t print the zero-count lines grep does.
Recursive is the default, it skips binary files quietly instead of complaining, and --stats tells you what it covered:
sudo rg -z -c "Failed password for" /var/log --stats | tail -6
1 files contained matches
115 files searched
0 bytes printed
6934548 bytes searched
0.047200 seconds spent searching
0.012202 seconds
115 files including the compressed ones, in 47 milliseconds. That’s the “search all of /var/log including history” command, and it’s the one I’d put on a sticky note.
Two more that are genuinely nicer than the grep equivalents. -r rewrites the output using capture groups, which replaces a grep -o | sed pipeline:
rg -o 'Failed password for (invalid user )?(\w+)' -r '$2' /var/log/auth.log | sort | uniq -c | sort -rn | head -4
3 ubuntu
3 test
3 root
3 pi
And when you’re a normal user, ripgrep tells you what it couldn’t read instead of silently skipping it, which is worth knowing on RHEL where most of /var/log is root-only.
The thing to remember: ripgrep respects .gitignore files and skips hidden files by default. That’s great in a source tree and irrelevant in /var/log, but if you ever wonder why it skipped something, -uu turns all of that off.
Making it fast
Here’s a 388 MB, 4.5 million line log file, searched a few ways. Timings are from the same box, same file, cached in memory:
ls -lh big.log; wc -l big.log
388M big.log
4500000 big.log
time grep -c "Out of memory" big.log
563110
real 0m0.185s
time grep -c -F "Out of memory" big.log
563110
real 0m0.168s
time grep -c -E "Failed password.*from 203\.0\.113\.[0-9]+" big.log
562462
real 0m0.296s
time rg -c "Failed password.*from 203\.0\.113\.[0-9]+" big.log
562462
real 0m0.176s
time rg -c "Out of memory" big.log
563110
real 0m0.076s
So, in order of what actually helps:
- Search the smallest thing that can contain the answer. Narrow by time window, by file, by unit. Nothing else comes close. Searching 5 MB is faster than any trick you can pull on 388 MB.
- Stop early.
grep -m1exits at the first match; on this file it returned in 2 milliseconds instead of 185. If you just need to know whether something happened,-m1or-q(quiet, exit status only) is the flag. - Count before you print.
-cfirst, then decide whether you want to see 24 lines or need to filter further. - Use ripgrep on anything big. 2.4x faster than grep on the plain search here, 1.7x on the regex, and the gap widens on larger files and directories because it runs in parallel.
-Ffor fixed strings is a small, free win when your pattern has no regex in it. It also stops.in an IP address from matching any character.
Two things that didn’t help. LC_ALL=C grep, the classic advice, made no measurable difference here: modern grep is already fast on ASCII data, and the trick only pays off with older grep versions or multibyte patterns. And compression: zgrep took 2.05s on the gzipped copy against 0.19s on the plain file, and rg -z was 2.34s, because 90% of the time is gunzip, not searching. If you search old logs often, decompress them once or keep more uncompressed generations in logrotate.
Watching a log live
Sometimes you want to see the line as it happens rather than find it afterwards:
tail -f /var/log/auth.log | grep --line-buffered "Failed password"
journalctl -f -u ssh -g "Failed|Invalid"
less +F /var/log/syslog
--line-buffered matters. Without it, grep in a pipe holds output until it has 4 KB to write, so your live filter shows nothing for minutes and then dumps a screenful. journalctl -f doesn’t have that problem. And less +F is the underrated one: it follows like tail -f, Ctrl-C stops following so you can scroll back and search with /, and &pattern filters the view to matching lines only. Type F to resume following.
Counting things with awk
grep finds lines. awk reads columns. Together they answer “how many, per what”:
# failed logins per minute (Ubuntu ISO timestamps: characters 12-16 are HH:MM)
grep "Failed password for" /var/log/auth.log | awk '{print substr($1,12,5)}' | sort | uniq -c
9 17:37
15 17:38
3 17:39
# which programs are the noisiest (RHEL: program name is field 5)
sudo awk '{print $5}' /var/log/messages | cut -d[ -f1 | sort | uniq -c | sort -rn | head -5
4995 kernel:
2008 systemd
133 runsvc.sh
100 pm2
95 systemd-logind
On Ubuntu’s ISO format the program name is field 3, not 5, because the timestamp is one token instead of three. Same idea, different column number. That’s most of awk for log work: pick a field, count it. cut, sort and uniq do the rest, and if any of those are new, the essential commands guide covers them.
Which tool, when
| Situation | Reach for |
|---|---|
| One file, know what you’re looking for | grep -n pattern file |
| Not sure what’s around the match | grep -C3 |
| Rotated history, gzip or otherwise | rg -z, or zgrep $(ls -tr file*) |
All of /var/log, including compressed |
sudo rg -z pattern /var/log |
| One service, one time window | journalctl -u svc --since ... -g pattern |
| Kernel messages | journalctl -k |
| Top N of something | grep -o ... | sort | uniq -c | sort -rn |
| Field extraction from structured data | journalctl -o json | jq |
| Live | journalctl -f -g, less +F |
| Just “did it happen?” | grep -q or grep -m1 |
| Huge file, repeated searches | rg, and narrow by time first |
Last thing. Every example above assumes the log is on the box you’re logged into. Once you’re past a handful of servers, the search you actually want is “which host had this error”, and no amount of grep does that. Ship the logs somewhere central. Until then, for h in web1 web2 web3; do ssh $h "grep -c 'Out of memory' /var/log/syslog"; done is ugly and it works, and the passwordless SSH setup is what makes it not painful.