Retrouver des logs perdus

Le répertoire /run/log est en RAM, donc en cas d’arrêt du système les données qu’il contient seront perdues mais il est possible de déplacer ses données dans un autre répertoire.

Retrouver les logs perdus avec journalctl :

La commande journalctl permet de récuperer les logs perdus.

# journalctl

Afficher les 3 dernières entrées :

# journalctl -n 2
Sep 18 15:20:52 SERVER02 systemd[1]: Started Network Manager Script Dispatcher Service.
Sep 18 15:21:02 SERVER02 systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully.

Afficher les 10 dernières entrées en temps réel :

# journalctl -f
Sep 18 14:33:05 SERVER02 systemd[1]: dnf-makecache.service: Deactivated successfully.
Sep 18 14:33:05 SERVER02 systemd[1]: Finished dnf makecache.
Sep 18 15:01:01 SERVER02 CROND[32507]: (root) CMD (run-parts /etc/cron.hourly)
Sep 18 15:01:01 SERVER02 run-parts[32510]: (/etc/cron.hourly) starting 0anacron
Sep 18 15:01:01 SERVER02 run-parts[32516]: (/etc/cron.hourly) finished 0anacron
Sep 18 15:01:01 SERVER02 CROND[32506]: (root) CMDEND (run-parts /etc/cron.hourly)
Sep 18 15:20:52 SERVER02 NetworkManager[923]: <info> [1726687252.9025] dhcp4 (ens192): state changed new lease, address=192.168.1.110
Sep 18 15:20:52 SERVER02 systemd[1]: Starting Network Manager Script Dispatcher Service...
Sep 18 15:20:52 SERVER02 systemd[1]: Started Network Manager Script Dispatcher Service.
Sep 18 15:21:02 SERVER02 systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully.

Filtrer les entrées avec la priorité err ou supérieure :

# journalctl -p err
Sep 08 12:43:24 SERVER02 sudo[25805]: pam_unix(sudo:auth): conversation failed
Sep 08 12:43:24 SERVER02 sudo[25805]: pam_unix(sudo:auth): auth could not identify password for [aline]
Sep 08 12:43:25 SERVER02 sudo[25805]: aline : user NOT in sudoers ; TTY=pts/0 ; PWD=/home/aline ; USER=root ; COMMAND=/bin/cat /etc/sudoers
[...]

Afficher les messages d’une unité précise :

# journalctl -u sshd.service
Aug 28 10:25:13 SERVER02 systemd[1]: Starting OpenSSH server daemon...
Aug 28 10:25:13 SERVER02 sshd[9446]: Server listening on :: port 22.
Aug 28 10:25:13 SERVER02 systemd[1]: Started OpenSSH server daemon.
Aug 28 10:25:22 SERVER02 sshd[9450]: Connection closed by authenticating user aline 192.168.1.109 port 44064 [preauth]
Aug 28 10:25:25 SERVER02 unix_chkpwd[9454]: password check failed for user (aline)
Aug 28 10:27:41 SERVER02 sshd[9472]: pam_unix(sshd:auth): authentication failure; logname= uid=0 euid=0 tty=ssh ruser= rhost=192.168.1.109 user=aline
Aug 28 10:27:43 SERVER02 sshd[9472]: Failed password for aline from 192.168.1.109 port 40770 ssh2
[...]

Afficher depuis une date avec --since et --until :

# journalctl --since today
# journalctl --since "-1 hour"

Dans une tranche de temps :

# journalctl --since "2024-09-18 03:00" --until "2024-09-18 03:30"
Sep 18 03:01:01 SERVER02 CROND[31962]: (root) CMD (run-parts /etc/cron.hourly)
Sep 18 03:01:01 SERVER02 run-parts[31965]: (/etc/cron.hourly) starting 0anacron
Sep 18 03:01:01 SERVER02 anacron[31975]: Anacron started on 2024-09-18
Sep 18 03:01:01 SERVER02 anacron[31975]: Will run job `cron.daily' in 30 min.
Sep 18 03:01:01 SERVER02 anacron[31975]: Will run job `cron.weekly' in 50 min.
Sep 18 03:01:01 SERVER02 anacron[31975]: Jobs will be executed sequentially
Sep 18 03:01:01 SERVER02 run-parts[31977]: (/etc/cron.hourly) finished 0anacron
Sep 18 03:01:01 SERVER02 CROND[31961]: (root) CMDEND (run-parts /etc/cron.hourly)

Afficher plus de détails :

# journalctl -o verbose

Exemple

