在/etc/profile中加入以下脚本
#history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTDIR=/usr/share/.history
if [ -z $USER_IP ]
then
USER_IP=`hostname`
fi
if [ ! -d $HISTDIR ]
then
mkdir -p $HISTDIR
chmod 777 $HISTDIR
fi
if [ ! -d $HISTDIR/${LOGNAME} ]
then
mkdir -p $HISTDIR/${LOGNAME}
chmod 300 $HISTDIR/${LOGNAME}
fi
export HISTSIZE=4000000
DT=`date +%Y%m%d_%H%M%S`
export HISTFILE="$HISTDIR/${LOGNAME}/${USER_IP}.history.$DT"
export HISTTIMEFORMAT="[%Y.%m.%d %H:%M:%S]"
chmod 600 $HISTDIR/${LOGNAME}/*.history* 2>/dev/null
在/etc/bashrc中加入几个环境变量,用于history命令显示用户ip等内容
#history
USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'`
HISTFILESIZE=4000
HISTSIZE=4000
HISTTIMEFORMAT="%F %T ${USER_IP} `whoami` "
export HISTTIMEFORMAT
最终效果
27 2021-10-08 10:17:19 10.211.130.22 root history
28 2021-10-08 10:17:40 10.211.130.22 root date
29 2021-10-08 10:17:47 10.211.130.22 root ls
30 2021-10-08 10:17:51 10.211.130.22 root cat /etc/profile
31 2021-10-08 10:18:25 10.211.130.22 root vim /etc/bashrc
32 2021-10-08 10:18:40 10.211.130.22 root source /etc/bashrc
33 2021-10-08 10:18:42 10.211.130.22 root history
34 2021-10-08 10:20:12 10.211.130.22 root c;ear
35 2021-10-08 10:20:13 10.211.130.22 root clear
36 2021-10-08 10:20:31 10.211.130.22 root cat /etc/profile
37 2021-10-08 10:22:24 10.211.130.22 root history