查看某个IP登录用户后的历史操做

Linux用户操做记录通常经过命令history来查看历史记录,可是若是由于某人误操做了删除了重要的数据,这种状况下history命令就不会有什么做用了。如下方法能够实现经过记录登录IP地址和全部用户登陆所操做的日志记录!shell

在/etc/profile配置文件的末尾加入如下脚本代码就能够实现,下面脚本是我网上找来的,原做者不知。但原脚本的时间变量有错误,不能记录时间,本人测试发现并检查修正:安全

PS1="`whoami`@`hostname`:"'[$PWD]'
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/history ]
then
mkdir /tmp/history
chmod 777 /tmp/history
fi
if [ ! -d /tmp/history/${LOGNAME} ]
then
mkdir /tmp/history/${LOGNAME}
chmod 300 /tmp/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/tmp/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /tmp/history/${LOGNAME}/*history* 2>/dev/null

经过上面的代码能够看出来,在系统的/tmp新建个history目录(这个目录能够自定义),在目录中记录了全部的登录过系统的用户和IP地址,这也是监测系统安全的方法之一。测试

转自:http://outofmemory.cn/code-snippet/4677/time-denglu-IP-record-Linux-suo-exist-user-operation-rizhi-scriptspa