centos6 中history命令显示操作命令的时间和用户身份
- [root@node02~]#history
- 2942017-01-0616:46:48rootclear
- 2952017-01-0616:46:50rootll
- 2962017-01-0616:46:52rootcat
- 2972017-01-0616:46:56rootcatNginxlog_
- 2982017-01-0616:47:29rootclear
- 2992017-01-0616:47:34roothistory
- [root@node02~]#cat/etc/redhat-release
- CentOSrelease(Final)
- [root@node02~]#
而centos7中,history命令中不显示操作命令的时间和用户身份
- [root@localhostdata]#cat/etc/redhat-release
- CentOSLinuxrelease(Core)
- [root@localhostdata]#history-n10
- [root@localhostdata]#history10
- 1268\
- 1269history
- 1270cat/etc/redhat-release
- 1271clear
- 1272cat/etc/redhat-release
- 1273history
- 1274clear
- 1275cat/etc/redhat-release
- 1276history-n10
- 1277history10
- [root@localhostdata]#
解决该问题只需要在/etc/profile中添加如下变量即可:
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` "
然后运行source /etc/profile命令即可,注意引号中的空格
- [root@localhostdata]#history10
- 12732017-01-0519:40:18roothistory
- 12742017-01-0519:40:27rootclear
- 12752017-01-0519:40:29rootcat/etc/redhat-release
- 12762017-01-0519:40:35roothistory-n10
- 12772017-01-0519:40:39roothistory10
- 12782017-01-0519:41:12rootcat/etc/profile
- 12792017-01-0519:42:16rootvim/etc/profile
- 12802017-01-0519:42:26rootsource/etc/profile
- 12812017-01-0519:42:28roothistory
- 12822017-01-0519:42:42roothistory10
- [root@localhostdata]#
至此history命令输出结果格式完美解决,如果要清除历史记录,可以运用history -c,具体history用法如下:
history命令的用法及参数usage: history [-c] [-d offset] [n] or history -anrw [filename] or history -ps arg [arg...]
参数:
n :数字,要列出最近的若干命令列表
-c :将目前的 shell 中的所有 history 内容全部消除
-a :将目前新增的 history 指令新增入 histfiles 中,若没有加 histfiles ,则预设写入 ~/.bash_history
-r :将 histfiles 的内容读到目前这个 shell 的 history 记忆中
-w :将目前的 history 记忆内容写入 histfiles