GNU/Linux >> Belajar Linux >  >> Linux

Cara mencatat setiap perintah shell di Linux

Pertanyaan :Bagaimana cara mengarahkan riwayat perintah shell ke Syslog?

Ada beberapa metode untuk melakukan ini. Anda dapat mencoba salah satu dari 3 metode di bawah ini:

Metode 1 – melalui layanan rsyslog

Untuk menggunakan rsyslog untuk mencatat setiap perintah shell, cukup ikuti langkah-langkah di bawah ini:

1. Buat file konfigurasi rsyslog baru, dan tentukan jalur file log. Misalnya:/var/log/commands.log .

# vi /etc/rsyslog.d/bash.conf
local6.* /var/log/commands.log

2. Edit ~/bashrc user pengguna . Catatan:Anda perlu mengedit ~/bashrc setiap pengguna siapa pun yang membutuhkan log tersebut.

# 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]"'

Misalnya:

[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. Mulai ulang layanan rsyslog

# systemctl restart rsyslog

Semua selesai. Lihat contoh format log di bawah ini:

[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: [email protected] [1643]: date [0]
Apr 9 00:26:18 hostname root: [email protected] [1643]: cat /etc/redhat-release [0]
[root@hostname ~]# 

Metode 2 – melalui opsi bash shell

1. Tambahkan ‘shopt -s syslog_history ' ke dalam startup seluruh sistem /etc/profile atau file inisialisasi pribadi ~/.bash_profile. Misalnya:

[root@hostname ~]# cat /etc/profile | grep shopt
shopt -s syslog_history

2. Logout dan login lagi untuk mencerminkan opsi ini.

3. Contoh log:

[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

Metode 3 – melalui perintah skrip

Selain itu, jika Anda hanya ingin mencatat satu sesi terminal, coba saja perintah 'script' seperti di bawah ini, juga mudah digunakan dan sangat membantu.

1. Untuk mulai masuk, jalankan saja:

# script /tmp/screen.log

2. Sekarang Anda dapat memulai perintah bash Anda. Setelah selesai, Anda dapat keluar:

# exit

Kemudian akan menyimpan semua sesi ke file /tmp/screen.log

3. Verifikasi output:

# cat /tmp/screen.log

Misalnya:

[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 ~]#


Linux
  1. Bagaimana Cara Anda Mencatat Setiap Perintah yang Diketik?

  2. Cara menggunakan alias shell di Linux

  3. Bagaimana saya bisa menemukan versi shell saya menggunakan perintah Linux?

  1. Cara Menjalankan atau Mengulang Perintah Linux Setiap X Detik Selamanya

  2. Bagaimana cara mencetak keluaran perintah Linux ke file?

  3. Bagaimana cara membatasi penggunaan memori berdasarkan aplikasi di linux?

  1. Cara Mengganti Shell di Linux

  2. Cara Mengonfigurasi Bash Shell Terbatas di Linux

  3. Cara menggunakan fungsi shell baris perintah di Linux