The bash history keeps a record of all commands executed by a user on the Linux command line. This allows you to easily run previously executed commands by using the “up arrow” or “down arrow” keys to scroll through the command history file.
In this article, we will show you two simple ways to clear your command-line history on a Linux system.
The major reason for removing command-line history from the Linux terminal is to prevent another user, who could be using the same account.
For instance if you have typed a command that contained a password in plain-text and you don’t want another system user or an attacker to see this password, you need to delete or clear the history file.
Take a look at the command below, here the user aaronkilik has typed the database server password on the command line.
$ sudo mysql -u root [email protected]!#@%$lab
If you look into th bash history file towards the end, you will see the password typed above in there.
$ history
Check Last Executed Commands
The bash_history file is normally located in a user’s home directory /home/username/.bash_history.
$ ls -l /home/aaronkilik/.bash_history
To remove a single line from the history file, use the -d
option. For example, if you want to clear a command where you entered clear-text password as in the scenario above, find the line number in the history file and run this command.
$ history -d 2038
To delete or clear all the entries from bash history, use the history command below with the -c
option.
$ history -c
Alternatively, you can use the command below to delete history of all last executed commands permanently in the file.
$ cat /dev/null > ~/.bash_history
Note: A normal user can only view his/her own command history, but the root user can view the command history of all other users on the system.
You can learn more about the bash history file and useful history commands here: The Power of Linux “History Command” in Bash Shell.
Always remember that all commands you run are recorded in a history file, so do not type plain-text passwords on the command line. If you have questions or thoughts to share with us, make use of the feedback form below.
Source: tecmint.com