linux 精确记录用户IP以及用户操作的两种方法

27 篇文章 0 订阅

第一种,sh放入profile的方式

linux 精确记录用户IP以及用户操作命令
主要功能:
可以记录哪个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

第二种,Python代码的方式
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 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值