在centos7 中虽然可以使用history
查看历史操作,但是仅限于当前登录用户的历史操作,如果多人使用一台服务器,就必须记录多用户的操作记录了,找了一个方法如下:
- 在
/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
的文件,里面包含了以多个用户名命名的文件夹(只有相应登录过得用户才有记录),然后就可以快乐的查看人家到底做了什么事情了。