History is one the most useful command line tool for all Linux and Unix geeks. As the name suggests history command is used to keep track of all commands that were executed on a Linux server. By default, history command stores last one thousand commands in their output.
History command’s output becomes very handy during audit and in situations where we want to know which command were exactly executed to install applications and to do troubleshooting.
Sample Output of History Command,
As we can see in above history command output what commands were executed but issue is here that we do not have timestamp in output. So, we can’t say anything about the timings of command execution.
So, to enable timestamp in history command output, we must configure “HISTTIMEFORMAT” variable.
Execute following command to configure this variable
# export HISTTIMEFORMAT="%F %T "
%F –> shows Date in the format ‘YYYY-M-D’ (Year-Month-Day)
%T –> shows Time in the format ‘HH:MM:S’ (Hour:Minute:Seconds)
Now execute the history command again and verify whether we can see date and time in front of each command,
That’s it we have successfully enable timestamp in history command. To make “HISTTIMEFORMAT” variable persistent across the reboot, configure it permanently in ~.bashrc file , append the following code at end of file
# vi ~/.bashrc ………… export HISTTIMEFORMAT="%F %T " …………
Save and exit the file.
To make changes of bashrc file into the effect immediately, execute beneath command,
# source ~/.bashrc
In case if you wish to remove timestamp from history command then remove the line which contains “export HISTTIMEFORMAT=”%F %T ” from ~/.bashrc file
If you want to enable timestamp in history command for all local users too, then define the variable HISTTIMEFORMAT in /etc/profile file instead of root user’s ~/.bashrc file.
# vi /etc/profile …………… export HISTTIMEFORMAT="%F %T " …………
Save and exit the file. To make above changes into the effect , source it.
# source /etc/profile
Now run history command via local user and see whether date and time is visible in front of each command.
[[email protected] ~]# su - linuxtechi [[email protected] ~]$ history 1 2019-10-28 01:07:46 exit 2 2019-10-28 01:07:46 exit 3 2019-10-28 01:07:46 sudo vi /etc/fstab 4 2019-10-28 01:07:46 vi /etc/ntp.conf 5 2019-10-28 01:07:46 exit 6 2019-10-28 01:07:46 echo $JAVA_HOME 7 2019-10-28 01:07:46 exit ………
That’s all from this article. Please do share it among your technical friends and share your feedback and comments in the comments section below.
from Linuxtechi https://www.linuxtechi.com/enable-timestamp-history-command/