通过 SSH 重新连接后不同的“history”命令输出

通过 SSH 重新连接后不同的“history”命令输出

为什么在重新连接断开的 SSH 会话后,同一用户的“history”命令会得到不同的结果?

  • 我使用 putty (SSH) 连接到服务器,例如 root
  • 我的网络断开了
  • 我重新连接我的 Putty 会话
  • 当我按向上箭头键重新运行上次使用的命令时,它显示不同的命令

我猜这是因为重新连接到不同的点。我对吗?

答案1

这是因为当前会话中的命令历史记录会在注销期间刷新到磁盘上。如果您已断开连接,当您再次连接时,您将获得上次刷新的历史记录。

您还可以通过运行以下命令手动将历史记录刷新到磁盘:

history -a

参考man history

   history [n]
   history -c
   history -d offset
   history -anrw [filename]
   history -p arg [arg ...]
   history -s arg [arg ...]
          With no options, display the command history list with line numbers.  Lines listed with a * have been  modified.
          An  argument  of n lists only the last n lines.  If the shell variable HISTTIMEFORMAT is set and not null, it is
          used as a format string for strftime(3) to display the time stamp associated with each displayed history  entry.
          No intervening blank is printed between the formatted time stamp and the history line.  If filename is supplied,
          it is used as the name of the history file; if not, the value of HISTFILE is used.  Options, if  supplied,  have
          the following meanings:
          -c     Clear the history list by deleting all the entries.
          -d offset
                 Delete the history entry at position offset.
          -a     Append  the ‘‘new’’ history lines (history lines entered since the beginning of the current bash session)
                 to the history file.
          -n     Read the history lines not already read from the history file into the current history list.   These  are
                 lines appended to the history file since the beginning of the current bash session.
          -r     Read the contents of the history file and use them as the current history.
          -w     Write the current history to the history file, overwriting the history file’s contents.
          -p     Perform  history  substitution on the following args and display the result on the standard output.  Does
                 not store the results in the history list.  Each arg must be quoted to disable normal history  expansion.
          -s     Store  the  args  in the history list as a single entry.  The last command in the history list is removed
                 before the args are added.

          If the HISTTIMEFORMAT variable is set, the time stamp information associated with each history entry is  written
          to  the history file, marked with the history comment character.  When the history file is read, lines beginning
          with the history comment character followed immediately by a digit are interpreted as timestamps for the  previ-
          ous  history line.  The return value is 0 unless an invalid option is encountered, an error occurs while reading
          or writing the history file, an invalid offset is supplied as an argument to -d, or the history  expansion  sup-
          plied as an argument to -p fails.

答案2

基本上@frzie 回答了这个问题,但是,为了可能澄清,您有一个登录到 shell 会话时创建的临时历史记录文件。当您注销时,该历史文件将附加到.history存储在$HOME.

由于您尚未注销,因此未发生此追加。要对此进行测试,您可以发出命令history并将输出(包括临时历史记录)与 的输出(即中文件cat $HOME/.history的内容)进行比较。这些输出应该有所不同(即使您只看到刚刚在其中一个输出中发出的命令,而看不到另一个输出)。现在,如果您发出命令,文件应该或多或少相同。.history$HOMEhistoryhistory -a

答案3

有几个环境变量控制历史记录的行为。其中一些包括:

HISTFILE=/home/saml/.bash_history
HISTFILESIZE=1000
HISTSIZE=1000

通常会发生的情况是,您打开了多个 shell,并且最后关闭的 shell 会破坏您首先关闭的 shell 之前写入的一些条目。

看看Bash 参考手册中有关历史命令的部分了解更多信息。

相关内容