按时按登录IP记录Linux所有用户操作日志的方法(附脚本)
时间:2014-12-05 18:22 来源:linux.it.net.cn 作者:IT
Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了。以下方法可以实现通过记录登陆IP地址和所有用户登录所操作的日志记录!
在/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
本脚本在CentOS5.5、CentOS6.2测试成功!
(责任编辑:IT)
Linux用户操作记录一般通过命令history来查看历史记录,但是如果因为某人误操作了删除了重要的数据,这种情况下history命令就不会有什么作用了。以下方法可以实现通过记录登陆IP地址和所有用户登录所操作的日志记录! 在/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
本脚本在CentOS5.5、CentOS6.2测试成功! |