Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

解決 CentOS 7 的 history 命令不顯示時間與使用者身份的問題

編輯:關於 Android 開發

在 CentOS 6 中,history 命令輸出的每筆操作記錄都會帶上時間與使用者身份,例如:

[root@bdkyr ~]# history
294 2017-01-06 16:46:48 root clear
295 2017-01-06 16:46:50 root ll
296 2017-01-06 16:46:52 root cat hostname.sh
297 2017-01-06 16:46:56 root cat nginxlog_cut.sh
298 2017-01-06 16:47:29 root clear
299 2017-01-06 16:47:34 root history
[root@bdkyr ~]# cat /etc/redhat-release
CentOS release 6.6 (Final)

但在 CentOS 7 上,history 預設只顯示指令本身,看不到時間與使用者:

[root@bdkyr data]# cat /etc/redhat-release
CentOS Linux release 7.2.1511 (Core)
[root@localhost data]# history 10
1268
1269 history
1270 cat /etc/redhat-release
1271 clear
...

要解決這個問題,只要在 /etc/profile 中加入以下變數即可:

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

接著執行 source /etc/profile 讓設定生效(要特別注意引號內的空格)。生效後,history 的輸出就會帶上時間與使用者:

[root@bdkyr data]# history 10
1273 2017-01-05 19:40:18 root history
1274 2017-01-05 19:40:27 root clear
1275 2017-01-05 19:40:29 root cat /etc/redhat-release
...
1282 2017-01-05 19:42:42 root history 10

至此 history 的輸出格式就完美解決了。若要清除歷史記錄,可以使用 history -c。以下是 history 的常見用法與參數:

usage: history [-c] [-d offset] [n]
   or  history -anrw [filename]
   or  history -ps arg [arg...]

參數說明:

n:數字,列出最近的 n 筆指令。
-c:清除目前 shell 中的所有 history 內容。
-a:將目前新增的 history 寫入 histfiles,若未指定則預設寫入 ~/.bash_history。
-r:將 histfiles 的內容讀進目前 shell 的 history 記憶中。
-w:將目前的 history 記憶寫入 histfiles。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved