Linux Log Analysis
Log analysis is crucial for system administration, troubleshooting, and security monitoring. Linux provides powerful command-line tools for processing, analyzing, and monitoring log files efficiently.
Overview
Linux systems generate extensive logs that provide insights into system behavior, errors, security events, and performance. Key tools for log analysis include:
- grep - Pattern matching and filtering
- awk - Field processing and calculations
- sed - Text transformation
- tail - Real-time monitoring
- journalctl - Systemd journal analysis
- sort/uniq - Data organization and counting
- cut - Column extraction
Common Linux Log Files
| Log File | Description | Content |
|---|---|---|
/var/log/syslog |
System messages | General system activity and messages |
/var/log/auth.log |
Authentication logs | Login attempts, sudo usage, SSH connections |
/var/log/kern.log |
Kernel messages | Kernel events, hardware issues, driver messages |
/var/log/apache2/access.log |
Apache access log | Web server requests and responses |
/var/log/apache2/error.log |
Apache error log | Web server errors and warnings |
/var/log/mail.log |
Mail server logs | Email server activity and errors |
/var/log/cron.log |
Cron job logs | Scheduled task execution |
Basic Log Analysis
Searching and Filtering
Search for specific patterns
Finds all lines containing "ERROR"
Case-insensitive search
Searches for "error" regardless of case
Search with context
Shows 3 lines before and after each match
Multiple pattern search
Searches for multiple patterns
Exclude patterns
Shows all lines except those containing "INFO"
Time-based Analysis
Filter by date
Shows entries for January 21st
Filter by time range
Shows entries between 10:00 and 11:00
Recent entries
Shows last 100 log entries
Real-time Log Monitoring
Following Log Files
Monitor single log file
Continuously displays new log entries
Monitor multiple log files
Monitors multiple files simultaneously
Monitor with filtering
Shows only error messages in real-time
Monitor with highlighting
Highlights error and warning messages
Advanced Analysis with AWK
Field Processing
Extract specific fields
Extracts timestamp and process information
Count log levels
Counts different log levels
Analyze by hour
Groups log entries by hour
Top processes by log entries
Shows processes generating most log entries
Statistical Analysis
Calculate log entry frequency
Shows log frequency per minute
Average log entries per hour
Calculates average log entries per hour
Web Server Log Analysis
Apache Access Log Analysis
Top IP addresses
Shows top 10 IP addresses by request count
Most requested pages
Shows most frequently requested URLs
HTTP status code analysis
Counts HTTP status codes
404 errors analysis
Shows most common 404 errors
Bandwidth analysis
Calculates total bandwidth usage
Error Log Analysis
Error frequency by type
Groups errors by type
Security Log Analysis
Authentication Analysis
Failed login attempts
Shows IP addresses with failed login attempts
Successful logins
Shows successful login details
SSH connection analysis
Analyzes SSH connection sources
Sudo usage tracking
Tracks sudo command usage
Systemd Journal Analysis
Basic journalctl Usage
View recent entries
Shows last 50 journal entries
Follow journal in real-time
Continuously displays new journal entries
Filter by service
Shows entries for Apache service
Filter by priority
Shows only error-level messages
Time range filtering
Shows entries within specific time range
Advanced Journal Analysis
Boot analysis
Shows entries from previous boot
Kernel messages
Shows kernel messages only
JSON output for processing
Outputs journal in JSON format for processing
Log Rotation and Management
Analyzing Rotated Logs
Search across rotated logs
Searches compressed and uncompressed log files
Combine multiple log files
Combines current and previous log files
Log Size Analysis
Check log file sizes
Shows size of all log files
Find largest log files
Lists 10 largest log files
Automated Log Analysis Scripts
Daily Log Summary Script
Example script for daily log analysis
Alert Script for Critical Events
Real-time alerting for critical log events
Best Practices
Log Analysis Best Practices
- Regular Monitoring - Set up automated log monitoring and alerting
- Log Retention - Maintain appropriate log retention policies
- Centralized Logging - Use tools like rsyslog or ELK stack for centralization
- Time Synchronization - Ensure accurate timestamps with NTP
- Log Rotation - Implement proper log rotation to manage disk space
- Security - Protect log files from unauthorized access and tampering
Performance Tips
Optimizing Log Analysis Performance
- Use Specific Patterns - More specific grep patterns are faster
- Limit Search Scope - Use time ranges and specific log files
- Pipeline Efficiently - Order commands to filter early in the pipeline
- Index Large Logs - Consider tools like grep with -F for fixed strings
- Compress Old Logs - Use zgrep for compressed log files
Common Use Cases
Typical Log Analysis Scenarios
- Troubleshooting - Finding root cause of system issues
- Security Monitoring - Detecting intrusion attempts and anomalies
- Performance Analysis - Identifying bottlenecks and resource issues
- Compliance - Meeting audit and regulatory requirements
- Capacity Planning - Understanding usage patterns and growth