Home> System Tutorial> LINUX> body text

How to hide your Linux command line history

WBOY
Release: 2024-08-17 07:34:37
Original
707 people have browsed it

If you are a Linux command line user, sometimes you may not want certain commands to be recorded in your command line history. There could be many reasons, for example, you hold a certain position in a company and you have certain privileges that you don't want others to abuse. Or maybe there are some particularly important commands that you don't want to execute by mistake while browsing the history list.

However, is there a way to control which commands go into the history list and which don't? Or in other words, can we enable incognito mode like a browser in a Linux terminal? The answer is yes, and depending on the specific goals you want, there are many ways to achieve it. In this article, we’ll discuss some proven methods.

Note: All commands appearing in this article have been tested under Ubuntu.

Different possible methods

The first two methods have been described in a previous article. If you already know this, you can skip this part. However, if you don't understand it, it is recommended to read it carefully.

1. Insert a space before the command

Yes, you read that right. By inserting a space before the command, the command will be ignored by the shell, which means it will not appear in the history. But this method has a prerequisite. It will only work if your environment variable HISTCONTROL is set to "ignorespace" or "ignoreboth". In most cases, this is the default value.

So, like the following command (here [space] means entering a space):

[space]echo "this is a top secret"
Copy after login

If you have previously executed the following command to set environment variables, the above command will not appear in the history.

export HISTCONTROL = ignorespace
Copy after login

The screenshot below is an example of this approach.

如何隐藏你的 Linux 的命令行历史

The fourth "echo" command is not recorded in the history because there is a space in front of it.

2. Disable all history of the current session

If you want to disable all history for a session, you can simply clear the value of the environment variable HISTSIZE before starting command line work. Execute the following command to clear its value:

export HISTSIZE=0
Copy after login

HISTSIZE indicates the number of commands (number of lines) that can be saved in the history list of a bash session. By default it is set to a non-zero value, for example on my computer it is 1000.

So the above mentioned command sets its value to 0, with the result that nothing will be stored in the history until you close the terminal. Remember that you also cannot see previously executed commands by pressing the up arrow key or running the history command.

3. Clear the entire history after finishing the work

This can be seen as another implementation of the solution proposed in the previous part. The only difference is to execute this command after you have finished all your work. The following is the command just mentioned:

history -cw
Copy after login

As mentioned just now, this has the same effect as the HISTSIZE method.

4. Turn off history only for your work

While the previously described methods (2 and 3) will serve the purpose, they can clear the entire history, and in many cases some may not be what we expect. Sometimes you may want to save the history until you started working on the command line. For such needs, you start executing the following commands before working:

[space]set +o history
Copy after login

Note: [space] means space. And because of the spaces, the command itself is not logged either.

The above command will temporarily disable the history function, which means that all operations you perform after this command will not be recorded in the history, but everything before this command will be recorded in the history list as is.

To re-enable the history function, execute the following command:

[Space]set -o history
Copy after login

It will restore the environment to its original state, that is, you have completed your work, and the commands after executing the above command will appear in the history.

5. Delete the specified command from the history

现在假设历史记录中已经包含了一些你不希望记录的命令。这种情况下我们怎么办?很简单。直接动手删除它们。通过下面的命令来删除:

history | grep "part of command you want to remove"
Copy after login

上面的命令会输出历史记录中匹配的命令,每一条前面会有个数字。

一旦你找到你想删除的命令,执行下面的命令,从历史记录中删除那个指定的项:

history -d [num]
Copy after login

下面是这个例子的截图。

如何隐藏你的 Linux 的命令行历史

第二个 ‘echo’命令被成功的删除了。

(如果你不希望上述命令本身也被记录进历史中,你可以在上述命令前加个空格)

同样的,你可以使用向上的箭头一直往回翻看历史记录。当你发现你感兴趣的命令出现在终端上时,按下 “Ctrl + U”清除整行,也会从历史记录中删除它。

总结

有多种不同的方法可以操作 Linux 命令行历史来满足你的需求。然而请记住,从历史中隐藏或者删除命令通常不是一个好习惯,尽管本质上这并没有错。但是你必须知道你在做什么,以及可能产生的后果。

The above is the detailed content of How to hide your Linux command line history. For more information, please follow other related articles on the PHP Chinese website!

source:linuxprobe.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!