CentOS6下记录后台操做日志的两种方式
bash
平时为了记录登陆CentOS Linux系统的操做命令,须要将操做日志记录下来,下面介绍两种方式微信
一、利用script以及scriptreplay工具编辑器
script通常默认已安装,可使用script工具记录用户在当前终端的全部的操做,已经输出到屏幕的内容。将这些信息保存到指定的文本文件中。 工具
也就是说,script命令在你须要记录或者存档终端活动时可能颇有用,记录文件会存储为文本文件,因此能够很方便地用文本编辑器打开。 spa
在使用script命令将终端的会话过程录制下来以后,可使用 scriptreplay将其录制的结果进行回放。 .net
script 的好处就在于你在终端中的全部操做、敲过的命令和打印出的结果它均可以原本来本地进行录制。日志
下面介绍如何使用scriptorm
开启记录,并输出到文本及时间节点记录文件
blog
script -t 2> test.time -a test.logip
回放的话使用
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文件查看回放录像,动态效果就不演示了
二、记录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后从新登陆验证效果
本文分享自微信公众号 - WalkingCloud(WalkingCloud2018)。
若有侵权,请联系 support@oschina.cn 删除。
本文参与“OSC源创计划”,欢迎正在阅读的你也加入,一块儿分享。