How to clear bash history completely?

Watch out! This tutorial is over 4 years old. Please keep this in mind as some code snippets provided may no longer work or need modification to work on current systems.
Tutorial Difficulty Level    

Sometimes you don’t want to leave commands in the Bash history (accessed with UP key) of your Linux server available to other users, because it may contain some sensitive data like passwords.

This tutorial may help you to control your Bash history file, which is where these commands are actually stored.

Normally, you can type the following command to clear all your Bash history:

$ history -cw
Option Description
-c Clear the history list
-w Write out the current history to the history file

 

Type the following command to remove a certain line (e.g. 352) from the Bash history file:

$ history -dw 352
Option Description
-d Delete specified line from the history

 

The problem with the above commands arise when it comes to SSH sessions. Basically, when a new SSH session is started, the previous commands are still all there in the history!

What you need to concentrate on here is that it is the file at ~/.bash_history that ACTUALLY holds the history of what you typed (everything is a file when talking about Linux, even devices – remember this).

So, to clear the bash history completely on the server, open terminal and type:

cat /dev/null > ~/.bash_history

In technical terms, “/dev/null” is a virtual device file. But unlike other virtual devices, whatever you write to “/dev/null” is discarded, forgotten, thrown into the void…

However, one annoying side-effect is that the history entries has a COPY in the memory and it will flush back to the file when you log out!!! Grrrr…

So, to workaround this, use the following command every time:

cat /dev/null > ~/.bash_history && history -c && exit

Don’t worry if you don’t fully understand it, just know that this could be the most important security tip you will ever read for a new Linux user!