Bash – Hide ones command history from other users on a Linux server

bashcommand historylinuxusers

When I log in via ssh on a Linux(Ubuntu) server, I notice that all the bash commands executed by other users on the server are saved in the command history. Is there a way that could allow me to hide the commands that I have typed in the command line from other users on the server?

Best Answer

There are many ways to hide your command history, but it's a bad idea to turn off history altogether as it is very useful. Here are three good ways to turn it off temporarily.

  1. Quickest solution: Type unset HISTFILE

    That will prevent all commands run in the current login session from getting saved to the .bash_history file when you logout. Note that HISTFILE will get reset the next time you login, so history will be saved as usual. Also, note that this removes all commands from the session, including ones run before you typed unset HISTFILE, which may not be what you want. Another downside is that you cannot be sure you did it right until you logout as bash will still let you use the up arrow to see previous commands.

  2. Best solution: type a space before a command

    Try it and then hit up arrow to see if it got added to your history. Some sites have it already set up so that such commands are not saved. If it does not work, add the line export HISTCONTROL=ignoreboth to your .bashrc file. When you login in the future, commands that start with a space will be forgotten immediately.

  3. Easiest to remember: Type sh

    That will start a subshell with the original Bourne shell. Any commands written in it (until you exit) will not be saved in your history. Anybody looking at your history file will be able to see that you ran sh (which is suspicious), but not see what you ran after that.

There are many other ways of doing this. You can even tell bash which commands to never remember (HISTIGNORE). See the man page for bash(1) and search for HIST to see lots of possibilities.

Related Question