Linux本身提供last及history两个命令来查询用户的操作历史。但是当有多个用户同时进行操作的话那就显得有所不足了。我们可以通过简单的设置对Linux用户的系统级别的操作进行监控。
[root@localhost ~]# vi /etc/profile
添加以下内容:
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
if [ ! -d /tmp/usrmon ]
then
mkdir /tmp/usrmon
chmod 777 /tmp/usrmon
fi
if [ ! -d /tmp/usrmon/${LOGNAME} ]
then
mkdir /tmp/usrmon/${LOGNAME}
chmod 300 /tmp/usrmon/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/tmp/usrmon/${LOGNAME}/${USER_IP} usrmon.$DT"
chmod 600 /tmp/usrmon/${LOGNAME}/*usrmon* 2>/dev/null
实际的日志目录可以自己定义,添加了以上命令以后每个用户的操作系统级别的操作都将记录在唯一的文件中。此时通过history命令只能查到当前用户的操作记录,当用户退出以后history的记录会被清空。