Possible Duplicate:
using history command with ssh and getting output with time stamps

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

ssh -i private_key user@ip 'export HISTFILE=~/.bash_history;export HISTTIMEFORMAT='%D-%T' ;set -o history; history' > myfile.txt

When i first login and then use history command, i get all details including date and time. when i use this code however, instead of getting the time at which the command was executed, i get the present time(i.e if i execute this code at 3 o clock all commands will have their timestamps as 3 o clock)

  • yeah it is. but there by mistake i thought i had found the solution. didnt notice the timestamps were incorrect. So started this thread. – user1356163 May 21 '12 at 11:27
  • Feel free to edit your original question to update or correct it. It will then be bumped and everyone gets to see it again. No need for a new question :) – Der Hochstapler May 21 '12 at 11:29
  • k. @OliverSalzburg it would be more helpful if u would remove the earlier question and let this one stay. – user1356163 May 21 '12 at 11:49
  • Check here for the solution. superuser.com/questions/962001/… – Rajasekhar Aug 26 '15 at 6:41

As I said in my answer to your more or less identical question:

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.

So: look at the remote ~/.bash_history file. Does it include timestamps on every second line? If not, you have not saved any time stamp information for the commands, and cannot get it displayed either.

You need to set the HISTTIMEFORMAT environment variable on the remote location for every session that you want to log the times for. As a suggestion in e.g. ~/.bash_profile (see the "Invocation" section in man bash for more info on which files are read during startup).

  • not more or less, its the same. i thought i would try it out here, since nothing over there worked :D – user1356163 May 21 '12 at 11:23
  • yeah it has timestamps but with the hash followed by epoch time. i tried setting the histtimeformat variable in .profile .bash_profile /etc/profile. still didnt work. I set HISTTIMEFORMAT by exporting it in these files – user1356163 May 21 '12 at 11:24
  • You should have the times in epoch format, it is converted and presented in a different way depending on HISTTIMEFORMAT. Just having HISTTIMEFORMAT set will save the epoch times; its value decides how it is presented to the user. – Daniel Andersson May 21 '12 at 15:35
  • Still this didn't solve my purpose. ~/.bash_history file includes timestamp in every other second line and the history output from the ssh shows them as commands and timestamp value of the actual commands is set to the current time stamp. Explained here: – Rajasekhar Aug 24 '15 at 13:25
  • @ganducoder, Check herer for the solution. superuser.com/questions/962001/… – Rajasekhar Aug 26 '15 at 6:41

Not the answer you're looking for? Browse other questions tagged or ask your own question.