Use dmesg, journalctl, and tail -f /var/log/syslog to check Linux logs—these commands instantly reveal hardware errors, service crashes, or authentication failures. For deeper troubleshooting, combine them with filters like grep or awk. Below, we break down the exact steps, common pitfalls, and how to interpret log messages without confusion.
What You'll Need
- A terminal with root or sudo privileges (most commands require elevation)
- Basic familiarity with Linux commands (e.g.,
ls,cd) - The specific log file or service you’re troubleshooting (e.g.,
/var/log/auth.logfor authentication) - Optional:
greporawkfor parsing logs (included by default on most distros)
Step-by-Step: How to Check Linux Logs
-
Identify the log type. Linux logs are stored in various files and sysems:
- Kernel logs (hardware/driver issues):
/var/log/dmesgordmesg - System logs (general errors/startup):
/var/log/syslog - Authentication logs (failed logins):
/var/log/auth.log(Ubuntu/Debian) or/var/log/secure(RHEL/CentOS) - Application logs (e.g., Apache/Nginx): Check service-specific directories like
/var/log/apache2/ (modern distros): journalctl(replaces oldersyslogon many systems)
Note: Paths vary slightly by distro. Use
find /var/log -type f -name "*.log"to locate all logs. - Kernel logs (hardware/driver issues):
-
Check kernel logs for hardware issues (run as root):
sudo dmesg | grep -i errorThis filters for errors in the kernel ring buffer. For real-time monitoring:
sudo tail -f /var/log/kern.logReplace
kern.logwithdmesgif the file doesn’t exist. -
Inspect system-wide logs with
journalctl(systemd distros only):journalctl -xe-x: Show expanded output with explanations-p 3: Filter for errors (priority 3)--since "2024-01-01": Limit to a date range
For a specific service (e.g.,
nginx):journalctl -u nginx --no-pager -
Read traditional syslog files (older distros):
cat /var/log/syslog | grep -i "failed"Common syslog files:
/var/log/auth.log(Ubuntu/Debian): Failed logins, sudo attempts/var/log/secure(RHEL/CentOS): Similar to auth.log/var/log/messages: General system events
-
Tail logs in real-time (for ongoing issues):
sudo tail -f /var/log/auth.logUse
Ctrl+Cto exit. Add| grep "service_name"to filter (e.g.,sudo tail -f /var/log/nginx/error.log | grep 500). -
Search logs for keywords:
grep "permission denied" /var/log/syslogFor recurring issues, combine with
awk:grep "error" /var/log/nginx/error.log | awk '{print $1, $2}' | sort | uniq -cThis counts errors by date/time.
-
Check logs for a specific service:
systemctl status apache2Then inspect its log directory (e.g.,
/var/log/apache2/error.log). For systemd services:journalctl -u.service -
Archive old logs (optional):
sudo logrotate /etc/logrotate.confMost distros auto-rotate logs, but verify with
ls -l /var/log/.
Common Mistakes
- Ignoring log timestamps: Always check the date/time of errors. A 2023 log won’t help a 2024 issue.
-
Assuming all logs are in
/var/log: Application logs (e.g., Docker, PostgreSQL) may be elsewhere (e.g.,/var/lib/docker/containers/). -
Overlooking permissions: You’ll get
Permission deniedif checking logs withoutsudo. Always usesudounless the file is readable by your user. -
Not filtering logs: Raw log files are overwhelming. Use
grep,journalctl -p 3, ortail -n 50to focus on critical lines. -
Assuming
syslogexists on all systems: Modern distros (e.g., Ubuntu 22.04+) rely onjournalctl. Check withwhich journalctl. - Copy-pasting errors without context: A single log line may not explain the issue. Read 5–10 lines before and after for root cause.
Tips for Effective Log Checking
-
Use
lessfor interactive browsing:sudo less /var/log/syslogNavigate with arrow keys; press
/to search,qto quit. -
Set up log monitoring alerts:
sudo journalctl --no-pager | grep -m 1 "CRITICAL"Add to
cronfor daily checks:0 3 * * * sudo grep "error" /var/log/syslog >> /tmp/log_errors.txt -
Check logs remotely with SSH:
ssh user@server "sudo journalctl -u nginx --no-pager" -
Correlate logs with processes:
ps aux | grep nginxsudo journalctl -u nginx --since "2024-01-01 08:00:00"Match the process ID in logs to active services.
-
Use
logwatchfor automated summaries (install via package manager):sudo apt install logwatchsudo logwatch --service all -
Document recurring errors: Create a text file (e.g.,
/root/log_issues.md) to track patterns over time.
Troubleshooting: When Logs Don’t Help
If logs show nothing useful or the issue persists:
-
Check system resources:
topfree -h df -hLow memory, disk space, or CPU can mask errors in logs.
-
Test with a minimal environment:
sudo systemctl isolate multi-user.targetReboot into a text-only mode to rule out GUI-related issues.
-
Update the system:
sudo apt update && sudo apt upgrade -yBugs may be fixed in newer kernel/logging packages.
-
Reinstall the problematic service:
sudo apt purge nginx && sudo apt install nginxReplace
nginxwith your service name. This clears corrupted configs. -
Check for conflicting services:
systemctl list-units --type=service --state=failedFailed services may hijack ports or resources.
-
Isolate the issue:
sudo journalctl --list-bootsCompare logs from a working boot to a failing one:
sudo journalctl -b -1 --no-pager -
Enable debugging for the service:
sudo nginx -T | grep debugFor Apache, edit
/etc/apache2/apache2.confand add:LogLevel debugThen restart the service.
-
Check hardware:
sudo smartctl -a /dev/sdaReplace
/dev/sdawith your disk. Disk errors often appear in kernel logs but may not be logged elsewhere.
Frequently Asked Questions
How do I check logs for a specific user?
Use grep with the username in auth logs:
sudo grep "user_name" /var/log/auth.log
For systemd journals:
journalctl -u sshd --user=username
Why do my logs show "kernel panic" but no errors?
A kernel panic halts the system before logging. Reboot and check:
dmesg | grep -i "panic\|oom"
Look for Out of Memory (OOM) killer messages or driver crashes. Enable CONFIG_PANIC_TIMEOUT=0 in the kernel config to prevent automatic reboots for debugging.
Can I redirect logs to a remote server?
Yes, use rsyslog or syslog-ng:
sudo apt install rsyslog
sudo nano /etc/rsyslog.conf
Add:
*.* action(type="omfwd" Target="remote_log_server" Port="514" Protocol="tcp")
Restart rsyslog:
sudo systemctl restart rsyslog
How do I clear old logs?
Use logrotate (automatic) or manually:
sudo rm /var/log/syslog
sudo touch /var/log/syslog
sudo chmod 640 /var/log/syslog
For journalctl, clear with:
sudo journalctl --vacuum-time=7d
This retains logs for 7 days.
What’s the difference between dmesg and journalctl?
dmesg shows kernel messages from the ring buffer (hardware/driver events). journalctl captures all systemd logs (services, users, etc.). Use dmesg for hardware issues; journalctl for software/services.
How do I check logs for a specific time range?
For journalctl:
journalctl --since "2024-01-01 08:00" --until "2024-01-01 09:00"
For syslog:
sudo grep "error" /var/log/syslog | grep -E "Jan 1 08|Jan 1 09"
Use --since and --until with dates/times in journalctl.