Smart Systems for Home

Plain how-to guides for smart home, security and home Wi-Fi

How to View Linux Log Files

· 6 min read · Home Tech

In this article
  1. What You'll Need
  2. Step-by-Step
  3. Common Mistakes
  4. Tips
  5. Troubleshooting
  6. Frequently Asked Questions

Linux logs are the system's memory: they record everything from kernel messages and service errors to user logins and application output. Knowing how to read them is essential for troubleshooting, monitoring system health, and understanding what your machine is doing. This guide walks you through the most common commands and tools to view, filter, and follow logs on any modern Linux distribution.

What You'll Need

  • A Linux system (any distribution, e.g., Ubuntu, Debian, Fedora, CentOS) with a terminal.
  • A user account with read access to log files. Most logs in /var/log are readable by all users, but some (like auth logs) may require sudo.
  • Basic familiarity with the terminal (typing commands, using man pages).

Step-by-Step

  1. Open a terminal. Use your desktop's terminal emulator or switch to a virtual console (Ctrl+Alt+F2).
  2. Know where logs live. The standard directory is /var/log. List its contents with ls /var/log. Common files include syslog or messages (general system logs), auth.log (authentication attempts), kern.log (kernel messages), and dmesg (kernel ring buffer).
  3. View a log file with cat or less. For a quick look, use cat /var/log/syslog (prints the whole file to the terminal). For large files, use less /var/log/syslog – this allows you to scroll with arrow keys, search with /pattern, and quit with q.
  4. Read the newest entries with tail. tail /var/log/syslog shows the last 10 lines. To see more, use tail -n 50 /var/log/syslog. To watch the file in real time (like a live feed), use tail -f /var/log/syslog. Press Ctrl+C to stop.
  5. Search for specific terms with grep. Instead of scrolling through thousands of lines, filter: grep "error" /var/log/syslog. Combine with tail to search the most recent entries: tail -n 100 /var/log/syslog | grep "error". Use grep -i to ignore case, and grep -E for extended regex.
  6. Use journalctl on systemd-based systems. Most modern distros use systemd, which centralizes logs. Run journalctl to view the entire log. Filter by boot: journalctl -b (current boot) or journalctl -b -1 (previous boot). Filter by service: journalctl -u ssh. Follow live: journalctl -f. Show only errors: journalctl -p err (priority levels: emerg, alert, crit, err, warning, notice, info, debug).
  7. Check the kernel ring buffer with dmesg. Kernel messages (hardware, drivers) are in a ring buffer. Run dmesg to see them. Use dmesg -H for human-readable timestamps, dmesg -w to follow, and dmesg | grep -i usb to filter by topic.
  8. Examine application-specific logs. Many applications write to /var/log or their own directories. For example, Apache logs are in /var/log/apache2/access.log and error.log; Nginx in /var/log/nginx/. Use the same commands on these files.
  9. Use logrotate to understand log rotation. Logs are rotated (archived) to prevent them from growing unbounded. Check /etc/logrotate.conf and /etc/logrotate.d/ to see the schedule. Old logs often have extensions like .1, .gz, or .old. To read a compressed log, use zcat /var/log/syslog.2.gz or zgrep to search inside it.

Common Mistakes

  • Using cat on huge files – it floods the terminal and can freeze your session. Always use less or tail for large logs.
  • Forgetting to use sudo – some logs (like auth.log or btmp) are restricted. If you get a "Permission denied" error, prefix with sudo.
  • Ignoring log rotation – searching only the current file will miss older events. Check rotated files with ls -la /var/log to see all versions.
  • Using tail -f without understanding it – it blocks the terminal. Press Ctrl+C to exit, or use timeout 5 tail -f to auto-stop after 5 seconds.
  • Assuming all logs are in plain text – some are binary (like wtmp). Use last or utmpdump for those, not cat.

Tips

  • Combine commands with pipes – journalctl -u ssh | grep -i fail | tail -n 20 gives you the last 20 failed SSH attempts.
  • Use watch to refresh automatically – watch 'grep error /var/log/syslog' updates the output every 2 seconds.
  • Set up aliases – add alias logs='cd /var/log && ls -lt' to your .bashrc to quickly jump to the most recent logs.
  • Learn journalctl time filters – journalctl --since "1 hour ago" or --since today are powerful for pinpointing issues.
  • Use lnav if installed – a terminal log viewer that colorizes and groups entries, making it easier to spot errors.

Troubleshooting

  • Log file is empty or missing – Check if the service is running (systemctl status service-name). If the file doesn't exist, the service may not have started or logs are written elsewhere (e.g., journald only). Try journalctl -u service-name.
  • Permission denied – Use sudo. If you still get denied, check the file's permissions with ls -l and consider adding your user to the adm group (on Debian/Ubuntu) via sudo usermod -aG adm $USER, then log out and back in.
  • Logs are not updating – Verify the service is writing to that file (check with lsof | grep /var/log/name). If using systemd, the service may log to journald instead. Run journalctl -u service-name to see if entries exist.
  • Timestamps are in UTC – Many logs use UTC by default. To see local time, use journalctl --utc (to force UTC) or check your system timezone with timedatectl. For syslog, the timestamp is usually local, but if not, you can convert mentally or use date -d.
  • Logs are too verbose – Use grep -v to exclude noise: tail -f /var/log/syslog | grep -v "systemd".

Frequently Asked Questions

What is the difference between syslog and journald?

syslog is a traditional logging protocol that writes to files like /var/log/syslog. journald is systemd's structured logging system that stores logs in a binary format and can be read with journalctl. Many systems run both, with journald forwarding to syslog.

How do I view logs from a specific time range?

For journalctl, use --since and --until: journalctl --since "2025-03-01 10:00" --until "2025-03-01 12:00". For text logs, use grep with a date pattern, but it's easier to use journalctl.

Can I view logs without root access?

Yes, most logs in /var/log are world-readable. If a specific log is restricted, you can ask your admin to add you to the appropriate group (like adm or systemd-journal).

How do I clear a log file?

Use sudo truncate -s 0 /var/log/syslog or sudo journalctl --vacuum-time=1d to remove journal entries older than one day. Be careful – clearing logs can hamper future troubleshooting.

What does "log rotation" mean?

Log rotation is the automatic archiving and compression of log files to prevent them from consuming all disk space. Tools like logrotate rename the current log to log.1, then start a fresh one, and optionally compress old ones.

Why do some logs show a different timezone?

Logs often use UTC (Coordinated Universal Time) to avoid ambiguity across time zones. You can adjust your system's timezone with sudo timedatectl set-timezone Your/Zone, but existing logs will still show the timezone they were written in.