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
sudofor protected log directories (e.g.,/var/log/)
Step-by-Step
- Open a terminal on your Linux system. If you're accessing a remote server, use SSH:
ssh username@server_ip. - Locate the log directory. Most system logs reside in
/var/log/. List its contents withls /var/log/. Common files includesyslog,auth.log,kern.log,dpkg.log, and distribution-specific entries likejournalctlentries (managed by systemd). - View the most recent system log using
lessfor safe, scrollable reading:sudo less /var/log/syslog(on Debian/Ubuntu) orsudo less /var/log/messages(on RHEL/CentOS/Fedora). Pressqto quit. - Follow logs in real time (e.g., to watch live system activity or startup messages) with
tail -f:sudo tail -f /var/log/syslog. PressCtrl+Cto stop following. - Filter logs for specific keywords using
grep. For example, to find all SSH-related entries:sudo grep 'ssh' /var/log/auth.log. Combine withtailfor recent matches:sudo grep 'error' /var/log/syslog | tail -n 20. - Use
journaldfor 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'. - Check application-specific logs. Services like Apache (
/var/log/apache2/or/var/log/httpd/), MySQL (/var/log/mysql/), and Docker (/var/log/docker.logordocker logs container_name) have dedicated directories or commands. For Docker:docker logs -f my-app-container. - 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(createssyslog.gz).
Common Mistakes
- Skipping
sudowhen accessing protected logs (e.g.,/var/log/auth.log), which returnsPermission deniederrors. Always trysudofirst if file access fails. - Confusing log locations between distributions. For example,
/var/log/messagesexists on RHEL-based systems but not Ubuntu; Ubuntu uses/var/log/syslogas the main system log. - Using
caton large logs without pagination.cat /var/log/syslogfloods your terminal and may hang the display. Preferless,head, ortail. - Ignoring timestamp formats in
journalctlqueries. 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 withzcat /var/log/syslog.1.gz | grep 'error'orzless.
Tips
- Use
tail -n 50to see the last 50 lines of a log instead of scrolling manually. - Combine
grepoptions for better precision:grep -i 'error'(case-insensitive),grep -v 'debug'(exclude lines), orgrep -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
~/.bashrcfor speed:alias syslogs='sudo less /var/log/syslog'. - For containerized apps, use
docker psto list containers first, thendocker logs [container_id]to avoid typos. - Monitor disk space before large log exports:
df -h /var/logto avoid filling the partition during troubleshooting.
Troubleshooting
If your log-checking steps fail, follow this sequence:
- 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). - Check service status. On systemd systems,
rsyslogorsystemd-journaldmust be active. Start them withsudo systemctl start rsyslog(Debian/Ubuntu) orsudo systemctl start syslog(some RHEL variants). - Confirm user permissions. Add your user to the
admgroup:sudo usermod -aG adm $USER, then log out and back in. - Test with
journaldif traditional logs are absent:journalctl -bshows logs from the current boot. If this works but/var/log/syslogis empty,rsyslogmay be misconfigured. - Inspect configuration files. For
rsyslog, check/etc/rsyslog.confand/etc/rsyslog.d/for syntax errors. Validate withrsyslogd -N1. - Handle permission issues with
strace(advanced):sudo strace -e openat less /var/log/syslogto 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.