您的位置:首页 > 运维架构 > Linux

linux 实现通过记录登陆后的IP地址和某用户名所操作的历史记录

2010-12-15 15:08 946 查看
在linux系统的环境下,不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令history来查看历史记录,可是假如一台服务器多人登陆,一天因为某人误操作了删除了重要的数据。这时候通过查看历史记录(命令:history)是没有什么意义了。那有没有什么办法实现通过记录登陆后的IP地址和某用户名所操作的历史记录呢?答案:有的。

通过在/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/dbasky ]

then

mkdir /tmp/dbasky

chmod 777 /tmp/dbasky

fi

if [ ! -d /tmp/dbasky/${LOGNAME} ]

then

mkdir /tmp/dbasky/${LOGNAME}

chmod 300 /tmp/dbasky/${LOGNAME}

fi

if [ ! -d /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt ]

then

touch /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt

chmod 600 /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt

fi

export HISTSIZE=4096

#DT=`date "+%Y%m%d_%H%M%S"`

export HISTFILE="/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky"

chmod 600 /tmp/dbasky/${LOGNAME}/*dbasky 2>/dev/null

if [ -a "/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky" ]

then

cat "/tmp/dbasky/${LOGNAME}/${USER_IP} dbasky" | perl -pe 's/(\d+)/localtime($1)/e' >> /tmp/dbasky/${LOGNAME}/SIP=${USER_IP}.txt

fi

其实通过上面的代码不能看出来,在系统的/tmp新建个dbasky目录,在目录中记录了所有的登陆过系统的用户和IP地址,是不是觉得很方便呢?我们还可以用这个方法来监测系统的安全性。

perl -pe 's/(\d+)/localtime($1)/e' .bash_history 修改UNIX time显示为UTC

本文来自CSDN博客,转载请标明出处:http://blog.csdn.net/ydt619/archive/2010/09/17/5890750.aspx
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: 
相关文章推荐