让Linux的history命令显示用户名和时间

修改/etc/profile

增加

export HISTTIMEFORMAT="%Y%m%d_%T `whoami` `who am i|awk '{print $1,$5}'|sed 's/ (/@/'|sed 's/)//'` "
export PROMPT_COMMAND='history 1|{ read x y; echo $y; } >> /var/log/.history/${LOGNAME}.history'

之后

mkdir /var/log/.history
source /etc/profile

即可

效果:

 1003  20191227_10:01:45 root root@1.2.3.4 history
 1004  20191227_10:01:57 root root@1.2.3.4 source /etc/profile
 1005  20191227_10:02:13 root root@1.2.3.4 mkdir /var/log/.history
 1006  20191227_10:02:19 root root@1.2.3.4 source /etc/profile
 1007  20191227_10:02:22 root root@1.2.3.4 history
posted @ 2019-12-28 23:19  临冬城城主  阅读(1353)  评论(0编辑  收藏  举报