Journalctl ist das Werkzeug zum Lesen des Systemd Journals. Es bietet mächtige Filter- und Suchmöglichkeiten für System-Logs.

Grundlegende Verwendung

Alle Logs anzeigen

# Alle Logs (älteste zuerst)
journalctl

# Neueste zuerst
journalctl -r

# Live-Ansicht (wie tail -f)
journalctl -f

# Anzahl begrenzen
journalctl -n 100

Ausgabe-Formate

# Kurz (Standard)
journalctl -o short

# Mit Zeitstempeln
journalctl -o short-iso
journalctl -o short-precise

# JSON-Ausgabe
journalctl -o json
journalctl -o json-pretty

# Verbose (alle Felder)
journalctl -o verbose

# Nur Nachricht
journalctl -o cat

Nach Zeit filtern

Relative Zeit

# Letzte Stunde
journalctl --since "1 hour ago"

# Letzte 30 Minuten
journalctl --since "30 min ago"

# Seit gestern
journalctl --since yesterday

# Heute
journalctl --since today

Absolute Zeit

# Ab bestimmter Zeit
journalctl --since "2024-01-15 10:00:00"

# Bis bestimmte Zeit
journalctl --until "2024-01-15 12:00:00"

# Zeitraum
journalctl --since "2024-01-15" --until "2024-01-16"

Boot-spezifisch

# Aktueller Boot
journalctl -b

# Vorheriger Boot
journalctl -b -1

# Alle verfügbaren Boots
journalctl --list-boots

# Bestimmter Boot
journalctl -b abc123  # Boot-ID

Nach Unit filtern

Services

# Bestimmter Service
journalctl -u nginx
journalctl -u nginx.service

# Mehrere Services
journalctl -u nginx -u php-fpm

# Pattern
journalctl -u "ssh*"

Service + Zeit

# Nginx seit heute
journalctl -u nginx --since today

# MySQL letzte Stunde
journalctl -u mysql --since "1 hour ago" -f

Nach Priorität filtern

Log-Level

# Nur Fehler
journalctl -p err

# Fehler und kritischer
journalctl -p crit

# Warnungen und höher
journalctl -p warning

# Bereich
journalctl -p err..emerg

Prioritäten

| Level | Nummer | Beschreibung | |-------|--------|--------------| | emerg | 0 | System unbenutzbar | | alert | 1 | Sofortige Aktion nötig | | crit | 2 | Kritische Fehler | | err | 3 | Fehler | | warning | 4 | Warnungen | | notice | 5 | Normale, wichtige Events | | info | 6 | Informationen | | debug | 7 | Debug-Informationen |

Nach Prozess filtern

PID

# Nach PID
journalctl _PID=1234

# Kernel-Meldungen
journalctl -k
journalctl --dmesg

UID/GID

# Nach Benutzer-ID
journalctl _UID=1000

# Nach Benutzer-Name
journalctl _UID=$(id -u username)

Executable

# Nach Programmpfad
journalctl _EXE=/usr/sbin/sshd

# Nach Command
journalctl _COMM=sshd

Suchen und Filtern

Text-Suche

# Mit grep
journalctl | grep "error"

# Case-insensitive
journalctl | grep -i "failed"

# Kontext anzeigen
journalctl | grep -C 3 "error"

Journal-interne Suche

# Grep-ähnlich (schneller)
journalctl -g "pattern"

# Case-insensitive
journalctl -g "(?i)error"

Felder anzeigen

# Alle Felder einer Nachricht
journalctl -o verbose -n 1

# Verfügbare Felder
journalctl -N
journalctl --fields

# Feld-Werte
journalctl -F _SYSTEMD_UNIT
journalctl -F PRIORITY

Festplatten-Nutzung

Speicher prüfen

# Journal-Größe
journalctl --disk-usage

# Älteste und neueste Einträge
journalctl --header

Speicher begrenzen

# /etc/systemd/journald.conf

[Journal]
SystemMaxUse=500M
SystemKeepFree=1G
SystemMaxFileSize=100M
MaxRetentionSec=1month

Manuell bereinigen

# Nach Zeit
journalctl --vacuum-time=7d

# Nach Größe
journalctl --vacuum-size=500M

# Nach Dateien
journalctl --vacuum-files=5

Persistente Logs

Aktivieren

# Verzeichnis erstellen
mkdir -p /var/log/journal

# Oder in Konfiguration
# /etc/systemd/journald.conf
[Journal]
Storage=persistent
systemctl restart systemd-journald

Storage-Optionen

| Option | Beschreibung | |--------|--------------| | volatile | Nur in /run (RAM) | | persistent | In /var/log/journal | | auto | Persistent wenn Verzeichnis existiert | | none | Logs deaktiviert |

Praktische Beispiele

Fehlgeschlagene Logins

journalctl -u sshd | grep -i "failed"
journalctl _SYSTEMD_UNIT=sshd.service | grep "Failed password"

Boot-Probleme

# Kernel-Meldungen beim Boot
journalctl -b -k

# Fehler beim Boot
journalctl -b -p err

# Vor dem Absturz
journalctl -b -1 -p err

Service-Debugging

# Service-Start verfolgen
journalctl -u nginx -f

# Detailliert
journalctl -u nginx -o verbose

# Mit Debug-Level
systemctl edit nginx
# [Service]
# Environment="DEBUG=1"

Cron-ähnliche Aufgaben

# Systemd Timer-Logs
journalctl -u backup.timer -u backup.service

Zusammenfassung

| Option | Beschreibung | |--------|--------------| | -u | Nach Unit filtern | | -p | Nach Priorität | | -b | Nach Boot | | -f | Live-Ansicht | | -n | Letzte N Zeilen | | -r | Umgekehrte Reihenfolge | | --since | Ab Zeitpunkt | | --until | Bis Zeitpunkt | | -o | Ausgabeformat | | --disk-usage | Speicher anzeigen |

| Format | Beschreibung | |--------|--------------| | short | Standard | | short-iso | Mit ISO-Zeitstempel | | verbose | Alle Felder | | json | JSON-Ausgabe | | cat | Nur Nachricht |

Fazit

Journalctl bietet umfangreiche Möglichkeiten zur Log-Analyse. Die Kombination von Zeit-, Unit- und Prioritäts-Filtern ermöglicht präzise Suchen. Für Produktivserver empfiehlt sich persistentes Logging mit Größenbegrenzung. Die strukturierten Felder machen journalctl mächtiger als traditionelle Log-Dateien.