​​centos 7 查看所有登录用户的操作历史​​

文章目录

  • ​​编辑文本​​
  • ​​在底部写入​​
  • ​​使它生效​​
  • ​​查看历史记录/opt/history​​

编辑文本

vim /etc/profile

在底部写入

#记录每个用户的操作信息
export PS1='[\u@\h \w]# '
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 /opt/history ]
then
mkdir /opt/history
chmod 777 /opt/history
fi
if [ ! -d /opt/history/${LOGNAME} ]
then
mkdir /opt/history/${LOGNAME}
chmod 300 /opt/history/${LOGNAME}
fi
export HISTSIZE=4096
DT=`date +"%Y%m%d_%H%M%S"`
export HISTFILE="/opt/history/${LOGNAME}/${USER_IP} history.$DT"
chmod 600 /opt/history/${LOGNAME}/*history* 2>/dev/null

使它生效

source /etc/profile

查看历史记录/opt/history

/opt/history