Every time you type a command into the Linux terminal—whether it is a simple cd command or a complex database query containing sensitive passwords—the Bash shell automatically records it. This feature is incredibly useful for recalling past commands using the up-arrow key, but it poses a severe security risk if you are working on a shared server.
Anyone who logs into your user account can simply type history to view a chronological list of your last 1,000 commands. If you accidentally typed an API key or a root password directly into the prompt, you must clear this history immediately.
How to Clear the Active Session History
If you want to quickly wipe the memory of the current terminal session, you can use the built-in clear flag.
- Open your terminal.
- Type the following command:
history -c
- Press Enter.
This command clears the history from the active RAM. If you press the up-arrow key right now, nothing will happen. However, this method is not sufficient for total security, because the older commands are still permanently saved in a hidden text file on your hard drive.
How to Permanently Delete the History File
By default, Bash writes all history data to a hidden file located at ~/.bash_history in your home directory.
To completely and permanently eradicate your command history, you must overwrite this file, and then clear the RAM.
- Execute the following command to securely overwrite the history file with absolute nothingness (null):
cat /dev/null > ~/.bash_history
- Now, clear the active memory of the current session so it doesn’t accidentally re-save anything when you close the window:
history -c
If you type the history command now, the terminal will only output a single line: the history command you just typed.
How to Prevent Commands from Being Saved in the Future
If you are about to type a highly sensitive command (like passing a database password as an argument) and you do not want it logged in the first place, simply add a single space before the command.
mysql -u root -pSuperSecretPassword
By default, most modern Linux distributions are configured to completely ignore any command that begins with a leading space, ensuring it is never written to the .bash_history file.