在 Linux 中删除终端历史记录

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6709349/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-05 05:06:10  来源:igfitidea点击:

Delete terminal history in Linux

linuxbashterminal

提问by Frank Vilea

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.

当您在 Linux 终端中使用向上键时,您可以再次使用以前的命令。很棒的功能。但是,我开始使用命令中的敏感细节将 mysql 登录到 mysql。

How can I delete that history?

我怎样才能删除那个历史记录?

采纳答案by Eddie

You can clear your bash history like this:

您可以像这样清除 bash 历史记录:

history -cw

history -cw

回答by sagi

If you use bash, then the terminal history is saved in a file called .bash_history. Delete it, and history will be gone.

如果您使用 bash,则终端历史记录将保存在名为 .bash_history 的文件中。删除它,历史将消失。

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.

但是,对于 MySQL,更好的方法是不要在命令行中输入密码。如果您只指定 -p 选项而没有值,则系统会提示您输入密码,并且不会被记录。

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:

如果不想每次都输入密码,另一种选择是将其存储在 my.cnf 文件中。创建一个名为 ~/.my.cnf 的文件,内容如下:

[client]
user = <username>
password = <password>

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.

当然,这样您的密码仍然保存在您的主目录中的纯文本文件中,就像之前保存在 .bash_history 中一样。