How to View Linux Log Files
In this article
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/logare readable by all users, but some (like auth logs) may requiresudo. - Basic familiarity with the terminal (typing commands, using
manpages).
Step-by-Step
- Open a terminal. Use your desktop's terminal emulator or switch to a virtual console (Ctrl+Alt+F2).
- Know where logs live. The standard directory is
/var/log. List its contents withls /var/log. Common files includesyslogormessages(general system logs),auth.log(authentication attempts),kern.log(kernel messages), anddmesg(kernel ring buffer). - View a log file with
catorless. For a quick look, usecat /var/log/syslog(prints the whole file to the terminal). For large files, useless /var/log/syslog– this allows you to scroll with arrow keys, search with/pattern, and quit withq. - Read the newest entries with
tail.tail /var/log/syslogshows the last 10 lines. To see more, usetail -n 50 /var/log/syslog. To watch the file in real time (like a live feed), usetail -f /var/log/syslog. Press Ctrl+C to stop. - Search for specific terms with
grep. Instead of scrolling through thousands of lines, filter:grep "error" /var/log/syslog. Combine withtailto search the most recent entries:tail -n 100 /var/log/syslog | grep "error". Usegrep -ito ignore case, andgrep -Efor extended regex. - Use
journalctlon systemd-based systems. Most modern distros use systemd, which centralizes logs. Runjournalctlto view the entire log. Filter by boot:journalctl -b(current boot) orjournalctl -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). - Check the kernel ring buffer with
dmesg. Kernel messages (hardware, drivers) are in a ring buffer. Rundmesgto see them. Usedmesg -Hfor human-readable timestamps,dmesg -wto follow, anddmesg | grep -i usbto filter by topic. - Examine application-specific logs. Many applications write to
/var/logor their own directories. For example, Apache logs are in/var/log/apache2/access.loganderror.log; Nginx in/var/log/nginx/. Use the same commands on these files. - Use
logrotateto understand log rotation. Logs are rotated (archived) to prevent them from growing unbounded. Check/etc/logrotate.confand/etc/logrotate.d/to see the schedule. Old logs often have extensions like.1,.gz, or.old. To read a compressed log, usezcat /var/log/syslog.2.gzorzgrepto search inside it.
Common Mistakes
- Using
caton huge files – it floods the terminal and can freeze your session. Always uselessortailfor large logs. - Forgetting to use
sudo– some logs (likeauth.logorbtmp) are restricted. If you get a "Permission denied" error, prefix withsudo. - Ignoring log rotation – searching only the current file will miss older events. Check rotated files with
ls -la /var/logto see all versions. - Using
tail -fwithout understanding it – it blocks the terminal. Press Ctrl+C to exit, or usetimeout 5 tail -fto auto-stop after 5 seconds. - Assuming all logs are in plain text – some are binary (like
wtmp). Uselastorutmpdumpfor those, notcat.
Tips
- Combine commands with pipes –
journalctl -u ssh | grep -i fail | tail -n 20gives you the last 20 failed SSH attempts. - Use
watchto 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.bashrcto quickly jump to the most recent logs. - Learn
journalctltime filters –journalctl --since "1 hour ago"or--since todayare powerful for pinpointing issues. - Use
lnavif 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.,journaldonly). Tryjournalctl -u service-name. - Permission denied – Use
sudo. If you still get denied, check the file's permissions withls -land consider adding your user to theadmgroup (on Debian/Ubuntu) viasudo 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. Runjournalctl -u service-nameto 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 withtimedatectl. Forsyslog, the timestamp is usually local, but if not, you can convert mentally or usedate -d. - Logs are too verbose – Use
grep -vto 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.