需求描述:
在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