解决CentOS 7 history命令不显示操作记录的时间和用户身份问题

前端之家收集整理的这篇文章主要介绍了解决CentOS 7 history命令不显示操作记录的时间和用户身份问题前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

centos6 中history命令显示操作命令的时间和用户身份

  1. [root@node02~]#history
  2. 2942017-01-0616:46:48rootclear
  3. 2952017-01-0616:46:50rootll
  4. 2962017-01-0616:46:52rootcat
  5. 2972017-01-0616:46:56rootcatNginxlog_
  6. 2982017-01-0616:47:29rootclear
  7. 2992017-01-0616:47:34roothistory
  8. [root@node02~]#cat/etc/redhat-release
  9. CentOSrelease(Final)
  10. [root@node02~]#

而centos7中,history命令中不显示操作命令的时间和用户身份

  1. [root@localhostdata]#cat/etc/redhat-release
  2. CentOSLinuxrelease(Core)
  3. [root@localhostdata]#history-n10
  4. [root@localhostdata]#history10
  5. 1268\
  6. 1269history
  7. 1270cat/etc/redhat-release
  8. 1271clear
  9. 1272cat/etc/redhat-release
  10. 1273history
  11. 1274clear
  12. 1275cat/etc/redhat-release
  13. 1276history-n10
  14. 1277history10
  15. [root@localhostdata]#

解决该问题只需要在/etc/profile中添加如下变量即可:

export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` "

然后运行source /etc/profile命令即可,注意引号中的空格

  1. [root@localhostdata]#history10
  2. 12732017-01-0519:40:18roothistory
  3. 12742017-01-0519:40:27rootclear
  4. 12752017-01-0519:40:29rootcat/etc/redhat-release
  5. 12762017-01-0519:40:35roothistory-n10
  6. 12772017-01-0519:40:39roothistory10
  7. 12782017-01-0519:41:12rootcat/etc/profile
  8. 12792017-01-0519:42:16rootvim/etc/profile
  9. 12802017-01-0519:42:26rootsource/etc/profile
  10. 12812017-01-0519:42:28roothistory
  11. 12822017-01-0519:42:42roothistory10
  12. [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

猜你在找的CentOS相关文章