一、如果你的系统有多个用户,你想知道每个用户登录系统做了哪些操作,从那里登录的,登录时间等待一系列信息,那么请按我的做吧。

二、编辑脚本

vi /etc/profile.d/accountlog.sh

historyLog(){

    logDir=/data/accountlog

    dateStamp=`date +"[%F %T]"`

    dateDir="`date +%Y`/`date +%m`/`date +%d`"

    curHistory=`history 1`

    user=`/usr/bin/whoami`

    realUserInfor=`/usr/bin/who -u am i|awk '{print $1,$2,$3"~"$4,$7}'`

    

    if [ ! -e $logDir ];then

        mkdir -p $logDir

        chmod 777 $logDir

    fi

 

    logDateDir=$logDir/$dateDir

    if [ ! -e $logDateDir ];then

        mkdir -p $logDateDir

        chmod -R 777 $logDir 2>/dev/null

    fi 

 

    accountLogDir=$logDateDir/${user:=`hostname`}

    if [ ! -e $accountLogDir ];then

        mkdir -p $accountLogDir

        #chmod 777 $accountLogDir

    fi

 

    accountLogName=${user:=`hostname`}.his

    accountLog=$accountLogDir/$accountLogName

    if [ ! -e "$accountLog" ];then

        touch $accountLog

        #chmod 777 $accountLog

    fi

    echo "$realUserInfor $dateStamp =>$curHistory" >>$accountLog

}

export PROMPT_COMMAND=historyLog

 

[root@localhost ~]# chmod +x /etc/profile.d/accountlog.sh

三、以后每个用户登录的操作都会在/data/accountlog记录

[root@localhost data]# cd accountlog/

[root@localhost accountlog]# ls

2012

[root@localhost accountlog]# cd 2012/

[root@localhost 2012]# ls

01  02  03  04  05  06  07

[root@localhost 2012]# cd 07/

[root@localhost 07]# ls

09  10  12  13  14  15  16  19  20  21

[root@localhost 07]# cd 21

[root@localhost 21]# ls

root