有时候我们需要对线上用户操作记录进行历史记录待出现问题追究责任人,但系统自带的history命令用户有自行删除权限,那怎么设置可以让用户的操作记录实时记录,并保证普通用户无权删除呢?本文教你一招

1.mkdir -p /usr/local/domob/records/

  chmod 777 /usr/local/domob/records/
  chmod +t /usr/local/domob/records/
2.vi /etc/profile 在最后添加下面的代码
if [ ! -d  /usr/local/domob/records/${LOGNAME} ]
then
mkdir -p /usr/local/domob/records/${LOGNAME}
chmod 300 /usr/local/domob/records/${LOGNAME}
fi
export HISTORY_FILE="/usr/local/domob/records/${LOGNAME}/bash_history"
export PROMPT_COMMAND='{ date "+%Y-%m-%d %T ##### $(who am i |awk "{print \$1\" \"\$2\" \"\$5}") #### $(history 1 | { read x cmd; echo "$cmd"; })"; } >>$HISTORY_FILE'