Tuesday, 12 August 2014

Bash History: Display Date And Time For Each Command - Solved

Multiple users are using same OS account and you want to know who done this and who done that? Or you know that you done something on that day but you can remember what? Or you just want to know what you did on some specific day? Linux command that shows OS users previous typed command is history. But depending on Linux distro Linux command history can be useful to you or not for  this time execution command investigation.

In most Linux distros date and time are not showned in history. From my experience only SUSE SLES 11 has enabled this by default.

So lets see what is "problem"!

596  ls -la
597  cd ..
598  ls
599  yum info openssh
600  ifconfig
601  history
602  cat /etc/issue
603  history

So how to add time and date in history?
Because most users use bash shell you shoud add following line

export HISTTIMEFORMAT="%d.%m.%y. %T " 

in /etc/bashrc for  Red Hat, Fedora and CentOS distros or /etc/bash.bashrc for SUSE and Ubuntu distros. This will add in enviroment variables variable HISSTIMEFORMAT and this as result will add date and time for every typed command in bash shell. In case that user use some other shell like zsh or ksh add same line in zshrc and kshrc. Considering date and time format you should use format that you think that is best for you. Find right time format by using date command. For example time format that I use is

server# date "+%d.%m.%y. %T"
11.08.14. 21:25:08

I hope you get the point.

There is no need for restart of any service or something like that. Next time when you log in history will recorded with date and time and will look something like this:

 1018  11.08.14. 18:18:34 ls
 1019  11.08.14. 18:18:37 du . -h
 1020  11.08.14. 18:18:40 ls -lh
 1021  11.08.14. 18:18:52 history

1 comment:

Anonymous said...

That's a good tips !

Thanks !