A security-focused guide to parsing Linux authentication logs with grep — covering failed login extraction, attacking IP identification, account abuse detection, and one-liner triage commands for incident response.
What is auth.log?
On Debian-based Linux distributions (Ubuntu, Debian), /var/log/auth.log is the plain-text log file that records all authentication events: SSH login attempts, sudo usage, PAM authentication, su invocations, and cron authentication. On Red Hat-based distributions (CentOS, RHEL, AlmaLinux), the equivalent file is /var/log/secure. Because these are plain text files, standard tools — grep, awk, sort, uniq — can extract actionable intelligence without requiring any specialized log analysis software. This makes auth.log grep analysis the fastest path to initial triage on any server, regardless of whether additional tools like fail2ban, auditd, or a SIEM are installed. The file is typically readable only by root and the adm group, providing a basic form of log integrity protection against non-privileged tampering.
Syntax
grep 'Failed password' /var/log/auth.log
grep 'Accepted password' /var/log/auth.log
grep 'sudo' /var/log/auth.log
grep 'Invalid user' /var/log/auth.log
# RHEL/CentOS equivalent
grep 'Failed password' /var/log/secureThe file path differs by distribution: /var/log/auth.log on Debian/Ubuntu, /var/log/secure on RHEL/CentOS/AlmaLinux. Key search terms include: Failed password (failed auth attempts), Accepted (successful logins), Invalid user (non-existent username attempts), sudo (privilege escalation events), and Disconnected (session terminations).
Key grep Options for Log Analysis
| Flag / Parameter | Description | Security Note |
|---|---|---|
-E 'pattern1|pattern2' | Extended regex — match multiple patterns in a single grep command | — |
-c | Count matching lines instead of printing them | Use to quantify total failed attempts without processing large output |
-v pattern | Invert match — exclude lines containing the pattern | Use to filter out known-good IPs or benign events from analysis output |
-o | Print only the matching portion of the line (not the whole line) | Combine with regex and sort/uniq to extract and count specific fields like IP addresses |
--color=auto | Highlight matching text in terminal output | — |
-i | Case-insensitive matching | — |
Common Use Cases
Extract All Unique IPs with 10+ Failed SSH Attempts Today
The most common immediate triage task: identify which IP addresses are actively attacking the server, how many attempts each has made, and whether they should be manually blocked if fail2ban has not handled them. This one-liner requires no additional tools beyond standard Unix utilities.
grep 'Failed password' /var/log/auth.log | grep "$(date +'%b %e')" | grep -oE '[0-9]+\.[0-9]+\.[0-9]+\.[0-9]+' | sort | uniq -c | sort -rn | awk '$1 >= 10 {print $1, $2}'Find All Successful SSH Logins Today
After identifying a potential compromise, immediately list all successful authentications to determine which accounts were accessed and from which IPs. An unexpected source IP combined with a successful login is a confirmed unauthorized access event.
grep 'Accepted' /var/log/auth.log | grep "$(date +'%b %e')"
grep 'Accepted password\|Accepted publickey' /var/log/auth.log | tail -20Audit All sudo Commands Executed Today
Every sudo invocation appears in auth.log with the username, the command executed, and whether it was permitted or denied. Reviewing this output reveals what privileged commands were run on the server and whether any unexpected users escalated privileges.
grep 'sudo' /var/log/auth.log | grep "$(date +'%b %e')" | grep -v 'pam_unix'
grep 'sudo.*COMMAND' /var/log/auth.log | tail -30Security Relevance: Zero-Tool Rapid Triage
The value of auth.log grep analysis is that it requires nothing beyond what is installed on every Linux system by default. When responding to a suspected incident on an unfamiliar server — especially one where you cannot install additional software — grep-based log analysis gives you actionable intelligence within seconds. The key patterns to know by memory are: Failed password (attack in progress), Accepted (successful login — verify the source IP is expected), Invalid user (username enumeration by scanner), sudo.*COMMAND (privilege usage — document all instances), and session opened for user root (root-level interactive activity). On Debian systems, auth.log is readable only by root and the adm group — if you cannot read it as your user, that is itself a configuration finding. Combine grep triage with journalctl for the most complete authentication picture.
- auth.log is a plain-text file writable by root — a compromised root account can edit or clear it; always cross-reference with journalctl
- On RHEL/CentOS, the file is /var/log/secure — scripts hardcoded to /var/log/auth.log will find no data on these systems
- Rotated log files (auth.log.1, auth.log.2.gz) contain older history — use zgrep for compressed files
- grep output from auth.log does not include geolocation or reputation data — an IP showing few failed attempts may still be an attacker using slow/low detection techniques
- sudo log entries show the command argument but may be truncated — use journalctl -u sudo for complete untruncated command logging
Practical Examples
One-liner: top attacking IPs with attempt count from today's auth.log
grep 'Failed password' /var/log/auth.log | grep "$(date +'%b %e')" | grep -oP 'from \K[^ ]+' | sort | uniq -c | sort -rn | head -15Uses Perl-compatible regex (-oP) to extract precisely the IP address field from ‘from IP’ patterns in the auth.log format. More reliable than extracting the last IPv4 pattern, which can match port numbers. Returns the top 15 attacking IPs for today with attempt counts.
Find attempts against non-existent usernames (username enumeration or credential stuffing)
grep 'Invalid user' /var/log/auth.log | grep "$(date +'%b %e')" | awk '{print $8}' | sort | uniq -c | sort -rn | head -20Extracts the attempted username from ‘Invalid user USERNAME from IP’ log entries. High frequency attempts against specific usernames (admin, ubuntu, test, oracle, pi) indicate automated credential stuffing. Attempts against your actual administrative usernames indicate targeted reconnaissance.
Search compressed rotated auth.log archives for historical events
zgrep 'Accepted password' /var/log/auth.log.2.gz
for f in /var/log/auth.log* ; do
if [[ "$f" == *.gz ]]; then
zgrep 'Accepted password' "$f"
else
grep 'Accepted password' "$f"
fi
doneExtends the analysis window beyond the current auth.log to rotated archives. Compressed files require zgrep. This is essential when investigating a breach that may have occurred days or weeks ago, before the current log rotation cycle.
Troubleshooting Common Issues
Problem: auth.log is empty or does not exist
Solution: On Ubuntu/Debian, verify rsyslog is running: systemctl status rsyslog. If not installed, auth events may only be in journald. On RHEL/CentOS, check /var/log/secure instead — auth.log does not exist on these systems. Also check /etc/rsyslog.conf for the auth.* or authpriv.* routing rules.
Problem: grep finds no output for 'Failed password' even though SSH attacks are visible in netstat
Solution: Verify sshd is configured to log to the system logger. Check grep -i syslog /etc/ssh/sshd_config — SyslogFacility should be AUTH or AUTHPRIV. Also verify rsyslog is forwarding auth facility logs to auth.log: grep auth /etc/rsyslog.conf or grep auth /etc/rsyslog.d/*.conf.
Problem: Auth.log timestamps show wrong timezone
Solution: auth.log entries use the system’s local timezone. If timestamps look offset, check the system timezone with timedatectl. For consistent UTC-based analysis, use journalctl which stores in UTC and displays in local time but can be queried with explicit UTC times using –utc flag.
Summary
Grep-based auth.log analysis is the fastest path to SSH security intelligence on any Linux server. The patterns to know — Failed password, Accepted, Invalid user, sudo.*COMMAND — cover the majority of initial triage scenarios. Combine with sort, uniq -c, and awk to extract structured intelligence from unstructured text in seconds. When more comprehensive analysis is needed, escalate to journalctl, lastb, or ausearch based on the specific investigation requirement.
- The four essential search terms:
Failed password,Accepted,Invalid user,sudo.*COMMAND— cover 80% of initial triage scenarios - On RHEL/CentOS, the auth log is
/var/log/secure— not auth.log — adjust all commands accordingly - Pipe through
sort | uniq -c | sort -rnto convert any extracted field into a ranked frequency list — instant attack intelligence
Related Commands
journalctl for structured authentication logs • lastb to audit failed login attempts • fail2ban for automated IP blocking • ausearch for kernel audit log analysis
Your Server Logs Contain Threats — Are You Reading Them?
INTRAM configures automated log monitoring and alerting, parsing auth.log and journald for anomalous authentication events and notifying your team before a breach escalates.
Set Up Log Monitoring