[Linux] command to display the history record time

unix on inux and provides command history, command history can query before execution
, however, the record does not contain the time the project
can only see the command, but do not know what time the implementation of
the following methods is history recording time:

Step 1: Check whether the system supports

Note: This method is only valid for bash-3.0 or later

Execute rpm -q bash to display the version of bash

[root@localhost ~]# rpm -q bash
bash-4.2.46-20.el7_2.x86_64

Visible bash version of my system, there is no problem

Step two: the root user to edit / etc / bashrc file add the following

HISTFILESIZE=2000

HISTSIZE=2000

HISTTIMEFORMAT="%Y%m%d-%H%M%S: "

or

HISTTIMEFORMAT="%Y%m%d %T "

vim /etc/bashrc
HISTFILESIZE=5000
HISTSIZE=5000
HISTTIMEFORMAT="%Y%m%d-%H%M%S: "
Execution into force

command:

export HISTTIMEFORMAT

[root@localhost ~]# export HISTTIMEFORMAT

Note: The above method can also be performed using the following statement:

echo "HISTFILESIZE=5000" >> /etc/bashrc && echo "HISTSIZE=5000" >> /etc/bashrc && echo 'HISTTIMEFORMAT="%Y%m%d %T "'>> /etc/bashrc && export HISTTIMEFORMAT

[root@localhost ~]# echo "HISTFILESIZE=5000" >> /etc/bashrc && echo "HISTSIZE=5000" >> /etc/bashrc && echo 'HISTTIMEFORMAT="%Y%m%d %T "'>> /etc/bashrc && export HISTTIMEFORMAT

The third step: quit the current system, log in again

Save and exit, close the current shell, and re-log in
at this time, in ~ / .bash_history file, command execution time there is a record

This file is displayed with the cat command, but this time will see the date is not displayed
but according to unix time to show:

[root@localhost ~]#  cat ~/.bash_history
#1553077784
mkdir go
#1553077786
history
#1553080708
rpm -q bash
#1553080989
pwd
#1553081004
vim /etc/my.cnf
#1553081027
cat /etc/redhat-release 
#1553081042
exit

This time is called unix time, temporary starting from January 1, 1970, and now after a total number of seconds
since 1969, is born unix system, so January 1, 1970 is defined as the initial unix system born of time
linux because of the similarity system unix system and, in this way to completely recording time

In chronological order to show the way human time, command execution history to see, you can, for example:

[root@localhost ~]# history |more
    1  20190320 19:23:41 wget -O /etc/yum.repos.d/CentOS-Base.repo http://mirrors.aliyun.com/repo/Centos-7.repo
    2  20190320 19:23:58 yum makecache
    3  20190320 19:25:42 systemctl stop firewalld.service     
    4  20190320 19:28:01 setenforce 0
    5  20190320 19:30:56 yum install -y yum-utils
    6  20190320 19:31:05 yum-config-manager --add-repo https://download.docker.com/linux/centos/docker-ce.repo
--More--

This allows you to see what time did what command.

note:

This method must when installing a new server just fine, you set this parameter.
If you are already running a server for a long time before adding this parameter, the previous command history that is not the time display.

Attachment of parameters

HISTFILESIZE=5000

HISTSIZE=5000

== HISTFILESIZE == defines the total number of records stored in the command .bash_history, the file can be understood as only a maximum of .bash_history HISTFILESIZE line

== HISTSIZE == defines the number of records of the history command output, i.e. the output line of the last HISTSIZE file .bash_history

Note:
Linux command history default number of 1,000
defaults history command reserved 1000

Just because we have modified the configuration of these two items is 5000, this time to look is in effect, pay attention, you need to exit the current system or bash, log back in to take effect.

[root@localhost ~]# echo $HISTFILESIZE
5000
[root@localhost ~]# echo $HISTSIZE
5000

If only for safety we need to keep 200, how to do it? We can modify the maximum number of provisional reservations: HISTSIZE = 200 200 This modification became friends, but after the restart the server, and restore.

If you want to remain 200, we need to / etc / profile to modify his environment variable, as follows:
[root@localhost ~]# vim /etc/profile
HISTSIZE=5000

source /etc/profile
In addition to the above methods, you may be used to directly modify sed. Command is as follows:
[root@localhost ~]# sed -i 's/^HISTSIZE=1000/HISTSIZE=5000/' /etc/profile
[root@localhost ~]# source /etc/profile

After so even if you restart the server, the number of pieces of history to retain command of history is still 5000, until the next modification HISTSIZE variables.

note! ! !

Ali cloud server within the department, modify / etc / bashrc file might not take effect, the method used at this time to modify / etc / profile file:

[root@localhost ~]# vim /etc/profile 
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTSIZE=5000

[root@localhost ~]#source /etc/profile

parameter:

export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTSIZE=5000

Guess you like

Origin www.cnblogs.com/BabySermonizer/p/11441262.html