Linux下查看用户登陆后的操作记录

发表于:2009-11-24 10:54

字体: | 上一篇 | 下一篇 | 我要投稿

 作者:未知    来源:51Testing软件测试网采编

#
Linux
#
linux

  在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

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

《2023软件测试行业现状调查报告》独家发布~

精彩评论

  • mary1
    2011-12-07 16:55:30

    报错啊,麻烦帮忙解决下,谢谢哈
    下面是错误信息:
    Last login: Wed Dec  7 16:42:48 2011 from 192.168.0.9
    -bash:   history: command not found
    -bash:   USER_IP=192.168.0.9: command not found
    -bash:   if: command not found
    -bash:   then: command not found
    -bash:   USER_IP=localhost.localdomain: command not found
    -bash:   fi: command not found
    -bash:   if: command not found
    -bash:   then: command not found
    -bash:   mkdir: command not found
    -bash:   chmod: command not found
    -bash:   fi: command not found

    -bash:   if: command not found
    -bash:   then: command not found
    -bash:   mkdir: command not found
    -bash:   chmod: command not found
    -bash:   fi: command not found
    -bash:   export: command not found
    date: invalid date ` %Y%m%d_%H%M%S'
    -bash:   DT=: command not found
    -bash:   export: command not found

关注51Testing

联系我们

快捷面板 站点地图 联系我们 广告服务 关于我们 站长统计 发展历程

法律顾问:上海兰迪律师事务所 项棋律师
版权所有 上海博为峰软件技术股份有限公司 Copyright©51testing.com 2003-2024
投诉及意见反馈:webmaster@51testing.com; 业务联系:service@51testing.com 021-64471599-8017

沪ICP备05003035号

沪公网安备 31010102002173号