History

From DikapediaV2
Jump to: navigation, search

history - This commands stores the executed commands ran by a user.


Typically, these commands are store in the user's home directory: /home/<user>/.bash_history


For root user, it will be in: /root/.bash_history

You can search where the .bash_history file is by running:

$ echo $HISTFILE
/home/ec2-user/.bash_history

# Run as root
$ echo $HISTFILE
/root/.bash_history


How to Increase the History Size


First, check current settings:

$ echo $HISTSIZE
1000

$ echo $HISTFILESIZE
1000
  • HISTSIZE - The number of commands to remember in the command history (see HISTORY below). If the value is 0, commands are not saved in the history list. Numeric values less than zero result in every command being saved on the history list (there is no limit). The shell sets the default value to 500 after reading any startup files.
  • HISTFILESIZE - The maximum number of lines contained in the history file. When this variable is assigned a value, the history file is trun‐ cated, if necessary, to contain no more than that number of lines by removing the oldest entries. The history file is also truncated to this size after writing it when a shell exits. If the value is 0, the history file is truncated to zero size. Non-numeric values and numeric values less than zero inhibit truncation. The shell sets the default value to the value of HISTSIZE after reading any startup files.

Next, to change those limits, edit ~/.bashrc file and add:

$ sudo vim ~/.bashrc

# Add these lines at the bottom
HISTSIZE=10000
HISTFILESIZE=10000
# If you want unlimited history:
HISTSIZE=
HISTFILESIZE=

Lastly, apply changes:

$ source ~/.bashrc


NOTE** The commands will be stored in .bash_history once the SSH session has ended or when the user exits from the shell. I notice it updates with the command history once I log back into the server and check .bash_history.


Amazon Linux Specifics


In Amazon Linux 2 AMIs, there is no .bash_history file even though the history command will still store the commands. You can simply create the .bash_history. And then in your .bashrc file, you must add these lines:

For example, in .bashrc file, you can add:

# for setting history length see HISTSIZE and HISTFILESIZE in bash(1)
HISTSIZE=1000
HISTFILESIZE=2000


How to view history with time stamp


https://www.cyberciti.biz/faq/unix-linux-bash-history-display-date-time/


References:
* https://unix.stackexchange.com/questions/286300/sometimes-history-commands-are-not-stored-in-bash-history