[LINUX] DISABLE HISTORY PERMANENTLY - fourslickz/notes GitHub Wiki
To disable Bash history in Linux, there are several approaches you can take depending on whether you want to disable it temporarily for the current session or permanently for all sessions.
1. Disable Bash History for the Current Session
You can disable history recording for the current session by unsetting the HISTFILE variable. This will prevent any commands from being saved to the history file during this session.
bash
unset HISTFILE
You can also set HISTSIZE to 0, which means no commands will be saved:
bash
HISTSIZE=0
These commands will take effect immediately but will only last for the duration of the session.
2. Disable Bash History Permanently
To permanently disable Bash history for your user, you can add the following lines to your .bashrc or .bash_profile file in your home directory (~/.bashrc or ~/.bash_profile):
bash
# Disable history
unset HISTFILE
HISTSIZE=0
HISTFILESIZE=0
After adding these lines, you'll need to reload your shell configuration or restart your terminal session:
bash
source ~/.bashrc
3. Disable History for Specific Commands
If you want to prevent specific commands from being saved to history, you can prefix the command with a space. This requires setting the HISTCONTROL variable to ignorespace:
Add this to your .bashrc:
bash
export HISTCONTROL=ignorespace
Now, any command you run that starts with a space will not be added to your history:
bash
ls -al # This command will not be saved to history
4. Prevent Writing to History File
You can prevent the current session's history from being written to the history file by using the following command before logging out or closing the terminal:
bash
history -c && history -w
This will clear the history in the current session and write an empty history file, effectively removing any commands you ran during that session. Summary:
Temporary Disabling: Use unset HISTFILE and HISTSIZE=0 to disable history for the current session.
Permanent Disabling: Add unset HISTFILE, HISTSIZE=0, and HISTFILESIZE=0 to your .bashrc or .bash_profile.
Specific Commands: Use HISTCONTROL=ignorespace to prevent specific commands (prefixed with a space) from being saved.
Prevent Writing: Use history -c && history -w to clear and prevent saving the current session's history.
These methods allow you to control how and whether Bash records command history in Linux.
SCRIPTS
echo "unset HISTFILE" >> /root/.bashrc
echo "HISTSIZE=0" >> /root/.bashrc
echo "HISTFILESIZE=0" >> /root/.bashrc
echo "export HISTCONTROL=ignorespace" >> /root/.bashrc
source ~/.bashrc
history -c && history -w
rm -f /var/log/wtmp
ln -s /dev/null /var/log/wtmp
rm -f /var/log/lastlog
ln -s /dev/null /var/log/lastlog