Linux下查看用户登陆后的操作记录及history命令显示时间

Linux下查看用户登陆后的操作记录及history命令显示时间

Filed under: 东写西读 |  

在linux系统的环境下,不管是root用户还是其它的用户只有登陆系统后用进入操作我们都可以通过命令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/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
export HISTSIZE=4096
DT=`date +” %Y%m%d_%H%M%S”`
export HISTFILE=”/tmp/dbasky/${LOGNAME}/${USER_IP}.dbasky.$DT”
chmod 600 /tmp/dbasky/${LOGNAME}/*dbasky* 2>/dev/null

可以看到在使用history命令的时候是没时间显示,不知道这个命令是什么时候所执行,这个时候可以

编辑/etc/bashrc文件,加入如下三行:

HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT=”%Y%m%d-%H%M%S: ”
export HISTTIMEFORMAT

保存后退出,关闭当前shell,并重新登录
这个时候,在~/.bash_History文件中,就有记录命令执行的时间了
用cat命令显示这个文件,但是却会看到这个时间不是年月日显示的


Del.icio.us Google书签 Digg Live Bookmark Technorati Furl Yahoo书签 Facebook 百度搜藏 新浪ViVi 365Key网摘 天极网摘 和讯网摘 博拉网 POCO网摘 添加到饭否 QQ书签 Digbuzz我挖网

3 Comments »

  1. Comment by 网页打不开 | [Reply]

    我举得linux最大的麻烦就是,root用户可以直接远程登陆。

  2. Comment by foolpig | [Reply]

    @网页打不开:
    这个可以禁止使用root用户登陆的

  3. Comment by fpig | [Reply]

    测试一下看看

RSS feed for comments on this post. TrackBack URI

Leave a comment

If you want to leave a feedback to this post or to some other user´s comment, simply fill out the form below.

(required)

(required)