Log Management with Logwatch and Journald
Overview
- What you’ll learn: How Linux logging works under systemd-journald and rsyslog, how to query and filter logs with journalctl, how to deploy Logwatch for automated log analysis, and best practices for log retention, rotation, and centralization.
- Prerequisites: System Monitoring Fundamentals (Lesson 61), systemd basics
- Estimated reading time: 18 minutes
Introduction
Logs are the narrative history of everything that happens on a Linux system. Every service startup, authentication attempt, error condition, and configuration change is recorded in log files. For system administrators, logs are the primary tool for troubleshooting, security auditing, and compliance reporting.
Modern Ubuntu Server systems use two parallel logging subsystems: systemd-journald, which captures structured log data from all systemd-managed services, and rsyslog, which provides traditional syslog-compatible logging to text files. Understanding both systems and how they interact is essential for effective log management.
In this lesson, we will master the journalctl command for querying the systemd journal, configure rsyslog for log forwarding, deploy Logwatch for automated daily log summaries, and implement log rotation policies to manage disk space.
The Linux Logging Architecture
On a systemd-based Ubuntu system, logging flows through several layers:
- systemd-journald: The primary log collector. It captures stdout/stderr from all systemd services, kernel messages, and syslog messages. The journal is stored in a binary format in
/var/log/journal/(persistent) or/run/log/journal/(volatile). - rsyslog: The traditional syslog daemon. It reads from the journal and writes categorized log files to
/var/log/(e.g.,syslog,auth.log,kern.log). - Application logs: Some applications write directly to their own log files (e.g., Apache logs in
/var/log/apache2/, Nginx logs in/var/log/nginx/).
# Check if journald is running with persistent storage
$ systemctl status systemd-journald
$ ls -la /var/log/journal/
# Check if rsyslog is active
$ systemctl status rsyslog
# View the standard log files rsyslog creates
$ ls -la /var/log/syslog /var/log/auth.log /var/log/kern.log
Mastering journalctl
The journalctl command is the primary interface for querying the systemd journal. It supports powerful filtering by time, service unit, priority level, and many other fields.
# View all journal entries (newest last)
$ journalctl
# Follow new entries in real time (like tail -f)
$ journalctl -f
# Show entries from the current boot only
$ journalctl -b
# Show entries from the previous boot
$ journalctl -b -1
# Filter by systemd unit
$ journalctl -u nginx.service
$ journalctl -u ssh.service --since "1 hour ago"
# Filter by time range
$ journalctl --since "2026-03-01 00:00:00" --until "2026-03-01 23:59:59"
$ journalctl --since "yesterday" --until "today"
# Filter by priority (0=emerg through 7=debug)
$ journalctl -p err # Show only errors and above
$ journalctl -p warning..err # Show warnings and errors
# Filter by process PID
$ journalctl _PID=1234
# Show kernel messages only
$ journalctl -k
# Output in JSON format for parsing
$ journalctl -u nginx.service -o json-pretty --no-pager | head -30
# Show disk usage of the journal
$ journalctl --disk-usage
Archived and active journals take up 256.0M in /var/log/journal.
Configuring journald
The journal is configured through /etc/systemd/journald.conf. Key settings control storage persistence, size limits, and retention policies.
# Edit journald configuration
$ sudo nano /etc/systemd/journald.conf
[Journal]
# Persistent storage (survives reboots)
Storage=persistent
# Maximum disk usage for journal files
SystemMaxUse=500M
# Maximum size of individual journal files
SystemMaxFileSize=50M
# How long to keep journal entries
MaxRetentionSec=30day
# Rate limiting (per-service)
RateLimitIntervalSec=30s
RateLimitBurst=10000
# Restart journald to apply changes
$ sudo systemctl restart systemd-journald
rsyslog Configuration
While journald captures everything, rsyslog provides text-based log files that are easier to process with traditional Unix tools and are required by many compliance frameworks.
# Main rsyslog configuration
$ cat /etc/rsyslog.conf
# Facility.Priority routing rules in /etc/rsyslog.d/50-default.conf
auth,authpriv.* /var/log/auth.log
*.*;auth,authpriv.none /var/log/syslog
kern.* /var/log/kern.log
mail.* /var/log/mail.log
# Forward logs to a remote syslog server
$ sudo tee /etc/rsyslog.d/60-remote.conf > /dev/null <<'EOF'
# Forward all logs to central syslog server via TCP
*.* @@logserver.example.com:514
# Forward only auth logs via UDP
auth,authpriv.* @logserver.example.com:514
EOF
$ sudo systemctl restart rsyslog
Log Rotation with logrotate
Without rotation, log files would grow indefinitely and eventually fill the disk. The logrotate utility automatically compresses, rotates, and removes old log files based on configurable policies.
# Global logrotate configuration
$ cat /etc/logrotate.conf
# Application-specific configuration
$ cat /etc/logrotate.d/rsyslog
/var/log/syslog
/var/log/mail.log
/var/log/kern.log
/var/log/auth.log
{
rotate 7
daily
missingok
notifempty
delaycompress
compress
postrotate
/usr/lib/rsyslog/rsyslog-rotate
endscript
}
# Create a custom logrotate config for an application
$ sudo tee /etc/logrotate.d/myapp > /dev/null <<'EOF'
/var/log/myapp/*.log {
daily
rotate 30
compress
delaycompress
missingok
notifempty
create 0640 www-data www-data
sharedscripts
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
EOF
# Test logrotate configuration (dry run)
$ sudo logrotate -d /etc/logrotate.d/myapp
# Force rotation for testing
$ sudo logrotate -f /etc/logrotate.d/myapp
Deploying Logwatch
Logwatch is an automated log analysis tool that parses system logs and generates human-readable summary reports. It is particularly useful for daily security reviews, as it highlights failed login attempts, service errors, disk usage warnings, and other notable events.
# Install Logwatch
$ sudo apt install -y logwatch
# Run Logwatch manually for today's logs
$ sudo logwatch --detail High --range today --output stdout
# Run for a specific date range
$ sudo logwatch --detail Med --range "between 2/28/2026 and 3/1/2026" --output stdout
# Generate an HTML report
$ sudo logwatch --detail High --range yesterday --output file
--filename /tmp/logwatch-report.html --format html
# Configure Logwatch defaults
$ sudo nano /etc/logwatch/conf/logwatch.conf
Output = mail
Format = html
MailTo = admin@example.com
MailFrom = logwatch@server.example.com
Detail = Med
Range = yesterday
# Logwatch runs automatically via daily cron
$ ls -la /etc/cron.daily/00logwatch
Practical Log Analysis Patterns
Combining journalctl with traditional text processing tools allows for powerful ad-hoc log analysis.
# Find failed SSH login attempts
$ journalctl -u ssh.service | grep "Failed password"
# Count failed logins by IP address
$ journalctl -u ssh.service --since "24 hours ago" --no-pager
| grep "Failed password"
| grep -oP 'from K[0-9.]+'
| sort | uniq -c | sort -rn | head -10
# Find out-of-memory (OOM) killer events
$ journalctl -k | grep -i "oom"
# Check for disk errors
$ journalctl -k --since "7 days ago" | grep -i "error|fail|I/O"
# Monitor a specific service in real time
$ journalctl -u nginx.service -f -p warning
Key Takeaways
- Ubuntu uses two parallel logging subsystems: systemd-journald (structured, binary) and rsyslog (traditional text files).
journalctlprovides powerful filtering by unit, priority, time range, PID, and other structured fields.- Configure journald persistence and size limits in
/etc/systemd/journald.confto prevent journal files from consuming excessive disk space. - rsyslog routes log messages to categorized text files and can forward logs to remote servers for centralized collection.
logrotateautomatically compresses and removes old log files; always configure rotation for custom application logs.- Logwatch provides automated daily log summaries delivered by email, highlighting security events and system anomalies.
What’s Next
In the next lesson, we will explore Backup with Bacula and rsnapshot — learning how to implement reliable backup strategies to protect your data and enable disaster recovery.
繁體中文
概述
- 您將學到:Linux 在 systemd-journald 和 rsyslog 下的日誌記錄工作原理,如何使用 journalctl 查詢和過濾日誌,如何部署 Logwatch 進行自動化日誌分析,以及日誌保留、輪替和集中化的最佳實踐。
- 先決條件:系統監控基礎(第 61 課)、systemd 基礎
- 預計閱讀時間:18 分鐘
介紹
日誌是 Linux 系統上發生的所有事件的敘事歷史。每個服務啟動、認證嘗試、錯誤狀況和設定變更都記錄在日誌檔案中。對系統管理員來說,日誌是故障排除、安全稽核和合規報告的主要工具。
現代 Ubuntu Server 系統使用兩個平行的日誌子系統:systemd-journald(從所有 systemd 管理的服務捕獲結構化日誌數據)和 rsyslog(提供傳統的 syslog 相容日誌記錄到文字檔案)。
Linux 日誌架構
- systemd-journald:主要日誌收集器。它捕獲所有 systemd 服務的 stdout/stderr、核心訊息和 syslog 訊息。
- rsyslog:傳統的 syslog 守護程序。從日誌讀取並將分類的日誌檔案寫入
/var/log/。 - 應用程式日誌:某些應用程式直接寫入自己的日誌檔案。
掌握 journalctl
# 查看所有日誌條目
$ journalctl
# 即時追蹤新條目
$ journalctl -f
# 按 systemd 單元過濾
$ journalctl -u nginx.service
# 按時間範圍過濾
$ journalctl --since "yesterday" --until "today"
# 按優先級過濾
$ journalctl -p err
# 顯示日誌磁碟使用量
$ journalctl --disk-usage
設定 journald
$ sudo nano /etc/systemd/journald.conf
[Journal]
Storage=persistent
SystemMaxUse=500M
MaxRetentionSec=30day
rsyslog 設定
# 將日誌轉發到遠端 syslog 伺服器
*.* @@logserver.example.com:514
使用 logrotate 進行日誌輪替
沒有輪替,日誌檔案會無限增長並最終填滿磁碟。logrotate 工具根據可設定的策略自動壓縮、輪替和刪除舊日誌檔案。
部署 Logwatch
# 安裝 Logwatch
$ sudo apt install -y logwatch
# 手動執行 Logwatch
$ sudo logwatch --detail High --range today --output stdout
# 設定 Logwatch 預設值
$ sudo nano /etc/logwatch/conf/logwatch.conf
重點總結
- Ubuntu 使用兩個平行的日誌子系統:systemd-journald(結構化、二進制)和 rsyslog(傳統文字檔案)。
journalctl提供按單元、優先級、時間範圍和其他結構化欄位的強大過濾功能。- 在
/etc/systemd/journald.conf中設定 journald 持久化和大小限制。 - rsyslog 將日誌訊息路由到分類的文字檔案,並可將日誌轉發到遠端伺服器。
logrotate自動壓縮和刪除舊日誌檔案。- Logwatch 提供透過電子郵件傳送的自動化每日日誌摘要。
下一步
在下一課中,我們將探討使用 Bacula 和 rsnapshot 進行備份——學習如何實施可靠的備份策略來保護數據並實現災難復原。
日本語
概要
- 学習内容:systemd-journald と rsyslog によるLinuxのログ記録の仕組み、journalctl によるログのクエリとフィルタリング、Logwatch による自動ログ分析の展開、ログ保持・ローテーション・集約のベストプラクティス。
- 前提条件:システム監視の基礎(レッスン61)、systemd の基礎
- 推定読了時間:18分
はじめに
ログは Linux システム上で起こるすべてのことの物語的な履歴です。すべてのサービス起動、認証試行、エラー状態、設定変更がログファイルに記録されます。システム管理者にとって、ログはトラブルシューティング、セキュリティ監査、コンプライアンスレポートの主要なツールです。
モダンな Ubuntu Server システムは2つの並列ログサブシステムを使用します:systemd-journald(すべての systemd 管理サービスから構造化ログデータをキャプチャ)と rsyslog(従来の syslog 互換ログをテキストファイルに提供)。
Linux ログアーキテクチャ
- systemd-journald:主要なログコレクター。すべての systemd サービスの stdout/stderr、カーネルメッセージ、syslog メッセージをキャプチャする。
- rsyslog:従来の syslog デーモン。ジャーナルから読み取り、分類されたログファイルを
/var/log/に書き込む。 - アプリケーションログ:一部のアプリケーションは独自のログファイルに直接書き込む。
journalctl をマスターする
# すべてのジャーナルエントリを表示
$ journalctl
# リアルタイムで新しいエントリをフォロー
$ journalctl -f
# systemd ユニットでフィルタリング
$ journalctl -u nginx.service
# 時間範囲でフィルタリング
$ journalctl --since "yesterday" --until "today"
# 優先度でフィルタリング
$ journalctl -p err
# ジャーナルのディスク使用量を表示
$ journalctl --disk-usage
journald の設定
$ sudo nano /etc/systemd/journald.conf
[Journal]
Storage=persistent
SystemMaxUse=500M
MaxRetentionSec=30day
rsyslog の設定
# リモート syslog サーバーにログを転送
*.* @@logserver.example.com:514
logrotate によるログローテーション
ローテーションなしでは、ログファイルは無限に増大し、最終的にディスクを埋め尽くします。logrotate ユーティリティは設定可能なポリシーに基づいて、古いログファイルを自動的に圧縮、ローテーション、削除します。
Logwatch の展開
# Logwatch をインストール
$ sudo apt install -y logwatch
# Logwatch を手動実行
$ sudo logwatch --detail High --range today --output stdout
# Logwatch のデフォルトを設定
$ sudo nano /etc/logwatch/conf/logwatch.conf
重要ポイント
- Ubuntu は2つの並列ログサブシステムを使用する:systemd-journald(構造化、バイナリ)と rsyslog(従来のテキストファイル)。
journalctlはユニット、優先度、時間範囲、その他の構造化フィールドによる強力なフィルタリングを提供する。/etc/systemd/journald.confで journald の永続化とサイズ制限を設定する。- rsyslog はログメッセージを分類されたテキストファイルにルーティングし、リモートサーバーにログを転送できる。
logrotateは古いログファイルを自動的に圧縮・削除する。- Logwatch はメールで配信される自動化された日次ログサマリーを提供する。
次のステップ
次のレッスンでは、Bacula と rsnapshot によるバックアップについて学びます。データを保護し、災害復旧を可能にする信頼性の高いバックアップ戦略の実装方法を学びます。