linux保存每个登录用户命令记录

linux保存每个登录用户命令记录

1、在根目录创建/env目录,并修改权限为777

2、把下面的脚本添加到/etc/profile文件尾部

history
USER=`whoami`
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 /env/log/history ]; then
    mkdir -p /env/log/history
    chmod 766 /env/log/history
fi

if [ ! -d /env/log/history/${LOGNAME} ]; then
    mkdir -p /env/log/history/${LOGNAME}
    chmod 300 /env/log/history/${LOGNAME}
fi

export HISTSIZE=4096

DT=`date +"%Y%m%d_%H:%M:%S"`

export HISTFILE="/env/log/history/${LOGNAME}/${USER}@${USER_IP}_$DT"

chmod 600 /env/log/history/${LOGNAME}/*history* 2>/dev/null

 

posted @ 2021-09-09 14:04  goodboyyd  阅读(212)  评论(0编辑  收藏  举报