Das Systemd Journal ist das zentrale Logging-System moderner Linux-Distributionen. journalctl ermöglicht leistungsfähiges Filtern und Analysieren von Logs.
Grundbefehle
Logs anzeigen
# Alle Logs
journalctl
# Letzte Logs
journalctl -n 100
# Live-Ansicht
journalctl -f
# Ohne Pager
journalctl --no-pager
# Reverse (neueste zuerst)
journalctl -r
Nach Zeit filtern
# Seit Boot
journalctl -b
# Vorheriger Boot
journalctl -b -1
# Seit Zeitpunkt
journalctl --since "2024-01-26 10:00:00"
journalctl --since "1 hour ago"
journalctl --since "yesterday"
# Zeitraum
journalctl --since "2024-01-26" --until "2024-01-27"
# Letzte 10 Minuten
journalctl --since "-10min"
Nach Service
# Bestimmter Service
journalctl -u nginx
journalctl -u nginx.service
# Mehrere Services
journalctl -u nginx -u php-fpm
# Service live
journalctl -u nginx -f
Nach Priorität
# Nur Fehler
journalctl -p err
# Fehler und kritischer
journalctl -p crit
# Bereich
journalctl -p warning..err
| Priorität | Wert | Bedeutung |
|---|
| emerg | 0 | System unbrauchbar |
| alert | 1 | Sofortige Aktion nötig |
| crit | 2 | Kritisch |
| err | 3 | Fehler |
| warning | 4 | Warnung |
| notice | 5 | Normal aber wichtig |
| info | 6 | Informativ |
| debug | 7 | Debug |
Erweiterte Filter
Nach Kernel
# Kernel-Messages
journalctl -k
journalctl --dmesg
Nach Prozess/PID
# Nach PID
journalctl _PID=1234
# Nach Executable
journalctl _EXE=/usr/sbin/nginx
Nach User
journalctl _UID=1000
journalctl _UID=$(id -u www-data)
Kombinierte Filter
# Service + Priorität
journalctl -u nginx -p err
# Service + Zeit + Priorität
journalctl -u nginx --since today -p warning
# Mehrere Bedingungen (OR)
journalctl _SYSTEMD_UNIT=nginx.service + _SYSTEMD_UNIT=php-fpm.service
# Mehrere Bedingungen (AND)
journalctl _SYSTEMD_UNIT=nginx.service _PID=1234
JSON
# JSON-Ausgabe
journalctl -o json
# Pretty JSON
journalctl -o json-pretty
# Einzelne Zeilen JSON
journalctl -o json --no-pager | jq '.MESSAGE'
# Kurz
journalctl -o short
# Mit Timestamps
journalctl -o short-precise
# Verbose (alle Felder)
journalctl -o verbose
# Export-Format
journalctl -o export
Journal-Verwaltung
Speicherplatz
# Aktuelle Größe
journalctl --disk-usage
# Aufräumen nach Zeit
journalctl --vacuum-time=7d
# Aufräumen nach Größe
journalctl --vacuum-size=500M
# Aufräumen nach Anzahl Dateien
journalctl --vacuum-files=5
# Rotieren
journalctl --rotate
Konfiguration
# /etc/systemd/journald.conf
[Journal]
# Speichern auf Disk (persistent) oder RAM (volatile)
Storage=persistent
# Maximale Größe
SystemMaxUse=500M
SystemMaxFileSize=50M
# Runtime (RAM)
RuntimeMaxUse=100M
# Kompression
Compress=yes
# Aufbewahrung
MaxRetentionSec=1month
# Forwarding an Syslog
ForwardToSyslog=no
# Nach Änderung
systemctl restart systemd-journald
Persistentes Journal
# Verzeichnis erstellen
mkdir -p /var/log/journal
systemd-tmpfiles --create --prefix /var/log/journal
# Oder in Konfiguration
# Storage=persistent
Log-Analyse
Häufigste Meldungen
# Nach Häufigkeit
journalctl --no-pager | sort | uniq -c | sort -rn | head -20
Fehler-Übersicht
# Alle Fehler heute
journalctl --since today -p err
# Fehler pro Service
journalctl -p err --output json | jq -r '.SYSLOG_IDENTIFIER' | sort | uniq -c | sort -rn
Boot-Analyse
# Boot-Liste
journalctl --list-boots
# Aktueller Boot
journalctl -b 0
# Vorheriger Boot (bei Crash)
journalctl -b -1
# Boot-Zeit analysieren
systemd-analyze blame
systemd-analyze critical-chain
Praktische Beispiele
Debug-Script
#!/bin/bash
# debug-service.sh
SERVICE=$1
echo "=== Service Status ==="
systemctl status $SERVICE
echo ""
echo "=== Recent Logs ==="
journalctl -u $SERVICE -n 50 --no-pager
echo ""
echo "=== Errors ==="
journalctl -u $SERVICE -p err --since "1 hour ago" --no-pager
Log-Export
# Logs exportieren
journalctl -u nginx --since today > nginx-logs.txt
# Für externe Analyse
journalctl -o json --since "1 hour ago" > logs.json
Monitoring
#!/bin/bash
# Monitor auf Fehler
journalctl -f -p err | while read line; do
echo "ERROR: $line" | mail -s "Log Error Alert" admin@example.de
done
Cleanup-Cron
# /etc/cron.weekly/journal-cleanup
#!/bin/bash
journalctl --vacuum-time=14d
journalctl --vacuum-size=1G
Remote Logging
Journal-Remote
# Auf Empfänger
apt install systemd-journal-remote
systemctl enable systemd-journal-remote.socket
# Auf Sender
apt install systemd-journal-upload
# /etc/systemd/journal-upload.conf
[Upload]
URL=http://logserver:19532
Syslog-Forward
# /etc/systemd/journald.conf
[Journal]
ForwardToSyslog=yes
Zusammenfassung
| Befehl | Funktion |
|---|
| journalctl | Alle Logs |
| journalctl -f | Live-Ansicht |
| journalctl -u service | Nach Service |
| journalctl -p err | Nach Priorität |
| journalctl -b | Seit Boot |
| journalctl --since | Nach Zeit |
| journalctl --disk-usage | Speichernutzung |
| journalctl --vacuum-time | Aufräumen |
| Option | Funktion |
|---|
| -n N | Letzte N Zeilen |
| -r | Reverse |
| -o json | JSON-Format |
| --no-pager | Ohne Pager |
| Datei | Funktion |
|---|
| /etc/systemd/journald.conf | Konfiguration |
| /var/log/journal/ | Persistente Logs |
| /run/log/journal/ | Volatile Logs |
Fazit
Das Systemd Journal ist ein mächtiges Logging-System. Die strukturierten Logs ermöglichen präzises Filtern. JSON-Export erlaubt externe Analyse. Automatische Rotation verhindert Speicherprobleme. Für traditionelle Log-Dateien kann zusätzlich rsyslog eingesetzt werden.