8

When we frequently use Linux command lines, effective use of history records can greatly improve work efficiency.

In the usual Linux operation process, many commands are repeated, and you definitely don't want to enter a large number of repeated commands. If you are a system administrator, you may need to audit user operations. It is very important to manage Linux command history.

Today we will introduce several practical techniques for using history in Linux to reduce repetitive commands.

1 Basic principles

图片

The history of Linux commands will be stored persistently. The default location is the .bash_history file in the current user's home directory.

When the Linux system starts a Shell, the Shell will .bash_history file and store it in the corresponding memory buffer.

The Linux commands we usually operate will be recorded in _buffer_. Including the history command management executed by the history \_history file.

When we exit the Shell, such as pressing Ctrl+D , the Shell process will write the contents of the history buffer back to the \_history file.

2 Detailed use

history the basic principles of 0607ede6a65a93, let's learn how to use it in detail.

(1) Basic usage

Enter the history command directly, you can see that all the recent commands are displayed

`$ history`
 `1  bash`
 `2  ls`
 `3  vim .bash_history`
 `4  cat .bash_history`
 `5  history`
 `6  bash`

Sometimes I don’t need to display all the history commands, only the last 10 history records. You can add the number N

`$ history 10`

Under normal circumstances, the buffer content will be saved to a file only when the Shell exits normally. If you want to actively save the history of the buffer, execute the -w option.

`$ history -w`

Of course, if you execute some sensitive command operations, you can execute -c to delete the contents of the buffer directly

`$ history -c`

(2) Repeat the command

If you want to execute some commands repeatedly, you can use ! to quickly execute the repeated commands.

For example, if you repeat the 1024th historical command, you can execute the following command

`$ !1024`

1024 This number can be viewed through history

Repeat the previous command

`$ !!`

Repeated execution of the 6th last historical command, which can be indicated by -6 , 0607ede6a65bbe means the 6th last record

`$ !-6`

(three) search history command

Sometimes, you need to repeat the last command at the beginning of a string, you can also use ! to operate, and then press Enter to execute

For example, a very long command was executed just now, and only the beginning of the command is curl . At this time, you can quickly execute the command !curl

`$ !curl`

This usage is very efficient, but there are insecure factors, because it is possible that the executed command is not what you want to execute, which is a bad thing. It can be safely executed :p

`$ !curl:p`
`curl www.sina.com.cn`

After adding :p , only the searched command is printed out. If you want to execute it, please press Up , and then press Enter.

If you only know that a certain command contains x information and does not x , you can also execute commands containing strings ?

`$ !?sina`

(4) Interactive search history command

Searching history commands in Linux can also be done interactively, which is very efficient and direct. Ctrl+R on the command line, enter the interactive interface and enter the keyword to be searched. If multiple commands are matched, you can enter Ctrl+R multiple times to switch to the last matched command.

``(reverse-i-search)`sina': echo sina``

As you can see, after I input sina, it will automatically match the most recent command that matched sina. At this time, press Enter to execute the command.

(5) Repeat the last command

Here is a summary of multiple ways to execute the last command repeatedly. You can choose the one you like.

  • !!
  • !-1
  • Ctrl+p
  • Up
  • Ctrl+R

(Six) Display time stamp

Sometimes it is necessary to audit the Linux system, it is very useful to add a time stamp to the history record.

`$ export HISTTIMEFORMAT='%F %T '`
`$ history 3`
 `46  2021-04-18 15:21:33 curl baidu.com`
 `47  2021-04-18 15:21:35 pwd`
 `48  2021-04-18 15:21:39 history 3`

As you can see, the history record has displayed the timestamp. In fact, these are not enough for auditing requirements. More detailed information can be added:

``$ export HISTTIMEFORMAT="%F %T `who -u am i 2>/dev/null| awk '{print $NF}'|sed \-e 's/[()]//g'` `whoami` "``
 `6  2021-04-18 16:07:48 113.200.44.237 root ls`
 `7  2021-04-18 16:07:59 113.200.44.237 root pwd`
 `8  2021-04-18 16:08:14 113.200.44.237 root history`

(7) Total number of control history records

By default, the Linux system can store up to 1000 historical records, which can be viewed through the HISTSIZE environment variable

`$ echo $HISTSIZE`
`1000`

For scenarios that need to be audited, 1000 history records may be too few, we can modify it to a suitable value

`$ export HISTSIZE=10000`

Note that the HISTSIZE variable can only control the number of historical records in the buffer. If you need to control the maximum number of records stored in the .bash_history file, you can control HISTFILESIZE

The above command line modification only takes effect in the current Shell environment, if you need to take effect permanently, you need to write the configuration file

`$ echo "export HISTSIZE=10000" >> ~/.bash_profile`
`$ echo "export HISTFILESIZE=200000" >> ~/.bash_profile`
`$ source ~/.bash_profile`

(8) Change history file name

Sometimes, in order to facilitate management and backup, you need to change the path and name of the history file. Simple, you can also change its file name HISTFILE

`$ echo "export HISTFILE=/data/backup/chopin.bash_history" >> ~/.bash_profile`
`$ souce ~/.bash_profile`

(9) Disable history record

In a special environment, we need to disable history

`$ echo "export HISTSIZE=0" >> ~/.bash_profile`
`$ echo "export HISTFILESIZE=0" >> ~/.bash_profile`
`$ source ~/.bash_profile`

Haha, directly set the values of the above two variables to 0 to achieve the function of disabling history records

(10) A little trick that hackers must know

Finally, I will share an unknown trick that hackers must know.

Add an extra _space_ before the command. Such a command will not be recorded in the history record. Does it feel cool?

If this technique does not work in your system, please check whether the HISTCONTROL ignorespace . It seems that the centos system does not set this value by default.

3 Summary time

In the Linux system, the history command can be very convenient to help us manage historical commands. Normally, our commands are recorded in the _buffer area first, and will be recorded in the file when the Shell exits.

The history command provides a very convenient management function. Reasonable configuration and management of history records can make your Linux system more robust and secure.

Well, old rules, caring history Xiao, let’s summarize the common methods of 0607ede6a65fbb command

  • history n : Only display the most recent n history records
  • history -c : Clear the history in the cache
  • history -w : Save the history of the buffer to a file
  • history -d N : Delete the Nth historical record

Several methods of repeatedly executing commands: !! , !-1 , !N , !string etc.

For interactive historical command search, please use the shortcut key Ctrl+R

Use several related environment variables appropriately to make your Linux system more secure:

  • HISTSIZE : Control the maximum number of buffer history records
  • HISTFILESIZE : Control the maximum number in the history file
  • HISTIGNORE : Set which commands are not recorded in history
  • HISTTIMEFORMAT : Set the time format displayed by historical commands
  • HISTCONTROL : extended control options

If in a production environment, these environment variables need to be persisted to the configuration file ~/

`export HISTCONTROL=ignoreboth`
`# ignorespace: 忽略空格开头的命令`
`# ignoredups: 忽略连续重复命令`
`# ignoreboth: 表示上述两个参数都设置`
`# 设置追加而不是覆盖`
`shopt -s histappend`
`export HISTSIZE=1000`
`export HISTFILESIZE=200000`
`export HISTTIMEFORMAT="%F %T "`
`export HISTIGNORE="ls:history"`

Originated from: Official Account-Programming Cultivation


用户bPcHjCF
1.5k 声望1k 粉丝