How to clear bash history completely?
I want to clear all previous commands from the history of my server. I used history -c and it seems all things are cleared but when I ssh to the server, all the commands are still there. How can I clear them permanently?
9 Answers 9
The file ~/.bash_history holds the history.
To clear the bash history completely on the server, open terminal and type
cat /dev/null > ~/.bash_history
Other alternate way is to link ~/.bash_history to /dev/null
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.
To workaround this, use the following command (worked for me):
cat /dev/null > ~/.bash_history && history -c && exit
you can also put above command in .bashrc & .bash_logout . what it mean when you login u will have clear history & when you logout out your history will be cleared
I tried to do it on a raspberry box while connected via SSH. I added the above command ( cat /dev/null > ~/.bash_history && history -c && exit ) to both .bashrc & .bash_logout as suggested by @Qasim . Now as soon as I connect via SSH the remote host closes the connection (after printing the motd) . Help :/
Goes to show what happens when you blindly do things without reading properly and without understanding the processing and data flow. It also shows the pitfalls of bad communication. What devav2 did was run it one time, and what Qasim should have written is to remove the exit command before adding it in login script.
In every open bash shell (you may have multiple terminals open):
Why: As noted above, history -c empties the file ~/.bash_history . It is important to note that bash shell does not immediately flush history to the bash_history file. So, it is important to (1) flush the history to the file, and (2) clear the history, in all terminals. That’s what the commands above do.
For some reason this does not work on Ubuntu 14.04, probably others. It should but it doesn’t. If you issue the command «history -cw» you can confirm with the up arrow that the history isn’t there anymore, but if you start another terminal window (in Unity desktop) with shift + click on the terminal icon (I have it pinned in the launcher) the commands history are back, no matters how many times you do «history -cw». «cat /dev/null > ~/.bash_history» is the only way that worked for me.
It’s history -c and then history -w, you first clear the history then write the changes. This is easily confirmed by closing the terminal and opening it again, with -w first the commands are there again, with -w last history is effectively cleared.
execute the following commands to clear history forever
This answer would be even more helpful if it explained what these flags do (and therefore why/when they’re the right commands to use).
There’s another much simpler one: running history -c on the terminal prompt and gone are all entries in the bash_history file.
If you read the OP’s question you would know that using history -c is the exact method that led to this question.
Clear the current shell’s history:
When you log out, your current shell’s history is appended to ~/.bash_history, which is a cache of previous shells’ histories, to a maximum number (see HISTFILESIZE in «man bash»).
If you want to remove the history altogether, then you essentially have to empty out ~/.bash_history which many of the above entries have suggested. Such as:
This clears the current shell’s history and then forces the current shell’s history (empty) to overwrite ~/.bash_history. or to be more accurate, it forces it to overwrite HISTFILE (which defaults to ~/.bash_history).
Top 3 Linux Browser History Eraser Programs
If you have security concerns regarding the information you have viewed on the web, downloading a useful history deletion tool might be a good idea. Using a tool such as this, the entire history of websites that you have viewed in your web browser can be deleted at the click of a mouse, just as easily as deleting a smaller selection of visited pages.
Additionally, any files related with the site – images, adverts, scripts and the main content of the pages – can also be discarded, as can cookies that might be used to track your purchasing behaviour or your logins to personal accounts such as webmail, discussion forums, online shopping and banking and anything else that requires a login.
Of course, if any of these fail, you might also consider the built in history deletion tools that you will find among the functions of most browsers.
Bleachbit
Designed as an all-in-one tidy up tool for Linux, Bleachbit can be used to clean the Internet history as well as other databases and caches, re-instating vital hard disk space to your computer.
If you use OpenOffice.org, GIMP, Google Earth, Adobe Reader or any other tool that stores lists and databases of visited locations, completed tasks, searches or other temporary data, Bleachbit – a free tool that can be installed via Synaptic Package Manager (or in the case of Ubuntu, via the Ubuntu Software Center) – is designed to let you discard all of these, helping you to speed up your hard disk drive and maintain privacy.
From a browser point of view, Bleachbit removes cache, temporary Internet files, history, cookies and even broken shortcuts from your computer, and “vacuums” Firefox databases to improve performance.
While Linux browser history eraser options are limited, this is the one tool that you should really have installed.
Kill Flash Cookies
Anyone using Flash Player on their Linux box might be interested to know that the standard method of emptying temporary Internet files from your browser has no effect on Flash (the same is true for Windows and Mac users).
Adobe has created a cookie called Local Shared Object or LSOs which allows sites to track your movements. This clearly has various security concerns, but should come as no surprise given the number of website advertisements that use Flash.
These cookies are designed to record page requests and other session variables such as username, so obviously there is something to be concerned about here. However thanks to the developer of Kill Flash Cookies these cookies can be discarded.
You can get your copy of this utility from ufridman.org/kfc.html; note that multiple cleans may be necessary as apparently LSOs are difficult to delete…
Use Your Browser
If you have already tried deleting your browser history in Linux via Firefox (or whatever your browser of choice is) and found the results disappointing, it is possible that you weren’t doing it correctly.
There is more to opening Firefox and going to History > Show all History and manually deleting the sites that you have already visited; in fact, you could be there all day if you choose that option!
Instead, use the deletion tools available in Edit > Preferences > Privacy and change the Firefox will: Remember history option to Use custom settings for history in order to change how Firefox behaves when browsing, and then employ Tools > Clear Recent History… to delete your history and Tools > Start Private Browsing which prevents Firefox from retaining a copy of your visited sites.
Note that none of these tools will prevent a network admin from checking your history of visited websites, as these will be recorded on the network gateway.
References
How do I clear the terminal History?
I am using Linux Mint 17.1 Rebecca for about 2 days and accidentally typed my password into the terminal which is now displayed in the history list of commands I have previously typed. I want to clear the terminal history completely. I have tried using the following commands in the terminal which I thought would clear the history forever but they do not:
history -c reset tput reset
The above commands «will» clear the history from the terminal but when I exit and bring up a new one all my previous history is still there and can all be listed again using the — history command and also by pressing the UP arrow on my keyboard. I do not want this to happen until I have totally cleared my history, then I want to continue using it. How can I clear my terminal history completely — forever and start fresh? Please Note: I do not want to exit the terminal without saving history just clear it forever in this one instance.
@jasonwryan That alone wouldn’t solve the problem since the sed command would end up in the shell history.
I tried using the code from @jasonwryan but I got: sed: -e expression #1, char 0: no previous regular expression which I think I know why and that lead me to come up with this from a search and some messing around: cat /dev/null > ~/.bash_history && history -c && exit
7 Answers 7
reset or tput reset only does things to the terminal. The history is entirely managed by the shell, which remains unaffected.
history -c clears your history in the current shell. That’s enough (but overkill) if you’ve just typed your password and haven’t exited that shell or saved its history explicitly.
When you exit bash, the history is saved to the history file, which by default is .bash_history in your home directory. More precisely, the history created during the current session is appended to the file; entries that are already present are unaffected. To overwrite the history file with the current shell’s history, run history -w .
Instead of removing all your history entries, you can open .bash_history in an editor and remove the lines you don’t want to keep. You can also do that inside bash, less conveniently, by using history to display all the entries, then history -d to delete the entries you don’t want, and finally history -w to save.
Note that if you have multiple running bash instances that have read the password, each of them might save it again. Before definitively purging the password from the history file, make sure that it is purged from all running shell instances.
Note that even after you’ve edited the history file, it’s possible that your password is still present somewhere on the disk from an earlier version of the file. It can’t be retrieved through the filesystem anymore, but it might still be possible (but probably not easy) to find it by accessing the disk directly. If you use this password elsewhere and your disk gets stolen (or someone gets access to the disk), this could be a problem.
How to clear all history in linux/ubuntu terminal or bash permanently? [closed]
When you use the up key in a Linux terminal, you can use previous commands again. Great feature. However, I started logging mysql into mysql with the sensitive details in the command. How can I delete that history permanently?
Since this question is closed I can’t add this as an answer. You can tell bash not to save any history for a particular session with this command: export HISTFILE=/dev/null
2 Answers 2
You can clear your bash history like this:
Really like this answer — this takes effect immediately, rather than deleting .bash_history which requires the shell to be restarted to take effect.
Does not work on my Ubuntu 14.04 machine. History just appears with new terminal. All that worked is >~/bash_history . Have to restart terminal for this though.
Also works on Mac OS X El Capitan (tested on version 10.11.2), but you have to add that following line to your ~/.bash_profile : export SHELL_SESSION_HISTORY=0 , then do a source ~/.bash_profile and to finish quit and restart your Terminal app. If you want to understand what this export command does, then you should definitely check this following link: superuser.com/questions/950403/…
isn’t only -c enough? Manuals says -c clears the history list by deleting all of the entries. -w wirtes the current history to history file and append them to history list. Just -c works fine.
If you use bash, then the terminal history is saved in a file called .bash_history. Delete it, and history will be gone.
However, for MySQL the better approach is not to enter the password in the command line. If you just specify the -p option, without a value, then you will be prompted for the password and it won’t be logged.
Another option, if you don’t want to enter your password every time, is to store it in a my.cnf file. Create a file named ~/.my.cnf with something like:
Make sure to change the file permissions so that only you can read the file.
Of course, this way your password is still saved in a plaintext file in your home directory, just like it was previously saved in .bash_history.