Monitoring system activity in Linux often starts with the `tail` command. If you have ever wondered how to tail logs in linux, you are in the right place because this guide covers everything from basic usage to advanced filtering.
Log files are the lifeblood of troubleshooting. They tell you what went wrong, when it happened, and sometimes why. The `tail` command is your best friend for reading these files in real time or checking the latest entries.
In this article, you will learn how to tail logs like a pro. We will cover simple commands, practical examples, and common mistakes to avoid. Let’s get started.
How To Tail Logs In Linux
Before we dive into examples, let’s understand what `tail` does. The command displays the last few lines of a file. By default, it shows the last ten lines. But its real power comes when you use the `-f` flag to follow a file as it grows.
Think of it like watching a live feed of your system’s activity. Every time a new log entry is written, `tail -f` updates your screen instantly. This is perfect for debugging real-time issues.
You can also combine `tail` with other commands using pipes. This lets you filter, search, or format the output exactly how you need it.
Basic Syntax And Common Options
The basic syntax is simple: tail [options] filename. Here are the most useful options you will use every day.
- -n [number]: Show the last N lines instead of ten. Example:
tail -n 50 /var/log/syslog - -f: Follow the file as it grows. Press Ctrl+C to stop.
- -F: Like -f but also watches for file rotation. This is safer for logs that get rotated.
- -q: Quiet mode. Suppresses headers when tailing multiple files.
- -v: Verbose. Always show file headers.
For example, to see the last 100 lines of your system log, type: tail -n 100 /var/log/syslog. To follow it live, use: tail -f /var/log/syslog.
Tailing Multiple Log Files At Once
You can tail several files simultaneously. Just list them all after the command. This is super helpful when you need to watch both the web server and application logs together.
Example: tail -f /var/log/apache2/access.log /var/log/apache2/error.log
The output will show each file’s name as a header, followed by its new lines. This way you can see which log is producing each message.
If you want to suppress those headers, add the -q flag. But be careful, you might lose track of which log is which.
Using Tail With Grep For Filtered Logs
Logs can be noisy. You often only care about errors or specific keywords. That is where piping to grep comes in.
Example: tail -f /var/log/syslog | grep -i error
This will show only lines containing “error” (case-insensitive) as they appear. You can combine multiple grep patterns too.
To see lines that match either “error” or “warning”, use: tail -f /var/log/syslog | grep -E "error|warning"
Another trick is to use grep -v to exclude lines. For instance, to ignore debug messages: tail -f /var/log/app.log | grep -v debug
Following Log Files With File Rotation
Many Linux systems rotate logs daily or weekly. When a log is rotated, the old file is renamed and a new one is created. If you use tail -f, it will keep following the old file and miss new entries.
The solution is tail -F (capital F). This option watches for file creation and automatically switches to the new file. It is the safest way to tail logs that rotate.
Example: tail -F /var/log/syslog
This command will keep working even after log rotation. You won’t miss a single line.
Viewing Logs From A Specific Line Number
Sometimes you want to start viewing from a particular line, not just the last N lines. You can do this with the + sign.
Example: tail -n +500 /var/log/syslog will show all lines starting from line 500.
This is useful when you know exactly where the interesting part begins. You can combine it with -f to start from that line and then follow.
Example: tail -n +500 -f /var/log/syslog
Combining Tail With Other Commands
The real power of tail comes from piping. You can chain it with awk, sed, cut, or sort to format the output.
For example, to see only the timestamps and error messages: tail -f /var/log/syslog | awk '{print $1, $2, $5}'
To count how many errors appear per minute: tail -f /var/log/syslog | grep -i error | awk '{print $1, $2}' | uniq -c
You can even send the output to a file for later analysis: tail -f /var/log/syslog | tee captured.log
Common Log Files You Will Tail
Different services write to different log files. Here are the most common ones you will encounter.
- /var/log/syslog or /var/log/messages: General system logs
- /var/log/auth.log: Authentication logs (login attempts, sudo usage)
- /var/log/apache2/access.log: Apache web server requests
- /var/log/apache2/error.log: Apache errors
- /var/log/nginx/access.log: Nginx web server requests
- /var/log/nginx/error.log: Nginx errors
- /var/log/mysql/error.log: MySQL database errors
- /var/log/maillog or /var/log/mail.log: Mail server logs
Your distribution might use different paths. Check /var/log to see what is available.
Using Tail With Journalctl For Systemd Logs
Modern Linux systems use systemd and journald for logging. The journalctl command is the tool for these logs. It has its own tail-like functionality.
To follow the journal in real time: journalctl -f
To see the last 50 lines: journalctl -n 50
You can filter by service: journalctl -u nginx.service -f
This is often more convenient than tailing raw log files because journalctl adds timestamps and structured data.
Practical Examples For Everyday Use
Let’s look at some real-world scenarios where tailing logs saves the day.
Scenario 1: Debugging a web server error
You notice your website is slow. Tail the error log while reproducing the issue: tail -f /var/log/apache2/error.log
Scenario 2: Monitoring SSH login attempts
Watch for unauthorized access: tail -f /var/log/auth.log | grep -E "Failed|Accepted"
Scenario 3: Checking application output
Your custom app writes to a log file. Follow it: tail -f /opt/myapp/logs/app.log
Scenario 4: Combining with date filtering
You want to see only today’s errors: tail -f /var/log/syslog | grep "$(date +'%b %e')" | grep -i error
Performance Considerations When Tailing Large Logs
Tailing a very large file (like a 10GB log) can be slow if you use -n with a huge number. The command still reads from the end, but it might take a moment.
For huge files, avoid using -n 1000000 unless you really need it. Instead, use less or grep to search first.
Also, running multiple tail -f processes can consume resources. Use multitail or tmux to manage multiple tails efficiently.
Using Multitail For Advanced Monitoring
If you need to watch several logs at once with split screens, install multitail. It is not installed by default but is available in most repositories.
Install it with: sudo apt install multitail (Debian/Ubuntu) or sudo yum install multitail (RHEL/CentOS).
Then run: multitail /var/log/syslog /var/log/auth.log
This shows both logs in a split window. You can scroll each pane independently. It even supports color highlighting.
Common Mistakes And How To Avoid Them
Even experienced users make mistakes with tail. Here are the most common ones.
- Using -f instead of -F: If logs rotate, you will miss new entries. Always use
-Ffor rotating logs. - Forgetting to press Ctrl+C: The
-fflag runs forever. Remember to stop it when done. - Not using grep for filtering: Raw tail output can be overwhelming. Always filter when possible.
- Assuming log paths: Different distributions use different paths. Always check
/var/logfirst. - Running as root unnecessarily: Many logs are readable by normal users. Only use sudo when needed.
Scripting With Tail For Automation
You can use tail in scripts to monitor logs and trigger actions. For example, send an email when an error appears.
Here is a simple bash script:
#!/bin/bash
tail -F /var/log/syslog | while read line; do
if echo "$line" | grep -q "CRITICAL"; then
echo "Critical error: $line" | mail -s "Alert" admin@example.com
fi
done
This script follows the log and sends an email for every line containing “CRITICAL”. Be careful not to flood your inbox.
Using Tail With Less For Interactive Browsing
Sometimes you want to tail a log but also scroll back. You can pipe tail into less with the +F mode.
Example: tail -n 1000 /var/log/syslog | less +F
This opens the last 1000 lines in less and immediately starts following. You can press Ctrl+C to stop following and scroll around. Then press F to resume following.
This is a great way to combine live monitoring with historical browsing.
Colorizing Tail Output
Reading plain white text can be tiring. You can add color using ccze or grep --color.
Install ccze: sudo apt install ccze
Then: tail -f /var/log/syslog | ccze -A
This adds color coding for different log levels. Errors appear red, warnings yellow, and info green.
You can also use grep’s color option: tail -f /var/log/syslog | grep --color=always -E "error|warning"
Handling Binary Logs And Non-Text Files
Not all logs are plain text. Some applications write binary logs. The tail command will still work, but the output will be garbled.
For binary logs, use the application’s own tools. For example, journalctl for systemd logs, or mysqlbinlog for MySQL binary logs.
If you must tail a binary file, use tail -c to show bytes instead of lines: tail -c 1000 binary.log
Security Considerations When Tailing Logs
Logs often contain sensitive information like IP addresses, usernames, or even passwords. Be careful when sharing tail output.
Never paste raw log output into public forums without redacting sensitive data. Use sed or awk to mask IPs or usernames.
Example: tail -f /var/log/auth.log | sed 's/[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}/X.X.X.X/g'
Frequently Asked Questions
Q: How do I tail a log file that is being written very fast?
A: Use tail -F with a reasonable buffer. If the output is too fast, pipe it to less +F so you can pause and scroll.
Q: Can I tail a log file from a remote server?
A: Yes, use SSH: ssh user@server "tail -f /var/log/syslog". Or use tools like logwatch or centralized logging.
Q: What is the difference between tail -f and tail -F?
A: -f follows the file descriptor, while -F watches for file creation. Use -F for logs that rotate.
Q: How do I stop tail -f?
A: Press Ctrl+C on your keyboard. This sends an interrupt signal and stops the command.
Q: Why is tail not showing new log entries?
A: The file might have been rotated. Use tail -F instead of tail -f. Also check if the application is actually writing to that file.
Conclusion
Mastering how to tail logs in linux is essential for any system administrator or developer. The command is simple but incredibly powerful when combined with filters and other tools.
Start with basic tail -f for live monitoring. Add -F for rotated logs. Use grep to cut through the noise. And never forget to press Ctrl+C when you are done.
With practice, you will be able to diagnose issues in seconds instead of minutes. Logs are your friends, and tail is the window into them.
Now go ahead and try these commands on your own system. Open a terminal, pick a log file, and start tailing. You will be surprised how much you can learn about your system just by watching its logs flow by.