Linux下记录登录用户历史操作

  前言:众所周知Linux是一个可以同时让多个用户登录的操作系统,每个用户的操作都影响着Linux运行,除了要做好安全工作以外,防止人为恶意损坏也是很关键的,比如有人恶意执行危险命令,要查找就得记录所有用户名,登录用户,用户的IP地址,执行时间,执行的命令就很重要。分享一个脚本,可以记录所有用户使用过的操作命令外,还能记录登录的Key的用户,登录IP,时间等。
 

user_ip=`who -u am i 2>/dev/null | awk '{print $NF}'| sed -e 's/[()]//g'`
ssh_key=$(ssh-add  -L|awk '{print $2}' | xargs  -i grep '{}' ~/.ssh/authorized_keys |awk '{print $3}')
hisdir=/data/ops/.history
if [ -z ${user_ip} ];then
    user_ip=$(hostname)
fi

if [ ! -d ${hisdir} ];then
    mkdir -p ${hisdir}
    chmod 777 ${hisdir}
fi

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

export HISTSIZE=4000
DT=`date +%Y-%m-%d_%H:%M:%S`
export HISTFILE="${hisdir}/${LOGNAME}/${user_ip}_${ssh_key}_history.$DT"
export HISTTIMEFORMAT='%Y-%m-%d-%H:%M:%S: '
chmod 600 ${hisdir}/${LOGNAME}/*.history* 2>/dev/null

把脚本放到/etc/profile.d/目录下面,每次用户登录即可生效
效果如下:


以_分割,第一是用户的登录IP,第二个是KEY的名字后面接着是日期时间,下面是操作过的命令

相关文章

系ubuntu 下面打开终端输入:sudo apt-get install sendmail...
依家我有1个软件goagent目录(大家懂得) 放在/home/gateman/...
其实我想讲的是 cp -L关于-L参数的解释:-L, --dereferenc...
原地址:http://www.rjgc.net/control/content/content.php?...
chroot,即 change root directory (更改 root 目录)。在 li...
简单解析下, stdin就是标准输入, stdout就是标准。举个例子...