linux中,history命令,显示时间戳?操作人?IP地址?

需求描述

  在linux环境中,有的时候为了审计的需要,要记录谁什么时间从什么IP登录,执行了什么命令,bash的history命令就能够记录这些信息,但是在默认的情况下,是不记录时间的,所以呢,在这里记录下,对其进行改造。

操作过程

1.默认的history命令,只是显示行号,执行的命令

[root@testvm01 ~]# history | more
    3  ./test.sh
    4  ls
    5  vi nmon16e_x86_rhel65
    6  cdd /opt
    7  ls
    8  cd /opt
    9  ls
   10  cd softwares/
   11  ls
   12  cd ../app/
   13  ls
   14  cd test01/

备注:这样,就可以看到命令执行的历史,但是,看不到什么时间执行的,谁执行的。

2.使用HISTTIMEFORMAT变量来指定命令中增加时间戳

[root@testvm01 ~]# export HISTTIMEFORMAT="%F %T "      #注意: 在调用history命令时,行号 然后是HISTTIMEFORMAT的执行结果,然后是命令,注意%T后面有空格。
[root@testvm01 ~]# history | more
    5  2019-03-13 14:41:55 vi nmon16e_x86_rhel65 
    6  2019-03-13 14:41:55 cdd /opt
    7  2019-03-13 14:41:55 ls
    8  2019-03-13 14:41:55 cd /opt
    9  2019-03-13 14:41:55 ls
   10  2019-03-13 14:41:55 cd softwares/
   11  2019-03-13 14:41:55 ls
   12  2019-03-13 14:41:55 cd ../app/
   13  2019-03-13 14:41:55 ls
   14  2019-03-13 14:41:55 cd test01/

备注:这样就能够将命令执行的时间记录上了。或者说,给每个命令一个时间戳。或者说,本身命令历史就是有时间戳的,只是没有显示。

3.想要记录是哪个IP操作的,对HISTTIMEFORMAT变量进行改造

[root@testvm01 ~]# export HISTTIMEFORMAT="%F %T `who am i` "   #在后面,增加who am i的执行,就是哪个ip,哪个用户登录的。
You have new mail in /var/spool/mail/root
[root@testvm01 ~]# history 20
  990  2019-03-12 21:14:35 root     pts/2        2019-03-13 14:41 (192.168.53.2) vi zabbix_agent.sls 
  991  2019-03-12 21:16:08 root     pts/2        2019-03-13 14:41 (192.168.53.2) salt '*' state.sls init.zabbix_agent
  992  2019-03-12 21:16:33 root     pts/2        2019-03-13 14:41 (192.168.53.2) vi zabbix_agent.sls 
  993  2019-03-12 21:18:50 root     pts/2        2019-03-13 14:41 (192.168.53.2) salt '*' state.sls init.zabbix_agent
  994  2019-03-12 21:21:00 root     pts/2        2019-03-13 14:41 (192.168.53.2) ls
  995  2019-03-12 21:21:03 root     pts/2        2019-03-13 14:41 (192.168.53.2) cat zabbix_agent.sls 
  996  2019-03-12 21:30:02 root     pts/2        2019-03-13 14:41 (192.168.53.2) ls
  997  2019-03-12 21:30:07 root     pts/2        2019-03-13 14:41 (192.168.53.2) vi SysIni.sls 
  998  2019-03-12 21:33:38 root     pts/2        2019-03-13 14:41 (192.168.53.2) salt '*' state.sls init.SysIni
  999  2019-03-12 21:47:48 root     pts/2        2019-03-13 14:41 (192.168.53.2) salt 'testvm02' grains.items
 1000  2019-03-13 03:51:43 root     pts/2        2019-03-13 14:41 (192.168.53.2) init 0
 1001  2019-03-13 14:42:00 root     pts/2        2019-03-13 14:41 (192.168.53.2) history
 1002  2019-03-13 14:42:03 root     pts/2        2019-03-13 14:41 (192.168.53.2) history | more
 1003  2019-03-13 14:43:30 root     pts/2        2019-03-13 14:41 (192.168.53.2) export HISTTIMEFORMAT="%F %T "
 1004  2019-03-13 14:43:32 root     pts/2        2019-03-13 14:41 (192.168.53.2) history | more
 1005  2019-03-13 14:45:20 root     pts/2        2019-03-13 14:41 (192.168.53.2) history
 1006  2019-03-13 14:45:41 root     pts/2        2019-03-13 14:41 (192.168.53.2) history | more
 1007  2019-03-13 14:45:45 root     pts/2        2019-03-13 14:41 (192.168.53.2) history 20
 1008  2019-03-13 14:46:59 root     pts/2        2019-03-13 14:41 (192.168.53.2) export HISTTIMEFORMAT="%F %T `who am i` "
 1009  2019-03-13 14:47:02 root     pts/2        2019-03-13 14:41 (192.168.53.2) history 20

4.可以将这个变量加入到/etc/profile全局变量设置中,就对所有的会话都生效了

[root@testvm01 ~]# tail /etc/profile
        else
            . "$i" >/dev/null 2>&1
        fi
    fi
done

unset i
unset -f pathmunge

export HISTTIMEFORMAT="%F %T `who am i` "
[root@testvm01 ~]# source /etc/profile

备注:这样的话,对于后续登录的会话都是生效的。

疑问:在这里我就有个疑问了,为啥时间格式是%F %T呢,也找了半天,后来在bing上搜索了一个贴,里面提到,HISTTIMEFORMAT使用的是strftime函数的时间格式。

strftime函数的手册

http://man7.org/linux/man-pages/man3/strftime.3.html

里面提到

 %F     Equivalent to %Y-%m-%d (the ISO 8601 date format). (C99)
 %T     The time in 24-hour notation (%H:%M:%S).  (SU)

所以,就是需要的时间格式,自己也可以根据具体的情况,选择,是有AM/PM这种的,还是其他的方式,都可以,来源找到了,自然知道如何去配置。

文档创建时间:2019年3月13日14:53:50

猜你喜欢

转载自www.cnblogs.com/chuanzhang053/p/10523133.html