Every Linux server writes thousands of log lines a day, and most of them are noise. When something breaks at 3 a.m., you do not have time to read 5,000 lines of journalctl output. AI log analysis solves this problem: you pipe raw logs into a large language model (LLM) and get back a short summary of what failed, when it started, and what probably caused it. In this guide you will build a small, safe AI log analysis workflow on Ubuntu using the command line, so you can triage incidents in minutes instead of hours.
Why AI Log Analysis Works for Sysadmins
Traditional log analysis tools such as grep, awk and logwatch are excellent at counting and filtering, but they cannot explain context. An LLM can group related errors, ignore repetitive noise, and describe a failure chain in plain English. AI log analysis is especially useful for:
- Summarizing authentication logs to spot brute-force SSH attacks.
- Explaining cryptic kernel, systemd or application stack traces.
- Producing a first incident timeline before you start deep debugging.
- Comparing “normal” log patterns with the hour when an outage happened.
The golden rule: an LLM summary is a starting point for investigation, never proof. Always verify the important claims against the raw logs.
Prerequisites
- An Ubuntu 22.04 or 24.04 server with
sudoaccess. curlandjqinstalled (install withsudo apt install curl jq -y).- Either a local model through Ollama (recommended for sensitive logs) or an API key for a hosted LLM.
- Basic familiarity with
journalctland files in/var/log.
Security warning: logs can contain IP addresses, email addresses and occasionally secrets. Prefer a local model for production logs, or scrub sensitive data before sending anything to a cloud API.
Step 1: Collect a Focused Log Slice
LLMs have context limits, and huge inputs cost time and money. Always extract a focused slice instead of an entire log file. For system errors from the last hour:
journalctl -p err --since "1 hour ago" --no-pager | tail -n 300 > /tmp/errors.txt
For SSH authentication problems:
sudo grep -E "Failed|Invalid|Accepted" /var/log/auth.log | tail -n 200 > /tmp/auth.txt
For a crashed systemd service such as Nginx:
journalctl -u nginx --since "today" --no-pager | tail -n 200 > /tmp/nginx.txt
Step 2: Send the Logs to a Local LLM with Ollama
If you already run a local model (see our guide to self-hosting an LLM with Ollama), you can analyze logs without any data leaving your network. Build the JSON request safely with jq so quotes and newlines in the log are escaped correctly:
PROMPT="You are a Linux sysadmin assistant. Analyze these logs. List: 1) the most serious errors, 2) when the problem started, 3) the likely root cause, 4) the next three commands I should run to verify."
jq -n --arg model "llama3.1:8b" --arg prompt "$PROMPT" --rawfile logs /tmp/errors.txt \
'{model: $model, prompt: ($prompt + "\n\nLOGS:\n" + $logs), stream: false}' \
> /tmp/request.json
curl -s http://localhost:11434/api/generate -d @/tmp/request.json | jq -r '.response'
The model returns a structured summary: the failing service, the first error timestamp, and suggested verification commands. This is AI log analysis at its most practical — raw text in, actionable triage out.
Step 3: Use AI Log Analysis for SSH Attack Detection
Authentication logs are where AI log analysis really shines, because attack patterns are buried in repetition. Feed the auth slice with a security-focused prompt:
PROMPT="Analyze this SSH auth log. Count failed logins per IP, identify the most targeted usernames, and tell me if this looks like a brute-force attack. Suggest Fail2Ban or firewall actions."
Combine the answer with real data before acting. Verify the top offending IP yourself:
sudo grep "Failed password" /var/log/auth.log | awk '{print $(NF-3)}' | sort | uniq -c | sort -nr | head
If the IPs match the summary and you are not already protected, follow our guide to install Fail2Ban to protect SSH and block the worst offenders.

Step 4: Turn It into a Reusable Script
Save the workflow as /usr/local/bin/logtriage.sh so any team member can run it during an incident:
#!/usr/bin/env bash
# Usage: logtriage.sh <unit-or-file> <lines>
set -euo pipefail
SOURCE="${1:-system}"; LINES="${2:-200}"
TMP=$(mktemp)
if [ "$SOURCE" = "system" ]; then
journalctl -p warning --since "2 hours ago" --no-pager | tail -n "$LINES" > "$TMP"
else
journalctl -u "$SOURCE" --since "2 hours ago" --no-pager | tail -n "$LINES" > "$TMP"
fi
PROMPT="Summarize these Linux logs: main errors, start time, likely cause, next steps."
jq -n --arg model "llama3.1:8b" --arg prompt "$PROMPT" --rawfile logs "$TMP" \
'{model: $model, prompt: ($prompt + "\n\n" + $logs), stream: false}' \
| curl -s http://localhost:11434/api/generate -d @- | jq -r '.response'
rm -f "$TMP"
Make it executable with chmod +x /usr/local/bin/logtriage.sh and run logtriage.sh nginx 200 whenever a service misbehaves.
Troubleshooting
The model invents errors that are not in the log. Lower the input size, ask the model to quote the exact log line for every claim, and cross-check with grep. Smaller, focused slices hallucinate far less.
The request times out. Your log slice is too large for the model. Reduce to 100–200 lines, or filter first with grep -i error.
jq fails to build the request. Never paste raw logs into a JSON string by hand — quoting will break. Always use jq --rawfile as shown above.
Answers are too generic. Improve the prompt: state the service, the time window, and the exact output format you want (errors, timeline, cause, next steps).
Conclusion
AI log analysis turns the worst part of incident response — reading endless logs under pressure — into a two-minute triage step. Collect a focused slice with journalctl, send it to a local LLM through Ollama, and verify the summary before you act. Combined with solid basics like managing the journal size and proper monitoring, this small script will save you hours on every incident. Start with read-only summaries, keep sensitive logs on your own hardware, and treat the model as a fast junior analyst: helpful, quick, and always worth double-checking.