centos6配置日志外发_CentOS6下记录后台操作日志的两种方式
CentOS6下記錄后臺操作日志的兩種方式
平時為了記錄登錄CentOS?Linux系統的操作命令,需要將操作日志記錄下來,下面介紹兩種方式
1、利用script以及scriptreplay工具
script一般默認已安裝,可以使用script工具記錄用戶在當前終端的所有的操作,已經輸出到屏幕的內容。將這些信息保存到指定的文本文件中。
也就是說,script命令在你需要記錄或者存檔終端活動時可能很有用,記錄文件會存儲為文本文件,所以可以很方便地用文本編輯器打開。
在使用script命令將終端的會話過程錄制下來之后,可以使用 scriptreplay將其錄制的結果進行回放。
script 的好處就在于你在終端中的所有操作、敲過的命令和打印出的結果它都可以原原本本地進行錄制。
下面介紹如何使用script
開啟記錄,并輸出到文本及時間節點記錄文件
script -t 2> test.time -a test.log
回放的話使用
scriptreplay test.time test.log
如果要一登錄就自動利用script進行記錄,首先創建mkdir?-p /var/log/script_log/目錄
然后在/etc/profile最后追加如下腳本
if [ $UID -ge 0 ];then
exec /usr/bin/script -qaf -t 2> /var/log/script_log/$USER-$UID-`date +%Y%m%d%H%M`.time -a /var/log/script_log/$USER-$UID-`date +%Y%m%d%H%M`.out
fi
接下來退出重新登錄驗證效果
scriptreplay xxx.time xxx.out文件查看回放錄像,動態效果就不演示了
2、記錄history到日志文件的方式
建腳本文件/etc/log_history.sh腳本如下
[root@VM_Server ~]# cat log_history.sh
#!/bin/bash
history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
if [ "$USER_IP" = "" ]
then
USER_IP=`hostname`
fi
logname=`whoami`
if [ ! -d /var/log/history_log ] && [ "$logname" == "root" ]
then
mkdir -p /var/log/history_log
chmod 777 /var/log/history_log
fi
if [ ! -d /var/log/history_log/${logname} ] && [ "$logname" == "root" ]
then
mkdir /var/log/history_log/${logname}
chmod 300 /var/log/history_log/${logname}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H:%M:%S"`
export HISTFILE="/var/log/history_log/${logname}/${logname}@${USER_IP}_history_$DT"
chmod 600 /var/log/history_log/${logname}/*history* 2>/dev/null
追加到/etc/profile下
echo ". /etc/log_history.sh" >> /etc/profile
exit后重新登錄驗證效果
總結
以上是生活随笔為你收集整理的centos6配置日志外发_CentOS6下记录后台操作日志的两种方式的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python实现k core算法_Pyt
- 下一篇: java怎么获取文本里的值_怎么获取到t