Hisry commands are available on both inux and unix to query the history of previously executed commands
However, this record does not contain a time item
So you can only see commands, but you don't know when to execute them
Here's how history records time:
Step 1: Check to see if your system supports it
Note: This method is only valid for bash-3.0 and above
Executing rpm-q bash displays the version of Bash
[root@localhost ~]# rpm -q bash bash-4.2.46-20.el7_2.x86_64
You can see that there is no problem with the bash version on my system
Step 2: Edit the / etc/bashrc file as root user and add the following
HISTFILESIZE=2000
HISTSIZE=2000
HISTTIMEFORMAT="%Y%m%d-%H%M%S: "
perhaps
HISTTIMEFORMAT="%Y%m%d %T "
vim /etc/bashrc HISTFILESIZE=5000 HISTSIZE=5000 HISTTIMEFORMAT="%Y%m%d-%H%M%S: "
Effective execution
Command:
export HISTTIMEFORMAT
[root@localhost ~]# export HISTTIMEFORMAT
Note: The above method can also be executed with 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
Step 3: Exit the current system and log in again
Exit after saving, close the current shell, and log in again
At this point, in the ~/.bash_history file, there is a time to record the execution of the command
Use the cat command to display this file, but you will see that the time is not displayed on the day of the year
Instead, it is displayed as unix time:
[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. It's been a temporary period since January 1, 1970, and how many seconds have passed since then
Since 1969 was the birth of the unix system, January 1, 1970 was defined as the beginning of the birth of the unix system.
linux systems, because of their similarity to unix systems, also record time in this way
In order to display the time in the way of the human year, month and day, execute the history command to view it, 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 when and what commands were executed.
Be careful:
This method must set this parameter as soon as the server is newly installed.
If the server has been running for a long time before adding this parameter, the previous command history does not show time.
Parametric interpretation
HISTFILESIZE=5000
HISTSIZE=5000
==HISTFILESIZE==Defines the total number of records that hold commands in.bash_history, which can be interpreted as having at most one HISTFILESIZE line in the.bash_history file
==HISTSIZE==The number of records that define the output of the history command, the last HISTSIZE line in the output.bash_history file
Note:
The default number of linux history commands is 1000
The default value reserved by the history command is 1000
Since we have just modified the value of these two configurations to 5000, let's see if they are valid. Note that you need to exit the current system or bash and log in again to be valid.
[root@localhost ~]# echo $HISTFILESIZE 5000 [root@localhost ~]# echo $HISTSIZE 5000
What if we only need to keep 200 for safety?We can temporarily modify the maximum number of reserved entries: HISTSIZE=200, which changes to 200 entries, but after restarting the server, it is restored.
If you want to keep 200, we need to modify his environment variables at / etc/profile as follows:
[root@localhost ~]# vim /etc/profile HISTSIZE=5000 source /etc/profile
In addition to the above methods, sed can also be used to modify directly.The commands are as follows:
[root@localhost ~]# sed -i 's/^HISTSIZE=1000/HISTSIZE=5000/' /etc/profile [root@localhost ~]# source /etc/profile
This way, even after restarting the server, the history command retains 5000 histories until the next HISTSIZE variable modification.
Be careful!!!
Modifying the / etc/bashrc file may not take effect in the departmental Ali Cloud server. Modifying the / etc/profile file is now possible:
[root@localhost ~]# vim /etc/profile export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S " export HISTSIZE=5000 [root@localhost ~]#source /etc/profile
Parameters:
export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S "
export HISTSIZE=5000