導語
作為Linux管理員芽偏,在出現(xiàn)問題的時候,有時候想反查過去某段時間內(nèi) 那個用戶在什么時間執(zhí)行過什么命令骇两。這個時候就需要用到Linux下面的
history
功能
說明
作為管理員朝群,希望能將所有的history記錄保存自己方便控制的運維主機上面方便同意管理和查閱,不同主機的記錄按主機ip分目錄存放
管理員可以查閱所有的径缅,但是普通用戶只允許創(chuàng)建查閱屬于自己的history記錄我呢見
腳本
#!/usr/bin/env bash
currentip=$(/usr/sbin/ifconfig |grep 'inet ' |grep -v '127.0.0.1' |awk '{print $2}')
historyPath="/devOps/backup/history/"
## 不同主機的記錄按IP創(chuàng)建不同的目錄存放
if [ ! -d ${historyPath}${currentip} ]
then
mkdir -p ${historyPath}${currentip}
chmod -R 777 ${historyPath}${currentip}
chmod a+t ${historyPath}${currentip}
fi
## history setting
## 區(qū)分管理root和普通用戶掺栅,普通用戶只讀
if [ $UID -ge 500 ]
then
readonly HISTFILE=${historyPath}${currentip}/$USER-$UID.log
readonly HISTFILESIZE=50000
readonly HISTSIZE=10000
readonly HISTTIMEFORMAT="%F %T `who am i |awk '{print $1}'` `whoami` "
readonly HISTCONTROL=ignoredups
shopt -s histappend
readonly PROMPT_COMMAND="history -a"
else
HISTFILE=${historyPath}${currentip}/$USER-$UID.log
HISTFILESIZE=50000
HISTSIZE=10000
HISTTIMEFORMAT="%F %T `who am i |awk '{print $1}'` `whoami` "
HISTCONTROL=ignoredups
shopt -s histappend
PROMPT_COMMAND="history -a"
fi
實際效果
1588 2016-09-12 11:23:09 root root tail -f /usr/local/tomcat/logs/dataLog/define.log |grep MasterBGirlSayHiBoy
1589 2016-09-12 11:38:00 root root cat /usr/local/tomcat/logs/dataLog/define.log |grep sendTopicMsg
1590 2016-09-12 11:39:22 root root cat /usr/local/tomcat/logs/dataLog/define.log |grep sendTopicMsg |grep '"billing":"2"'
1591 2016-09-12 11:39:26 root root cat /usr/local/tomcat/logs/dataLog/define.log |grep sendTopicMsg |grep '"billing":"5"'
1592 2016-09-12 11:59:27 root root cat /etc/profile.d/history.sh