Using history command with ssh and getting output with time stamps

bashhistorysshunix

Basically i want to get a record of all commands executed on my ssh server, with their times.
The codes i have tried are :

ssh user@ip 'export HISTFILE=~/.bash_history; set -o history; history'

I got all commands,but i dont get time stamps with this which is expected i guess.

When i first login and then use history command, i get all details including date and time. I would like to get all details when i use history command with ssh. How should i go about it?
OH yeah, and if possible i would like to get the history without needing to export HISTFILE.(IF POSSIBLE)
Installing packages is not an option.

Best Answer

Two things:

  1. You need to also export e.g.

    HISTTIMEFORMAT='%F %T '
    

    to set display command history times.

  2. If you have not set the HISTTIMEFORMAT environment variable when using a session, the command times will not be saved and you will then of course not be able to see them. By default the current time will be printed as the command time for every command lacking a time indicator.

If you look at the ~/.bash_history file you can see if you have had time stamps activated for the sessions. Every other line will then start with a # followed by the epoch time of the next command.

Related Question