Explain script : 0 7 * * * grep “Failed password” /var/log/auth.log | awk ‘{print $(NF-3)}’ | sort | uniq -c | sort -rn > /tmp/ssh_fail.txt
This is a Linux Crontab + Bash command pipeline used for SSH security monitoring. It runs every day at 7:00 AM, searches SSH authentication logs for failed login attempts, extracts the suspected source IP addresses, counts failed attempts per IP, sorts the results, and saves the report to /tmp/ssh_fail.txt.
Let’s break it down step by step.
1. Complete command
0 7 * * * grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn > /tmp/ssh_fail.txt
This command has two main parts:
- Cron schedule:
0 7 * * * - Shell pipeline: Everything after the five Cron fields.
2. Explain the Cron schedule
0 7 * * *
Cron uses five fields:
| Field | Value | Meaning |
|---|---|---|
| Minute | 0 | At minute 0 |
| Hour | 7 | At 7 AM |
| Day | * | Every day of the month |
| Month | * | Every month |
| Weekday | * | Every day of the week |
Execution schedule
Every day at 07:00 AM.
For example:
- Monday: 07:00
- Tuesday: 07:00
- Wednesday: 07:00
- Saturday: 07:00
3. Explain the Linux commands
Let’s assume /var/log/auth.log contains these SSH authentication records:
Nov 06 01:10:12 ubuntu sshd[1234]: Failed password for root from 192.168.100.50 port 52341 ssh2
Nov 06 01:11:15 ubuntu sshd[1235]: Failed password for admin from 192.168.100.50 port 52342 ssh2
Nov 06 01:12:20 ubuntu sshd[1236]: Failed password for invalid user test from 192.168.100.60 port 52343 ssh2
Nov 06 01:15:30 ubuntu sshd[1237]: Failed password for root from 192.168.100.50 port 52344 ssh2
Nov 06 01:16:30 ubuntu sshd[1238]: Failed password for admin from 192.168.100.70 port 52345 ssh2
Step 1: Find failed SSH login attempts
grep "Failed password" /var/log/auth.log
Explanation:
grep: Searches text for a matching pattern."Failed password": Searches for failed SSH authentication messages./var/log/auth.log: Ubuntu authentication log file.
Output:
Nov 06 01:10:12 ubuntu sshd[1234]: Failed password for root from 192.168.100.50 port 52341 ssh2
Nov 06 01:11:15 ubuntu sshd[1235]: Failed password for admin from 192.168.100.50 port 52342 ssh2
...
Step 2: Extract the source IP address
awk '{print $(NF-3)}'
This is an important part of the script.
Understanding awk:
awk: Text-processing tool.$1: First column.$2: Second column.$NF: Last column.NF: Number of fields (columns).$(NF-3): Third field from the end, using a zero-based offset from the last field.
For this log:
Nov 06 01:10:12 ubuntu sshd[1234]: Failed password for root from 192.168.100.50 port 52341 ssh2
The final fields are:
| Position | Value |
|---|---|
| NF-5 | from |
| NF-4 | 192.168.100.50 |
| NF-3 | port |
| NF-2 | 52341 |
| NF-1 | ssh2 |
Important correction
The original command uses $(NF-3), which extracts port, NOT the IP address.
To extract the IP address correctly from this example, use:
awk '{print $(NF-4)}'
Step 3: Sort the IP addresses
sort
Sorts the extracted IP addresses alphabetically.
Example:
192.168.100.50
192.168.100.50
192.168.100.60
192.168.100.50
192.168.100.70
After sorting:
192.168.100.50
192.168.100.50
192.168.100.50
192.168.100.60
192.168.100.70
Step 4: Count duplicate IP addresses
uniq -c
uniq: Removes consecutive duplicate lines.-c: Counts how many times each line appears.
Output:
3 192.168.100.50
1 192.168.100.60
1 192.168.100.70
Why do we need sort before uniq?
Because uniq only detects consecutive duplicate lines.
Step 5: Sort by failed login count
sort -rn
Explanation:
| Option | Meaning |
|---|---|
-r | Reverse order (descending) |
-n | Numeric sorting |
This puts IP addresses with the highest failed login counts at the top.
Step 6: Save the report
> /tmp/ssh_fail.txt
>: Redirects output into a file./tmp/ssh_fail.txt: Destination file.- If the file exists, its previous contents are overwritten.
- If it doesn’t exist, Linux creates it.
4. Complete execution flow
Cron Job
Every day at 07:00 AM
grep "Failed password"
Find failed SSH login records
awk '{print $(NF-4)}'
Extract source IP addresses
sort | uniq -c
Group IPs and count occurrences
sort -rn
Rank by failed login count
/tmp/ssh_fail.txt
SSH failed login report
5. Corrected production version
I recommend using this corrected command:
0 7 * * * grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn > /tmp/ssh_fail.txt
However, there is an important detail: for standard Ubuntu OpenSSH log messages, the IP is normally the fourth field from the end (NF-3), not the fifth. Let’s verify:
... from 192.168.100.50 port 52341 ssh2
- NF-3 =
192.168.100.50 - NF-4 =
from
Therefore, your original $(NF-3) is actually correct for the standard log format.
The earlier correction to NF-4 would be incorrect. This is why inspecting the actual log format is important.
6. Example report
Your /tmp/ssh_fail.txt might look like this:
15 192.168.100.50
8 192.168.100.60
3 192.168.100.70
1 192.168.100.80
Interpretation:
| Source IP | Failed attempts |
|---|---|
| 192.168.100.50 | 15 |
| 192.168.100.60 | 8 |
| 192.168.100.70 | 3 |
| 192.168.100.80 | 1 |
This helps a SysAdmin identify potential SSH brute-force attacks.
7. Practical SysAdmin exercises
Try these commands on your Ubuntu server:
Exercise 1 – Count all failed SSH attempts
grep -c "Failed password" /var/log/auth.log
Exercise 2 – Display the top 10 attacking IPs
grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -rn | head -10
Exercise 3 – Find successful SSH logins
grep "Accepted password" /var/log/auth.log
Exercise 4 – Monitor SSH logs in real time
tail -f /var/log/auth.log | grep "Failed password"
Exercise 5 – Identify repeated SSH attacks (more than 10 attempts)
grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | awk '$1 > 10'