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

zlsh00 2019-12-29

修改/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

即可

效果:

20191227_10:01:45 root  history
 20191227_10:01:57 root  source /etc/profile
 20191227_10:02:13 root  mkdir /var/log/.history
 20191227_10:02:19 root  source /etc/profile
 20191227_10:02:22 root  history

相关推荐