Understanding /var/log: Linux Log Files Explained
You've just deployed an application, and something isn't working. You SSH into your server, run a few commands, and realize you have no idea where to look for errors. The application is crashing, but the logs are nowhere to be found. This is where /var/log comes in.
Every Linux system generates logs. These files contain critical information about what's happening on your server—successful operations, failed attempts, errors, warnings, and system events. Learning to read and understand /var/log is one of the most valuable skills for any system administrator or DevOps engineer.
What is /var/log?
/var/log is a standard directory on Unix-like operating systems that contains variable data files. The "var" stands for "variable," meaning these files are not static—they change as the system runs. Unlike /etc (configuration files) or /usr (programs), /var/log is specifically designed for log files that grow over time.
Most Linux distributions organize logs in /var/log with a consistent structure. The directory typically contains subdirectories for different types of logs, such as system logs, authentication logs, application logs, and kernel messages.
Common Log Files in /var/log
Let's look at the most important log files you'll encounter.
/var/log/syslog or /var/log/messages
This is the main system log file on most Linux distributions. It contains messages from the kernel and various system services. On Debian-based systems, you'll find /var/log/syslog. On Red Hat-based systems, it's /var/log/messages.
/var/log/auth.log
Authentication-related logs go here. This file records successful and failed login attempts, SSH connections, sudo commands, and other authentication events. It's your first line of defense when investigating security issues.
/var/log/dmesg
The kernel ring buffer is stored in /var/log/dmesg. It contains messages generated by the kernel during boot and runtime. These messages are useful for diagnosing hardware issues, driver problems, and kernel errors.
/var/log/kern.log
On some distributions, kernel messages are logged to /var/log/kern.log instead of /var/log/dmesg. This file contains similar information to dmesg but is written to disk rather than stored in the kernel ring buffer.
/var/log/apt/history.log
If you're managing a Debian-based system, /var/log/apt/history.log records all package installations, updates, and removals. This is useful for tracking changes to your system.
/var/log/nginx/access.log and /var/log/nginx/error.log
If you're running Nginx as a web server, these files contain access logs (requests) and error logs (server errors). These are essential for debugging web application issues.
Log File Rotation and Size Management
Log files grow indefinitely. If left unchecked, they can consume all available disk space. Linux uses log rotation to manage this.
Understanding logrotate
The logrotate utility automatically rotates, compresses, and removes old log files. Its configuration is typically in /etc/logrotate.conf and /etc/logrotate.d/.
A typical logrotate configuration might look like this:
This configuration tells logrotate to:
- Rotate logs daily
- Keep 14 days of logs
- Compress old logs after the first rotation
- Not rotate empty files
- Create new log files with specific permissions
- Send a signal to Nginx to reopen log files
Checking Log Rotation Status
Analyzing Log Files Effectively
Reading raw log files can be overwhelming. Here are practical techniques to extract useful information.
Using tail and head
Using grep for Pattern Matching
Using journalctl (systemd systems)
Modern Linux systems use systemd, which provides journalctl for querying logs.
Filtering by Time
Common Log Analysis Use Cases
Investigating Application Crashes
When an application crashes, check both the application logs and system logs.
Monitoring Security Events
Track authentication attempts and suspicious activity.
Troubleshooting Network Issues
Performance Monitoring
Log File Permissions and Security
Log files contain sensitive information. Proper permissions are critical for security.
Checking Log File Permissions
Setting Proper Permissions
Restricting Access
Best Practices for Log Management
1. Regular Log Review
Make it a habit to review logs regularly. Set up alerts for critical errors.
2. Centralized Logging
For distributed systems, consider centralized logging solutions like ELK Stack, Loki, or Graylog.
3. Log Level Management
Configure applications to use appropriate log levels (DEBUG, INFO, WARNING, ERROR, CRITICAL).
4. Log Retention Policies
Establish clear policies for how long logs should be retained based on compliance requirements and operational needs.
5. Log Encryption
For sensitive applications, consider encrypting log files at rest.
6. Structured Logging
Use structured logging formats (JSON, XML) for easier parsing and analysis.
Tools for Advanced Log Analysis
logwatch
logwatch is a Perl script that summarizes system logs.
logcheck
logcheck automatically checks logs for suspicious activity.
logtail
logtail follows a log file and outputs new lines.
Troubleshooting Log Issues
Logs Not Being Written
If logs aren't being written, check:
Corrupted Log Files
If a log file is corrupted, you can create a new empty file:
Conclusion
The /var/log directory is the window into your Linux system's behavior. By understanding the different log files, how to read them, and how to analyze them, you can diagnose issues, monitor security, and maintain system health.
Remember these key points:
/var/log/syslogor/var/log/messagescontains general system messages/var/log/auth.logtracks authentication events/var/log/dmesgcontains kernel messages- Use
tail,grep, andjournalctlto analyze logs effectively - Implement proper log rotation to prevent disk space issues
- Set appropriate permissions to protect sensitive log information
The next time your application misbehaves, don't panic. Start by checking /var/log. The answers are almost always there, waiting to be found.
Platforms like ServerlessBase simplify log management by providing centralized logging and monitoring dashboards, so you can focus on interpreting the data rather than hunting for it across multiple servers.