# journalctl --since "2024-09-18 03:00" --until "2024-09-18 03:30" -o verbose
[...]
Wed 2024-09-18 03:01:01.428620 EDT [s=28743968b4d643a2b44a1c188008e1aa;i=349f;b=7081bf7bbe63455794e99d65994310f7;m=2b9a12e5d83;t=6225f5e68d10f;x=85ec838495d878b2]
_TRANSPORT=syslog
PRIORITY=6
SYSLOG_FACILITY=9
SYSLOG_IDENTIFIER=CROND
MESSAGE=(root) CMDEND (run-parts /etc/cron.hourly)
_UID=0
_GID=0
_COMM=crond
_CAP_EFFECTIVE=1ffffffffff
_SELINUX_CONTEXT=system_u:system_r:crond_t:s0-s0:c0.c1023
_SYSTEMD_CGROUP=/system.slice/crond.service
_SYSTEMD_UNIT=crond.service
_SYSTEMD_SLICE=system.slice
_SYSTEMD_INVOCATION_ID=cfc2ba04d3734d30a1293c120b086ab0
_BOOT_ID=7081bf7bbe63455794e99d65994310f7
_MACHINE_ID=db2bb551f8eb413e9bf0af48634d2075
_HOSTNAME=SERVER02
_RUNTIME_SCOPE=system
SYSLOG_TIMESTAMP=Sep 18 03:01:01
SYSLOG_PID=31961
_PID=31961
_SOURCE_REALTIME_TIMESTAMP=1726642861428620

La partie TRUSTED JOURNAL FIELDS du man de https://man7.org/linux/man-pages/man7/systemd.journal-fields.7.html permet de faire une recherche précise sur des champs en particulier :

# journalctl _SYSTEMD_UNIT=crond.service _PID=31961
Sep 18 03:01:01 SERVER02 CROND[31961]: (root) CMDEND (run-parts /etc/cron.hourly)

Afficher et filtrer les logs de redémarrage

Affichier les logs à partir du 1er démarrage du système :

# journalctl -b 1

Affichier les logs à partir du 2ème démarrage du système :

# journalctl -b 2

Affichier tous les logs liées à un démarrage du système :

# journalctl --list-boots

Affichier les logs à partir du démarrage actuel du système :

# journalctl -b

Affichier les logs à partir du démarrage précédent du système :

# journalctl -b -1

Exemples :

# journalctl --list-boots
IDX BOOT ID                          FIRST ENTRY                 LAST ENTRY
0 7081bf7bbe63455794e99d65994310f7 Wed 2024-08-14 10:42:46 EDT Wed 2024-09-18 15:21:02 EDT

/!\ Résumé des commandes

Commandes :

# journalctl                     // Afficher les logs perdus
# journalctl -n 2 // Afficher les 2 dernier logs perdus
# journalctl -f // Afficher les 10 derniers en temps réel
# journalctl -p err|cri|warm... // Afficher les logs en fontion de leur criticité
# journalctl -u sshd.service // Afficher les logs en fonction d'un service

// Afficher en encadrant une période
# journalctl --since today
# journalctl --since "-1 hour"
# journalctl --since "AAA-MM-DD hh:mm" --until "AAA-MM-DD hh:mm"

// Plus de détails
# journalctl -o verbose
# journalctl _SYSTEMD_UNIT=<SERVICE>.service _PID=<PID>

# journalctl -b 1 // Affichier les logs à partir du 1er démarrage du système
# journalctl -b 2 // Affichier les logs à partir du 2ème démarrage du système
# journalctl --list-boots // Affichier tous les logs liées à un démarrage du système
# journalctl -b // Affichier les logs à partir du démarrage actuel du système
# journalctl -b -1 // Affichier les logs à partir du démarrage précédent du système

Fichiers :

/run/log          // Répertoire des logs volatils

Cas Pratique

Afficher uniquement les événements de log provenant du processus systemd PID 1 :

[aline@SERVER01 ~]$ journalctl _PID=1
[...]
Aug 14 10:42:49 localhost systemd[1]: dracut mount hook was skipped because no trigger condition checks were met.
Aug 14 10:42:49 localhost systemd[1]: Starting dracut pre-pivot and cleanup hook...
Aug 14 10:42:49 localhost systemd[1]: Finished dracut pre-pivot and cleanup hook.
Aug 14 10:42:49 localhost systemd[1]: Starting Cleaning Up and Shutting Down Daemons...
Aug 14 10:42:49 localhost systemd[1]: Stopped target Network.
Aug 14 10:42:49 localhost systemd[1]: Stopped target Timer Units.
Aug 14 10:42:49 localhost systemd[1]: dbus.socket: Deactivated successfully.
Aug 14 10:42:49 localhost systemd[1]: Closed D-Bus System Message Bus Socket.
Aug 14 10:42:49 localhost systemd[1]: dracut-pre-pivot.service: Deactivated successfully.
Aug 14 10:42:49 localhost systemd[1]: Stopped dracut pre-pivot and cleanup hook.
Aug 14 10:42:49 localhost systemd[1]: Stopped target Initrd Default Target.
Aug 14 10:42:49 localhost systemd[1]: Stopped target Basic System.
Aug 14 10:42:49 localhost systemd[1]: Stopped target Initrd Root Device.

