Watch or Monitor Linux Log Files in Real Time
In this article
To watch or monitor Linux log files in real time, use the tail -f command, which displays new lines as they are appended. For more advanced filtering and color-coded output, tools like journalctl -f (for systemd-based systems) and multitail provide additional capabilities.
Why Monitor Logs in Real Time?
Log files record events from the kernel, services, applications, and the system itself. Real-time monitoring lets you see what is happening as it happens, which is essential for troubleshooting active issues, detecting security incidents, and understanding application behavior. Instead of waiting for a problem to escalate, you can observe errors, warnings, and unusual patterns the moment they occur. This proactive approach saves time and helps maintain system stability.
The Basics: tail -f
The simplest and most widely used method is the tail command with the -f (follow) option. It prints the last few lines of a file and then continues to output new lines as they are written.
tail -f /var/log/syslog– Follows the system log.tail -f /var/log/nginx/access.log– Follows an Nginx access log.tail -n 100 -f /var/log/auth.log– Shows the last 100 lines before following.
To stop following, press Ctrl+C. The -f option works on any text file, making it a universal tool.
Following Multiple Files at Once
When you need to watch several logs simultaneously, tail -f can take multiple file names:
tail -f /var/log/syslog /var/log/auth.log /var/log/nginx/error.logHowever, output from different files is interleaved without clear separation. For better readability, use tail -F (capital F), which also handles log rotation by reopening files that are replaced. Alternatively, multitail displays each file in its own pane with color highlighting, making it easier to track multiple streams.
Using journalctl for systemd Logs
Modern Linux distributions use systemd, which centralizes logs in the journal. The journalctl command provides powerful real-time monitoring:
journalctl -f– Follows all journal entries.journalctl -f -u nginx– Follows logs for the nginx service.journalctl -f -p err– Shows only error-level messages.
You can combine options, such as journalctl -f -u ssh -u cron to follow multiple services. Journalctl also supports filtering by time, priority, and unit, giving you fine-grained control over what you see.
Filtering and Highlighting with grep and multitail
Raw log output can be noisy. Piping tail -f through grep filters for specific keywords:
tail -f /var/log/syslog | grep -i errorThis shows only lines containing "error" (case-insensitive). For more complex patterns, use extended regular expressions. multitail offers built-in filtering and color schemes, and it can merge multiple logs into one view while preserving file boundaries. It also supports interactive searching and highlighting, which is helpful when scanning for anomalies.
Common Problems and Misconceptions
A frequent mistake is using tail -f on a file that is rotated (e.g., syslog becomes syslog.1). The original file handle remains open, and you may miss new entries. Using tail -F solves this by detecting the file replacement. Another issue is assuming journalctl is available on all systems; older distributions or those without systemd use traditional log files in /var/log. Also, logs can be written in binary or compressed formats; tail only works on plain text. In those cases, use zcat or journalctl for appropriate handling.
Choosing the Right Tool
Your choice depends on your needs:
- Quick check:
tail -fis sufficient. - Multiple files:
multitailortail -Fwith clear labels. - Systemd systems:
journalctl -fprovides structured filtering. - Filtering keywords: Pipe
tail -ftogrep. - Persistent monitoring: Consider
lnav, which adds an interactive interface with search and history.
For long-term monitoring, you might want a log management tool like logrotate for file management and rsyslog for remote logging, but for immediate real-time viewing, the command-line tools are fast and reliable.
Safety and Permission Considerations
Most log files are readable only by root or users in the adm group. If you get a "Permission denied" error, use sudo or add your user to the appropriate group. Be cautious when following logs that contain sensitive information, such as authentication attempts or personal data. Avoid exposing log output in shared or public terminals. Also, remember that real-time monitoring does not replace log analysis; it is a snapshot of current activity.
Frequently Asked Questions
How do I stop tail -f?
Press Ctrl+C to terminate the command. This stops the follow mode and returns you to the shell prompt.
What is the difference between tail -f and tail -F?
tail -f follows the file descriptor, so if the file is rotated and a new file is created, it continues to follow the old, now-unused file. tail -F checks for file replacement and follows the new file by name, making it more robust for logs that rotate.
Can I monitor logs on a remote server?
Yes, you can use SSH to run tail -f on the remote machine. For example, ssh user@server "tail -f /var/log/syslog". This gives you real-time output in your local terminal.
How do I see only errors in real time?
Use journalctl -f -p err to show only error-level messages, or pipe tail -f through grep -i error to filter lines containing "error". You can also use multitail with a filter pattern.
Are there graphical tools for real-time log monitoring?
Yes, tools like gltail, lnav, and klog provide graphical or interactive interfaces. For web-based dashboards, you can use Kibana with Elasticsearch, but that requires a more complex setup.
Why does my log file not show new lines?
Check if the service is writing to a different file or if the log is buffered. Also, verify that you have the correct path and that the file is being appended to. Use ls -l to see if the file size is growing.