免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1082 | 回复: 0
打印 上一主题 下一主题

LINUX用户登录后精确命令记录 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2009-05-06 14:27 |只看该作者 |倒序浏览

主要功能:
可以记录哪个ip和时间(精确到秒)作了哪些命令
通过用户登录时候,重新定义HISTFILE
HISTFILE文件名包含登录用户名,ip,登录时间(精确到秒)等
这样即使相同的用户从不同ip、在不同的时间登录都会被记录
可以记录每条命令的开始执行时间

把下面的代码直接粘贴到/etc/profile后面就可以了
#history
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/var/log/.hist
if [ -z $USER_IP  ]
then
  USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
   mkdir -p $HISTDIR
   chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
    mkdir -p $HISTDIR/${LOGNAME}
    chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.hist.$DT"
chmod 600 $HISTDIR/${LOGNAME}/*.hist* 2>/dev/null
得到的结果,永久保存,每个用户的命令记录分目录保存
# ls -l /var/log/.hist/root/
-rw-------  1 root   root    546 2006-05-26 10:00 218.82.245.54.hist.20060526_092458
-rw-------  1 root   root    243 2006-05-28 13:28 218.82.245.54.hist.20060528_114822
-rw-------  1 root   root     10 2006-05-28 12:18 218.82.245.54.hist.20060528_121605
查看命令记录
# export HISTFILE=/var/log/.hist/root/222.72.16.204.hist.20060608_152551
# history
    1  [2006.06.24 13:22:51] vi /etc/profile
    2  [2006.06.24 13:23:25] cd /var/log/.hist
    3  [2006.06.24 13:23:26] ls -al
    4  [2006.06.24 13:23:30] cd sadmin
    5  [2006.06.24 13:23:31] ls -al
    6  [2006.06.24 13:24:22] more 58.35.169.51.hist.20060524_193219
    7  [2006.06.24 13:24:35]  222.72.16.204.hist.20060622_143133
    8  [2006.06.24 13:24:39] more  222.72.16.204.hist.20060622_143133
    9  [2006.06.24 13:24:51] hist -f  222.72.16.204.hist.20060622_143133
   10  [2006.06.24 13:24:59] history -f  222.72.16.204.hist.20060622_143133
   11  [2006.06.24 13:25:12] history   222.72.16.204.hist.20060622_143133
   12  [2006.06.24 13:25:32] man histtory
   13  [2006.06.24 13:25:38] man history
   14  [2006.06.24 13:26:00] hist
   15  [2006.06.24 13:26:04] history
   16  [2006.06.24 13:26:16] ls
   17  [2006.06.24 13:26:39] export  222.72.16.204.hist.20060622_143133
   18  [2006.06.24 13:26:59] export HISTFILE=222.72.16.204.hist.20060608_152551
   19  [2006.06.24 13:27:07] history

本文来自ChinaUnix博客,如果查看原文请点:http://blog.chinaunix.net/u/8227/showart_1919810.html
您需要登录后才可以回帖 登录 | 注册

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP