【linux】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


原文地址:https://www.cnblogs.com/opensmarty/p/11347720.html

时间: 2024-07-30 07:41:39

【linux】History- Linux下定制个性化history记录的相关文章

linux下MySQL5.6安装记录

MySQL下载地址: ftp://mirror.switch.ch/mirror/mysql/Downloads/MySQL-5.6/http://mirrors.sohu.com/mysql/ 1.查看服务器版本: [[email protected] ~]# cat /etc/redhat-releaseCentOS release 6.4 (Final) [[email protected] ~]# rpm -qa|grep mysql mysql-libs-5.1.66-2.el6_3.

linux下监控用户操作记录的工具

linux下监控用户操作记录的工具: apt-get install bsdutils mkdir /opt/operation_log chmod 777 -R /opt/operation_log vi /etc/profile exec script -t 2> /opt/operation_log/$USER-$UID-date +%F-%T.data -a -q -f /opt/operation_log/$USER-$UID-date +%F-%T.log :wq source /e

Linux命令行下编辑常用的快捷键

Linux命令行编辑快捷键: history                                   显示命令历史列表 ↑(Ctrl+p)                               显示上一条命令 ↓(Ctrl+n)                               显示下一条命令 !num                                     执行命令历史列表的第num条命令 !!                            

Linux Centos6.x 下常用查询命令整理

Linux Centos6.x 下常用查询命令整理 ---- 1.系统基本信息 ---- 查看 系统版本 cat /etc/redhat-release [[email protected] ~]# cat /etc/redhat-release CentOS release 6.9 (Final) 查看 处理器架构 arch 或 uname -m [[email protected] ~]# arch x86_64 [[email protected] ~]# uname -m x86_64

Linux和Windows下ping命令详解

转:http://linux.chinaitlab.com/command/829332.html 一.Linux下的ping参数 用途 发送一个回送信号请求给网络主机. 语法 ping [ -d] [ -D ] [ -n ] [ -q ] [ -r] [ -v] [ \ -R ] [ -a addr_family ] [ -c Count ] [ -w timeout ] [ -f | -i \ Wait ] [ -l Preload ] [ -p Pattern ] [ -s PacketS

Linux内核 Documentation下的00-INDEX文档翻译

This is a brief list of all the files in ./linux/Documentation and what they contain. If you add a documentation file, please list it here in alphabetical order as well, or risk being hunted down like a rabid dog. Please try and keep the descriptions

如何在Linux桌面环境下自动启动程序?

转自:http://os.51cto.com/art/201401/426464.htm 有时候,当你登录进入到Linux桌面后,可能想自动启动某个程序.这类启动程序在你登录进入到桌面后,可以进行面向整个系统的配置(比如自动代理),或者针对特定用户的桌面定制(比如Conky). 大多数Linux桌面环境有各自的图形用户界面(GUI),让用户可以配置针对特定用户的自动启动程序或服务. 我在本教程中将介绍如何在各种Linux桌面环境下,自动启动某个程序. GNOME桌面环境 在终端中运行这个命令,启

Windows Azure+Windows+Linux多环境下为Tomcat服务申请证书及分配证书

我们上一篇介绍了Windows Azure+Windows+Linux多环境下Tomcat服务的安装及配置,今天呢,主要介绍在Windows Azure+Windows+Linux多环境下为Tomcat服务申请证书及分配,一般我们都知道所谓的Web服务的证书配置就是启用SSL(加密)功能,主要提高数据传输的安全性.如果需要在互联网上访问的web服务,生成一个reg文件,然后发给第三方证书提供方来申请证书.而我们接下来的演示是通过内部证书颁发机构来实现证书申请的.需要给内部部署一台CA服务器,CA

linux 查询目录下包含关键字的所有文件

linux查找目录下的所有文件中是否含有某个字符串 查找目录下的所有文件中是否含有某个字符串 find .|xargs grep -ri "IBM" 查找目录下的所有文件中是否含有某个字符串,并且只打印出文件名 find .|xargs grep -ri "IBM" -l 1.正则表达式    (1)正则表达式一般用来描述文本模式的特殊用法,由普通字符(例如字符a-z)以及特殊字符(称为元字符,如/.*.?等)组成.   (2)基本元字符集及其含义       ^