Logs & Journalctl
Logs & Journalctl unter Linux
Logs sind essentiell für Troubleshooting und Überwachung. Dieses Guide erklärt das klassische Syslog-System und das moderne Journald.
Logging-Systeme im Überblick
| System | Beschreibung | Logs |
|---|---|---|
| Syslog | Klassisch, Textdateien | /var/log/ |
| Journald | Modern (systemd), binär | journalctl |
Die meisten Systeme nutzen heute beide parallel.
Wichtige Log-Dateien
/var/log/ Verzeichnis
| Datei | Inhalt |
|---|---|
/var/log/syslog |
Allgemeine Systemmeldungen (Debian) |
/var/log/messages |
Allgemeine Systemmeldungen (RHEL) |
/var/log/auth.log |
Authentifizierung, SSH, sudo |
/var/log/kern.log |
Kernel-Meldungen |
/var/log/dmesg |
Boot-Meldungen |
/var/log/dpkg.log |
Paketinstallationen (Debian) |
/var/log/apt/ |
APT-Logs |
/var/log/nginx/ |
Nginx Access/Error Logs |
/var/log/apache2/ |
Apache Logs |
/var/log/mysql/ |
MySQL/MariaDB Logs |
/var/log/fail2ban.log |
Fail2Ban |
/var/log/cron.log |
Cron-Jobs |
/var/log/mail.log |
Mail-Server |
/var/log/ufw.log |
UFW Firewall |
Logs live verfolgen
# Eine Datei
tail -f /var/log/syslog
# Mehrere Dateien
tail -f /var/log/syslog /var/log/auth.log
# Mit Highlighting
tail -f /var/log/syslog | grep --color -E "error|warning|"
Journalctl (Systemd Journal)
Grundlegende Abfragen
# Alle Logs anzeigen
journalctl
# Neueste zuerst
journalctl -r
# Live verfolgen (wie tail -f)
journalctl -f
# Nur letzte 100 Zeilen
journalctl -n 100
# Ohne Pager (direkte Ausgabe)
journalctl --no-pager
Nach Zeit filtern
# Seit dem letzten Boot
journalctl -b
# Vorheriger Boot
journalctl -b -1
# Alle Boot-Logs auflisten
journalctl --list-boots
# Seit bestimmter Zeit
journalctl --since "2024-01-15 10:00"
journalctl --since "1 hour ago"
journalctl --since "yesterday"
journalctl --since today
# Zeitbereich
journalctl --since "2024-01-15" --until "2024-01-16"
journalctl --since "09:00" --until "10:00"
Nach Unit/Service filtern
# Bestimmter Service
journalctl -u nginx
journalctl -u nginx.service
# Mehrere Services
journalctl -u nginx -u php-fpm
# Service live verfolgen
journalctl -u nginx -f
# Service seit Boot
journalctl -u nginx -b
Nach Priorität filtern
# Nur Fehler und kritischer
journalctl -p err
# Prioritätsstufen:
# 0: emerg - System unbenutzbar
# 1: alert - Sofortige Aktion nötig
# 2: crit - Kritisch
# 3: err - Fehler
# 4: warning - Warnung
# 5: notice - Normal, aber wichtig
# 6: info - Information
# 7: debug - Debug
# Bereich
journalctl -p warning..err
# Nur kritische Fehler
journalctl -p 0..3
Nach Prozess/User filtern
# Nach PID
journalctl _PID=1234
# Nach User
journalctl _UID=1000
journalctl _UID=$(id -u username)
# Nach Executable
journalctl /usr/bin/nginx
# Nach Hostname
journalctl _HOSTNAME=server1
Kernel-Logs
# Kernel-Meldungen
journalctl -k
# Kernel-Meldungen seit Boot
journalctl -k -b
# Entspricht dmesg
dmesg
dmesg -T # Mit Timestamp
dmesg -w # Live verfolgen
Output-Formate
# JSON-Format (für Parsing)
journalctl -o json
journalctl -o json-pretty
# Kurzes Format
journalctl -o short
# Verbose (alle Felder)
journalctl -o verbose
# Export-Format
journalctl -o export
Praktische Beispiele
Fehlersuche Service startet nicht
# Service-Status
systemctl status nginx
# Letzte Logs des Service
journalctl -u nginx -n 50 --no-pager
# Seit letztem Startversuch
journalctl -u nginx --since "5 minutes ago"
SSH-Anmeldeversuche
# Alle SSH-Logs
journalctl -u sshd
# Nur fehlgeschlagene
journalctl -u sshd | grep -i "failed\|invalid"
# Oder klassisch
grep "Failed password" /var/log/auth.log
grep "Accepted" /var/log/auth.log
Systemprobleme nach Reboot
# Was passierte vor dem Absturz?
journalctl -b -1 -p err
# Kernel-Fehler
journalctl -k -b -1 -p err
Cron-Jobs debuggen
journalctl -u cron
# oder
grep CRON /var/log/syslog
Festplatten-Probleme
# I/O-Fehler
journalctl -k | grep -i "error\|fail\|i/o"
dmesg | grep -i "error\|fail"
Log-Rotation
Logrotate
Verhindert, dass Logs zu groß werden.
# Konfiguration
/etc/logrotate.conf
/etc/logrotate.d/
# Manuell ausführen
sudo logrotate -f /etc/logrotate.conf
# Testen (Dry-Run)
sudo logrotate -d /etc/logrotate.d/nginx
Beispiel-Konfiguration:
# /etc/logrotate.d/myapp
/var/log/myapp/*.log {
daily # Täglich rotieren
rotate 7 # 7 Versionen behalten
compress # Mit gzip komprimieren
delaycompress # Erst nach 2. Rotation komprimieren
missingok # Kein Fehler wenn Log fehlt
notifempty # Leere Dateien nicht rotieren
create 0640 www-data www-data
postrotate
systemctl reload myapp > /dev/null 2>&1 || true
endscript
}
Journald-Speicher begrenzen
# /etc/systemd/journald.conf
[Journal]
SystemMaxUse=500M # Max. Speicher
SystemKeepFree=1G # Min. freier Speicher
MaxRetentionSec=1month # Max. Alter
# Änderungen anwenden
sudo systemctl restart systemd-journald
# Aktuellen Speicherverbrauch
journalctl --disk-usage
# Manuell aufräumen
sudo journalctl --vacuum-size=500M
sudo journalctl --vacuum-time=30d
Zentrales Logging
Rsyslog zu Remote-Server
Client (/etc/rsyslog.conf):
# An Remote-Server senden
*.* @logserver.example.com:514 # UDP
*.* @@logserver.example.com:514 # TCP
Server:
# Empfangen aktivieren
module(load="imudp")
input(type="imudp" port="514")
Journald zu Remote (systemd-journal-remote)
# Server
sudo apt install systemd-journal-remote
sudo systemctl enable --now systemd-journal-remote.socket
# Client
# /etc/systemd/journal-upload.conf
[Upload]
URL=http://logserver:19532
Nützliche Log-Befehle
Schnelle Analyse
# Häufigste Fehler
journalctl -p err --since today | sort | uniq -c | sort -rn | head
# Letzte Logins
last
lastlog
# Fehlgeschlagene Logins
lastb
# Wer ist eingeloggt?
who
w
Logs durchsuchen
# Mit grep
journalctl | grep -i "error"
# Mit grep und Kontext
journalctl | grep -B5 -A5 "error"
# Case-insensitive
journalctl | grep -i "nginx"
# Regex
journalctl | grep -E "error|warning|critical"
Log-Statistiken
# Anzahl Fehler pro Service
journalctl -p err -o json | jq -r '._SYSTEMD_UNIT' | sort | uniq -c | sort -rn
# Logs pro Stunde
journalctl --since today -o short-iso | cut -d'T' -f2 | cut -d':' -f1 | sort | uniq -c
Troubleshooting-Checkliste
-
Service-Problem?
bash systemctl status service journalctl -u service -n 50 -
Systemstart-Problem?
bash journalctl -b -p err dmesg | grep -i error -
Netzwerk-Problem?
bash journalctl -u NetworkManager journalctl -u systemd-networkd -
Authentifizierung?
bash journalctl -u sshd grep -i "fail\|invalid" /var/log/auth.log -
Festplatten?
bash journalctl -k | grep -i "error\|i/o" dmesg | tail -50 -
Out of Memory?
bash journalctl -k | grep -i "oom\|killed" dmesg | grep -i "oom"