Checking log files in Linux helps diagnose system issues, track errors, monitor activity, and verify service behavior. The core commands needed are built into almost all distributions, so no extra tools are required—just terminal access with appropriate permissions (often requiring sudo for system logs).

What You'll Need

  • Access to a Linux terminal (local or via SSH)
  • Basic familiarity with command-line navigation
  • Occasional use of sudo for protected log directories (e.g., /var/log/)

Step-by-Step

  1. Open a terminal on your Linux system. If you're accessing a remote server, use SSH: ssh username@server_ip.
  2. Locate the log directory. Most system logs reside in /var/log/. List its contents with ls /var/log/. Common files include syslog, auth.log, kern.log, dpkg.log, and distribution-specific entries like journalctl entries (managed by systemd).
  3. View the most recent system log using less for safe, scrollable reading: sudo less /var/log/syslog (on Debian/Ubuntu) or sudo less /var/log/messages (on RHEL/CentOS/Fedora). Press q to quit.
  4. Follow logs in real time (e.g., to watch live system activity or startup messages) with tail -f: sudo tail -f /var/log/syslog. Press Ctrl+C to stop following.
  5. Filter logs for specific keywords using grep. For example, to find all SSH-related entries: sudo grep 'ssh' /var/log/auth.log. Combine with tail for recent matches: sudo grep 'error' /var/log/syslog | tail -n 20.
  6. Use journald for systemd-managed systems (most modern distros). View all recent logs: journalctl. To follow logs live: journalctl -f. Filter by service: journalctl -u ssh.service. Limit entries by time: journalctl --since '2 hours ago'.
  7. Check application-specific logs. Services like Apache (/var/log/apache2/ or /var/log/httpd/), MySQL (/var/log/mysql/), and Docker (/var/log/docker.log or docker logs container_name) have dedicated directories or commands. For Docker: docker logs -f my-app-container.
  8. Export or rotate logs for analysis. If a log is large, extract a snapshot: sudo cp /var/log/syslog ~/syslog-backup.txt. To compress for storage: sudo gzip /var/log/syslog (creates syslog.gz).

Common Mistakes

  • Skipping sudo when accessing protected logs (e.g., /var/log/auth.log), which returns Permission denied errors. Always try sudo first if file access fails.
  • Confusing log locations between distributions. For example, /var/log/messages exists on RHEL-based systems but not Ubuntu; Ubuntu uses /var/log/syslog as the main system log.
  • Using cat on large logs without pagination. cat /var/log/syslog floods your terminal and may hang the display. Prefer less, head, or tail.
  • Ignoring timestamp formats in journalctl queries. Use ISO 8601 or relative terms like '1 hour ago' to avoid parsing errors.
  • Overlooking compressed logs. Old logs are often archived as .gz (e.g., syslog.1.gz). View them with zcat /var/log/syslog.1.gz | grep 'error' or zless.

Tips

  • Use tail -n 50 to see the last 50 lines of a log instead of scrolling manually.
  • Combine grep options for better precision: grep -i 'error' (case-insensitive), grep -v 'debug' (exclude lines), or grep -E 'error|fail' (regex OR).
  • Check log rotation status with logrotate -d /etc/logrotate.conf (dry-run mode) to verify logs are being archived correctly.
  • Set up persistent aliases in ~/.bashrc for speed: alias syslogs='sudo less /var/log/syslog'.
  • For containerized apps, use docker ps to list containers first, then docker logs [container_id] to avoid typos.
  • Monitor disk space before large log exports: df -h /var/log to avoid filling the partition during troubleshooting.

Troubleshooting

If your log-checking steps fail, follow this sequence:

  1. Verify file existence. Run ls -l /var/log/syslog (or your target file). If missing, the log may be disabled. Check if the service generating it is running (e.g., systemctl status rsyslog).
  2. Check service status. On systemd systems, rsyslog or systemd-journald must be active. Start them with sudo systemctl start rsyslog (Debian/Ubuntu) or sudo systemctl start syslog (some RHEL variants).
  3. Confirm user permissions. Add your user to the adm group: sudo usermod -aG adm $USER, then log out and back in.
  4. Test with journald if traditional logs are absent: journalctl -b shows logs from the current boot. If this works but /var/log/syslog is empty, rsyslog may be misconfigured.
  5. Inspect configuration files. For rsyslog, check /etc/rsyslog.conf and /etc/rsyslog.d/ for syntax errors. Validate with rsyslogd -N1.
  6. Handle permission issues with strace (advanced): sudo strace -e openat less /var/log/syslog to see where access fails.

Frequently Asked Questions

What is the fastest way to see recent errors in Linux?

Run sudo journalctl -p err -n 20. This shows the last 20 error-level messages from journald. For traditional logs: sudo grep -i 'error\|fail' /var/log/syslog | tail -n 20.

How do I check logs for a specific service like Apache?

First, locate the service log directory: ls /var/log/apache2/ (Ubuntu) or ls /var/log/httpd/ (RHEL). Then view access and error logs: tail -f /var/log/apache2/access.log and tail -f /var/log/apache2/error.log.

Can I view logs without sudo?

Yes—for user-specific logs like ~/.xsession-errors or application logs in your home directory. System-wide logs (e.g., /var/log/syslog) require sudo or group membership in adm.

Why does tail -f stop showing new lines?

This often happens if log rotation moved or compressed the file while you were following it. Restart tail -f on the current file, or use tail --follow=name to track the filename even after rotation.

Where are Docker container logs stored on the host?

Docker stores container logs in /var/lib/docker/containers/[container_id]/[container_id]-json.log, but you rarely need to access this directly. Use docker logs [container_name] instead for safe reading.

How do I clear old logs to free space?

Use journalctl --vacuum-time=7d to delete logs older than 7 days. For traditional logs, rotate with logrotate -f /etc/logrotate.conf or manually delete archived files (e.g., rm /var/log/syslog.2.gz), but avoid removing active logs like syslog.