Skip to content

Instantly share code, notes, and snippets.

@adamency
Last active February 3, 2025 04:19
Show Gist options
  • Save adamency/09764f8a94bf5039987f401fd4e897a8 to your computer and use it in GitHub Desktop.
Save adamency/09764f8a94bf5039987f401fd4e897a8 to your computer and use it in GitHub Desktop.
Display History of Remote Machine through SSH
temp_hist_file="/tmp/remote-host.bash_history"
ssh <user>@<remote-host> -i <private_key_for_remote-host> "cat ~/.bash_history" > "$temp_hist_file"
(history -cr "$temp_hist_file" ; history)
#rm "$temp_hist_file"
# Original source: https://unix.stackexchange.com/a/316120
@adamency
Copy link
Author

adamency commented Feb 3, 2025

Made for bash. Easy to adapt for other shells.

The main point of this gist is too show the correct solution because no clear solution for this problem is easily found online.

Pitfalls:

  • We cannot use ssh user@host "history" as history is a bash built-in and not a binary in the path. Built-ins are only loaded during interactive sessions.
  • Using ssh user@host "bash -ic 'history'" cause issues because it cannot find a TTY
    ex. error:
bash: cannot set terminal process group (-1): Inappropriate ioctl for device
bash: no job control in this shell
  • Using ssh -t user@host is not guaranteed to work for all ssh connections (issues can arise when local and remote machines don't use the same OS, e.g. Windows -> Linux.
  • Using HISTFILE=$temp_hist_file ; history does not work since the env var is only used at shell startup time
  • Using history -cr "$temp_hist_file" ; history without parentheses will affect the real bash history of the current local user, which is unwanted.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment