使用HISTTIMEFORMAT变量来指定命令中增加时间戳
[root@localhost ~]# export HISTTIMEFORMAT="%F %T " # #注意: 在调用history命令时,行号 然后是HISTTIMEFORMAT的执行结果,然后是命令,注意%T后面有空格。
[root@localhost ~]# history | tail
991 2019-12-08 17:26:33 ls
992 2019-12-08 17:26:33 cd ..
993 2019-12-08 17:26:33 ;s
994 2019-12-08 17:26:33 ls
995 2019-12-08 17:26:33 cd lib
996 2019-12-08 17:26:33 ls
997 2019-12-08 17:26:33 su - postgresql
998 2019-12-08 17:26:33 su - postgres
999 2019-12-08 17:40:24 export HISTTIMEFORMAT="%F %T "
1000 2019-12-08 17:40:31 history | tail
[root@localhost ~]#
想要记录是哪个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
可以将这个变量加入到/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函数的时间格式。
Linux查看具体历史命令
# history | grep zabbix
history 命令添加时间戳
echo ‘export HISTTIMEFORMAT="%F %T whoami "’ >> /etc/profile
注销,重新登录
993 2017-08-11 05:07:14 root exit
994 2017-08-11 04:47:21 root w
参考链接 :
https://blog.csdn.net/weixin_34194317/article/details/93424072
history 命令添加时间戳 : https://www.jianshu.com/p/23e0632f1873