Domanda :Come reindirizzare la cronologia dei comandi della shell a Syslog?
Ci sono diversi metodi per farlo. Puoi provare con uno qualsiasi dei 3 metodi seguenti:
Metodo 1 – tramite il servizio rsyslog
Per utilizzare rsyslog per registrare ogni comando della shell, segui i passaggi seguenti:
1. Creare un nuovo file di configurazione rsyslog e definire il percorso del file di registro. Ad esempio:/var/log/commands.log .
# vi /etc/rsyslog.d/bash.conf local6.* /var/log/commands.log
2. Modifica il ~/bashrc dell'utente . Nota:devi modificare ogni ~/bashrc di ogni utente che ha bisogno di tali log.
# vi ~/.bashrc whoami="$(whoami)@$(echo $SSH_CONNECTION | awk '{print $1}')" export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$whoami [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"'
Ad esempio:
[root@hostname ~]# cat ~/.bashrc | tail -n2 whoami="$(whoami)@$(echo $SSH_CONNECTION | awk '{print $1}')" export PROMPT_COMMAND='RETRN_VAL=$?;logger -p local6.debug "$whoami [$$]: $(history 1 | sed "s/^[ ]*[0-9]\+[ ]*//" ) [$RETRN_VAL]"' [root@hostname ~]#
3. Riavvia il servizio rsyslog
# systemctl restart rsyslog
Tutto fatto. Vedi l'esempio di formato del registro di seguito:
[root@hostname ~]# date Thu Apr 9 00:26:11 EDT 2020 [root@hostname ~]# cat /etc/redhat-release Red Hat Enterprise Linux Server release 7.9 (Maipo)
[root@hostname ~]# tail -2 /var/log/commands.log Apr 9 00:26:11 hostname root: root@x.x.x.x [1643]: date [0] Apr 9 00:26:18 hostname root: root@x.x.x.x [1643]: cat /etc/redhat-release [0] [root@hostname ~]#
Metodo 2 – tramite l'opzione della shell bash
1. Aggiungi "shopt -s syslog_history ' nel file di avvio a livello di sistema /etc/profile o nel file di inizializzazione personale ~/.bash_profile. Ad esempio:
[root@hostname ~]# cat /etc/profile | grep shopt shopt -s syslog_history
2. Esci e accedi nuovamente per confermare questa opzione.
3. Esempio di registro:
[root@hostname ~]# pwd /root [root@hostname ~]# date Thu Apr 9 01:26:46 EDT 2020
[root@hostname ~]# tail -2 /var/log/messages Apr 9 01:26:46 hostname -bash: HISTORY: PID=1345 UID=0 date Apr 9 01:26:52 hostname -bash: HISTORY: PID=1345 UID=0 tail -2 /var/log/messages
[bob@hostname ~]$ tail -f /var/log/messages Apr 9 01:26:45 hostname -bash: HISTORY: PID=1345 UID=0 pwd Apr 9 01:26:46 hostname -bash: HISTORY: PID=1345 UID=0 date Apr 9 01:26:52 hostname -bash: HISTORY: PID=1345 UID=0 tail -2 /var/log/messages
Metodo 3 – tramite comando script
Inoltre, se vuoi registrare solo una singola sessione del terminale, prova semplicemente il comando "script" come di seguito, è anche facile da usare e molto utile.
1. Per iniziare la registrazione, esegui:
# script /tmp/screen.log
2. Ora puoi avviare i tuoi comandi bash. Una volta terminato, puoi uscire:
# exit
Quindi salverà tutta la sessione in un file /tmp/screen.log
3. Verifica le uscite:
# cat /tmp/screen.log
Ad esempio:
[root@hostname ~]# script /tmp/screen.log Script started, file is /tmp/screen.log [root@hostname ~]# date Thu Apr 9 00:28:26 EDT 2020 [root@hostname ~]# whoami root [root@hostname ~]# exit exit Script done, file is /tmp/screen.log
[root@hostname ~]# cat /tmp/screen.log Script started on Thu 09 Apr 2020 12:28:23 AM EDT [root@hostname ~]# date Thu Apr 9 00:28:26 EDT 2020 [root@hostname ~]# whoami root [root@hostname ~]# exit exit Script done on Thu 09 Apr 2020 12:28:42 AM EDT [root@hostname ~]#