linux 记录用户操作记录日志

####################################################################################
history
USER_IP=$(who am i 2>/dev/null | awk '{print $NF}' | sed -e 's/[()]//g')
if [ "$USER_IP" = "" ];then
USER_IP=`hostname`
fi

if [ ! -d /tmp/user_history ];then
mkdir /tmp/user_history
chown root.root /tmp/user_history
chmod 777 /tmp/user_history
chattr +a /tmp/user_history
fi

if [ ! -d /tmp/user_history/${LOGNAME} ];then
mkdir -p /tmp/user_history/${LOGNAME}
fi

export HISTTIMEFORMAT='%F %T '
export HISTSIZE='40960'
time=$(date '+%Y%m%d-%H:%M:%S')
export HISTFILE="/tmp/user_history/${LOGNAME}/${USER_IP}[$time]"
chmod 600 /tmp/user_history/${LOGNAME}/*history* 2>/dev/null
####################################################################################

原文地址:https://www.cnblogs.com/louxindong/p/6216836.html