History- Linux下定制个性化history记录

导语

作为Linux管理员,在出现问题的时候,有时候想反查过去某段时间内 那个用户在什么时间执行过什么命令。这个时候就需要用到Linux下面的history功能


说明

作为管理员,希望能将所有的history记录保存自己方便控制的运维主机上面方便同意管理和查阅,不同主机的记录按主机ip分目录存放

管理员可以查阅所有的,但是普通用户只允许创建查阅属于自己的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创建不同的目录存放
if [ ! -d ${historyPath}${currentip} ] 
then
    mkdir -p ${historyPath}${currentip}
    chmod -R 777 ${historyPath}${currentip}
    chmod a+t ${historyPath}${currentip}
fi

## history setting 
## 区分管理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 

附加知识点

who am i && whoami

最后编辑于
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

  • linux资料总章2.1 1.0写的不好抱歉 但是2.0已经改了很多 但是错误还是无法避免 以后资料会慢慢更新 大...
    数据革命阅读 14,414评论 2 33
  • Ubuntu的发音 Ubuntu,源于非洲祖鲁人和科萨人的语言,发作 oo-boon-too 的音。了解发音是有意...
    萤火虫de梦阅读 100,279评论 9 468
  • 内核的引导(BIOS OS Read /boot 内核文件) - 运行init(所有进程的父进程 /etc/ini...
    时待吾阅读 3,746评论 0 1
  • 我的美女领导,过着自由随性的生活。我常常不自觉的以她为榜样,却又感觉遥不可及。她皮肤白皙,五官端正,个子高挑,有一...
    清晰无言阅读 2,490评论 0 0

友情链接更多精彩内容