The logging system in Linux is essential for monitoring, debugging, and auditing system activities. It provides detailed information about system events, errors, and application behaviors. This chapter covers how to view, configure, and manage logs effectively.
- Log Files: Files that record system and application events.
- Syslog: A standardized logging protocol used by many services.
- Journal: System logs managed by
systemd
.
/var/log/
: Primary location for system logs./var/log/syslog
: General system messages./var/log/auth.log
: Authentication logs./var/log/dmesg
: Kernel ring buffer logs.
- Use these commands to view log files directly.
- Display the entire log:
cat /var/log/syslog
- View logs page by page:
less /var/log/syslog
- Monitor logs in real-time:
tail -f /var/log/syslog
- View logs maintained by the
systemd
journal.
journalctl [options]
- View all logs:
journalctl
- View logs for a specific unit:
journalctl -u apache2.service
- View logs since boot:
journalctl -b
- Ensures log files don’t consume excessive disk space by rotating and compressing them.
/etc/logrotate.conf
: Global configuration./etc/logrotate.d/
: Per-service configurations.
/var/log/syslog {
weekly
rotate 4
compress
missingok
notifempty
}
- Explanation:
weekly
: Rotate logs weekly.rotate 4
: Keep four old log files.compress
: Compress rotated logs.missingok
: Skip errors if the log file is missing.notifempty
: Do not rotate empty files.
sudo logrotate -f /etc/logrotate.conf
- rsyslog: A common syslog daemon, highly configurable.
- syslog-ng: Alternative to
rsyslog
, offering advanced features.
- Configuration file:
/etc/rsyslog.conf
- Log all authentication messages to a custom file:
auth.* /var/log/auth.log
- Restart the service:
sudo systemctl restart rsyslog
- Extract specific information from logs.
- Search for errors:
grep "error" /var/log/syslog
- Search for a specific IP:
grep "192.168.1.100" /var/log/auth.log
- Extract and format specific log fields.
awk '{print $1, $2, $3}' /var/log/syslog
- Generates summaries of log activities.
sudo apt install logwatch
sudo logwatch --detail high --mailto admin@example.com --range today
- Only authorized users should access sensitive logs.
sudo chmod 640 /var/log/auth.log
sudo chown root:adm /var/log/auth.log
- Forward logs to a central server for secure storage and analysis.
- Add the following to
/etc/rsyslog.conf
:*.* @@remote-log-server:514
- Restart
rsyslog
:sudo systemctl restart rsyslog
By the end of this chapter, you should be able to:
- View and analyze log files using tools like
journalctl
,tail
, andgrep
. - Configure log rotation with
logrotate
. - Customize logging behavior using
rsyslog
orsyslog-ng
. - Secure log files and forward them to a remote server.
- Move to Chapter 12: Using and Abusing Services to learn about configuring and testing Linux services.
- View the last 20 lines of the
/var/log/syslog
file in real-time. - Configure
logrotate
to compress and rotate/var/log/auth.log
weekly. - Search for failed login attempts in
/var/log/auth.log
. - Forward logs from your system to a remote logging server.
- Generate a daily summary of logs using
logwatch
.