How to enable Timestamp in History Command
History command has frequently used in our day to day job to check the recently executed commands in the Bash shell. This command is used to keep track of all commands that were executed on a Linux server. By default, this command stores the last one thousand commands in their output. In Linux, the History command executed by the user can access by looking at .bash_history in the user home folder.
By default, the timestamp is not available while we execute the history command. For audit and troubleshooting purposes timestamp of the executed command is very much required.
history
Sample Output
1 pwd
2 cal
3 echo "linuxyogi.com"
4 date
5 whoami
6 id
7 ls -l
8 last | head
9 fdisk -l
10 history
To enable timestamp for history command output, create a shell variable named HISTTIMEFORMAT.
export HISTTIMEFORMAT="%d-%m-%Y %T "
Format String | Description | Example |
---|---|---|
%d | Day of Month (DD) | 15 |
%m | Month of year (MM) | 06 |
%Y | Year (YYYY) | 2020 |
%T | HH:MM:SS time format, same as %H:%M:%S | 10:50:15 |
%d-%m-%Y %T | DD-MM-YYYY HH:MM:SS date and time | 15-06-2020 10:50:15 |
Now timestamp variable “HISTTIMEFORMAT” is set. Configure it permanently for single user in .bash_profile and for all users in /etc/profile. Append the following code at end of file
HISTTIMEFORMAT="%d-%m-%Y %T "
For single user
vi .bash_profile
...............
HISTTIMEFORMAT="%d-%m-%Y %T "
...............
For make changes on immediate effect, execute below command.
source ~/.bash_profile
For all users
vi /etc/profile
...............
export HISTTIMEFORMAT="%d-%m-%Y %T "
...............
For make changes on immediate effect, execute below command.
source /etc/profile
Sample Output
1 08-11-2020 06:35:42 pwd
2 08-11-2020 06:35:42 cal
3 08-11-2020 06:35:42 echo "linuxyogi.com"
4 08-11-2020 06:35:42 date
5 08-11-2020 06:35:42 whoami
6 08-11-2020 06:35:42 id
7 08-11-2020 06:35:42 ls -l
8 08-11-2020 06:35:42 last | head
9 08-11-2020 06:35:42 fdisk -l
10 08-11-2020 06:35:42 history
11 08-11-2020 06:35:42 vi .bash_profile
12 08-11-2020 06:35:42 source ~/.bash_profile
13 08-11-2020 06:16:13 history
14 08-11-2020 06:16:19 su root
15 08-11-2020 06:16:35 history
16 08-11-2020 06:22:42 su root
17 08-11-2020 06:23:24 sudo reboot
18 08-11-2020 06:32:12 history
19 08-11-2020 06:32:28 sudo vi /etc/profile
20 08-11-2020 06:33:57 source /etc/profile