How to Remove/ Disable Zsh History' Commands on Linux.

This is the ninth in a series of posts featuring Protips, tips, tricks, hacks, and secrets provided by Our Team 🙊 — We want to share our top tips for the growing and thriving Linux community out there. Because sometimes you need a little help...

How to Remove/ Disable Zsh History' Commands on Linux.

Linux’s shell history is a fantastic feature letting you recall the list of previously typed commands. But there are instances, though, when you want to disable it. You might want to disable your Z shell commands history on a production server — accessible from outside, where a potential attacker could gain access and re-read your history file to look for potential commands, services in use, or accidentally inserted passwords.

Below are a number of instructions on how to disable your history storage or remove all currently saved commands. This feature was not available in the Bourne shell.

Find out the number of commands saved in history

In the Z shell, your command history is stored in a file .zsh_history in your home directory. By default, history displays a numbered list of the 500 t0 1000 most recent commands, from oldest to newest. The HISTSIZE variable specifies the number of instructions to preserve in a history list.

$ echo "$HISTSIZE"

Output

1000

Linux shell allows you to adjust the number of commands that it stores in history. It actually has two distinct options: the HISTFILESIZE parameter configures how many commands are kept in the history file, while the HISTSIZE controls the number stored in memory for the current session.

Search for both the HISTSIZE and HISTFILESIZE parameters if not present add them and feel free to modify its values. For the purposes of this tutorial, saving 10000 lines to disk and loading the last 5000 lines into memory will work just fine.

$ nano ~/.zhrc

...
..
.
# history length HISTSIZE and HISTFILESIZE setting
HISTSIZE=5000
HISTFILESIZE=10000

Where are my history commands stored?


The history is created from the file specified by the variable HISTFILE.
/.zsh_history is the default file.

$ echo "$HISTFILE"

Output

/home/linux/.zsh_history

Avoid duplicate entries in history

To avoid duplicate entries in your history, edit your ~/.zshrc file:

$ nano ~/.zshrc

Add the following at the end

export HISTCONTROL=ignoredups


Alternatively, use the following one-liner:

$ echo "export HISTCONTROL=ignoredups" >> ~/.zshrc


To apply these settings immediately for the current shell session execute the .zshrc file:

$ . ~/.zshrc

or

$ source .zshrc

We can control these duplicates using the HISTCONTROL variable. HISTCONTROL can have the following values:

  • ignorespace - lines beginning with a space will not be saved in history.
  • ignoredups - lines matching the previous history entry will not be saved. In other words, duplicates are ignored.
  • ignoreboth - It is shorthand for "ignorespace" and "ignoredups" values. If you set these two values to HISTCONTROL variable, the lines beginning with a space and the duplicates will not be saved.
  • erasedups - eliminate duplicates across the whole history.

You can also set multiple values to the HISTCONTROL variable with colon-separated as such:

$ export HISTCONTROL=ignoredups:erasedups

Clear history

To clear both the history file and any presently unsaved commands in the history file, use the following Linux command.

ps: You will no longer be able to view history for both saved and unsaved history commands after running the aforementioned command.

$ history -c

Disable history on Linux

You may delete the HISTFILE shell variable by using the unset HISTFILE command by adding the above line to the end of the ~/.zshrc:

$ echo 'unset HISTFILE' >> ~/.zshrc

Permanently disable history using set command

Another method is to use the set builtin command with the +o history option and add it to the end of the ~/.zshrc:

$ echo 'set +o history' >> ~/.zshrc


Next time a user login to the shell it will not store any commands to the history file .zsh_history.

To apply these settings immediately for the current shell session execute the .zshrc file:

$ . ~/.zshrc

or

$ source .zshrc

Disable the history system-wide


It will disable the command history system-wide and will be effective for the newly created users.

$ echo 'set +o history' >> /etc/profile


Delete a single command in history

This will delete a single command number 66 from history :

$ history -d 66

Bonus: Clear history on a remote host


The following command will blank the history file.

$ ssh user@linuxserver "> ~/.zsh_history"



We hope you have found this post as useful and informative as we do.