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

【linux】History- Linux下定制个性化history记录,第1张

概述导语  作为Linux管理员,在出现问题的时候,有时候想反查过去某段时间内 那个用户在什么时间执行过什么命令。这个时候就需要用到Linux下面的history功能     说明 作为管理员,希望能将所有的history记录保存自己方便控制的运维主机上面方便同意管理和查阅,不同主机的记录按主机ip分目录存放管理员可以查阅所有的,但是普通用户只允许创建查阅属于自己的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 })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 }‘` `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 }‘` `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
总结

以上是内存溢出为你收集整理的【linux】History- Linux下定制个性化history记录全部内容,希望文章能够帮你解决【linux】History- Linux下定制个性化history记录所遇到的程序开发问题。

如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。

欢迎分享,转载请注明来源:内存溢出

原文地址: https://outofmemory.cn/yw/1021723.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-05-23
下一篇 2022-05-23

发表评论

登录后才能评论

评论列表(0条)

保存