通过在/etc将以下代码添加到profile文件底部即可实现:
[root@test ~]# 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}
--------------------添加代码完成------------------------------
[root@test ~]# source /etc/profile #该命令使上述配置生效
---------------------------------------配置结束-------------------------------------------------
---------------------------------------查看操作记录日志-------------------------------------------------
[root@test ~]# cd /opt/history/
[root@test history]# ls
nanbo root
[root@test history]# cd root/
[root@test root]# ll
[root@localhost /opt/history/root]# cat 文件名称可以查看操作记录
————————————————