Afficher tous les événements de log provenant du service système avec un UID de 1337 :

[aline@SERVER01 ~]$ journalctl _UID=1337
Aug 14 12:40:56 SERVER01.org dbus-broker-lau[727]: Ready

Afficher les logs avec une priorité warning ou supérieure avec journalctl -p warning :

[aline@SERVER01 ~]$ journalctl -p warning
[...]
Aug 14 10:42:46 localhost kernel: core: CPUID marked event: 'cpu cycles' unavailable
Aug 14 10:43:04 localhost.localdomain systemd[1]: iscsi.service: Unit cannot be reloaded because it is inactive.
Aug 14 10:43:05 localhost.localdomain kernel: block dm-0: the capability attribute has been deprecated.
Aug 14 10:43:07 localhost.localdomain /usr/sbin/irqbalance[857]: Cannot change IRQ 0 affinity: Input/output error
Aug 14 10:43:07 localhost.localdomain /usr/sbin/irqbalance[857]: IRQ 0 affinity is now unmanaged
Aug 28 09:09:53 SERVER02 sshd[9126]: fatal: Timeout before authentication for 192.168.1.105 port 36298
Aug 28 10:31:59 SERVER02 sudo[9559]: pam_unix(sudo:auth): auth could not identify password for [aline]
Aug 28 10:32:01 SERVER02 sudo[9559]: aline : user NOT in sudoers ; TTY=pts/0 ; PWD=/home/aline ; USER=root ; COMMAND=/bin/cat /var/log/secure
Aug 28 10:33:00 SERVER02 sudo[9619]: pam_unix(sudo:auth): conversation failed
Aug 28 10:33:00 SERVER02 sudo[9619]: pam_unix(sudo:auth): auth could not identify password for [aline]
Aug 28 10:53:41 SERVER02 sshd[9812]: fatal: Timeout before authentication for 192.168.1.109 port 57728
Sep 08 12:43:24 SERVER02 sudo[25805]: pam_unix(sudo:auth): conversation failed
Sep 08 12:43:24 SERVER02 sudo[25805]: pam_unix(sudo:auth): auth could not identify password for [aline]
Sep 08 12:43:25 SERVER02 sudo[25805]: aline : user NOT in sudoers ; TTY=pts/0 ; PWD=/home/aline ; USER=root ; COMMAND=/bin/cat /etc/sudoers
Sep 18 12:36:32 SERVER02 root[32384]: Hello je t'ai à l'oeil !
[...]

Afficher tous les logs enregistrés au cours des 2 dernières heure :

[aline@SERVER01 ~]$ journalctl --since "-2h"
[...]
Sep 18 14:33:05 SERVER02 dnf[32501]: There are no enabled repositories in "/etc/yum.repos.d", "/etc/yum/repos.d", "/etc/distro.repos.d".
Sep 18 14:33:05 SERVER02 systemd[1]: dnf-makecache.service: Deactivated successfully.
Sep 18 14:33:05 SERVER02 systemd[1]: Finished dnf makecache.
Sep 18 15:01:01 SERVER02 CROND[32507]: (root) CMD (run-parts /etc/cron.hourly)
Sep 18 15:01:01 SERVER02 run-parts[32510]: (/etc/cron.hourly) starting 0anacron
Sep 18 15:01:01 SERVER02 run-parts[32516]: (/etc/cron.hourly) finished 0anacron
Sep 18 15:01:01 SERVER02 CROND[32506]: (root) CMDEND (run-parts /etc/cron.hourly)
Sep 18 15:20:52 SERVER02 NetworkManager[923]: <info> [1726687252.9025] dhcp4 (ens192): state changed new lease, address=192.168.1.110
Sep 18 15:20:52 SERVER02 systemd[1]: Starting Network Manager Script Dispatcher Service...
Sep 18 15:20:52 SERVER02 systemd[1]: Started Network Manager Script Dispatcher Service.
Sep 18 15:21:02 SERVER02 systemd[1]: NetworkManager-dispatcher.service: Deactivated successfully.

Afficher tous les logs provenant du service sshd depuis 09:00:00 avec journalctl --since :

[aline@SERVER01 ~]$ journalctl --since 9:00:00 _SYSTEMD_UNIT="sshd.service"
Sep 18 12:18:51 SERVER02 sshd[32304]: Accepted password for root from 192.168.1.105 port 42998 ssh2
Sep 18 12:18:51 SERVER02 sshd[32304]: pam_unix(sshd:session): session opened for user root(uid=0) by (uid=0)

Documentation

MAN journalctl(1)
MAN systemd.log-fields(7)
MAN systemd.time(7)

https://access.redhat.com/documentation/en-us/red_hat_enterprise_linux/9/htmlsingle/configuring_basic_system_settings/index#troubleshooting-problems-usinglog-files_getting-started-with-system-administration

> Partager <