如何清除Linux命令行历史记录

时间:2020-03-05 15:27:17  来源:igfitidea点击:

出于安全原因,我们可能希望清除历史文件和屏幕。
注销时,某些Linux发行版可能会清除屏幕,但其他版本没有。
许多程序一次将输入读为单行。

GNU历史库能够跟踪这些行,将任意数据与每行相关联,并在构思新的行中使用前一行的信息。
Bash和其他shell可以使用此历史库。
默认文件是~/.history或者~/.bash_history。

Bash的历史功能

Bash的历史函数取决于称为histfile的变量,通常设置为当前用户的.bash_history文件(位于用户的主目录中)。
回声后,它会返回用户历史文件的完整路径和名称,如下所示:

$echo $HISTFILE
/home/franhesco/.bash_history

1)删除Linux历史命令

历史可以用一些命令重置,但操作后,如果在shell中注销并登录,则会看到相同的历史记录

一种。删除以前的命令

我们可以使用history -c命令清除当前shell中的上一个历史命令。
如果我们刚刚键入了密码,则这就足够了(但矫枉过正了),并且没有退出该shell或者明确保存其历史记录。

以下示例显示了我们当前的历史

$history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 history

现在让我们使用命令。

$history -c
$exit

退出BASH时,历史记录将保存到历史记录文件中。
在当前会话期间创建的历史记录添加到文件中,已存在的条目未受影响。
让我们来看看新的历史

$history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 exit
 5 history

让我们向我们的历史添加一些命令。

$history 
 1 history 
 2 vim .bash_history 
 3 exit
 4 exit
 5 history 
 6 ls -l
 7 mkdir aa
 8 ls -li
 9 history

使用当前shell的历史记录覆盖历史记录文件,在history -c命令之后运行history -w

$history -c
$history -w
$exit

再次登录后,让我们查看我们的历史

$history 
 1 history -w
 2 exit
 3 history

我们可以看到我们的历史记录在history -w条目命令开始。

湾删除单个命令

我们可以删除我们不想要的历史记录条目,该名表使用-d选项。
这将删除位置偏移量的历史记录条目。
但是当我们关闭终端并再次打开它时,历史记录不会删除。
所以我们终于使用`历史记录-w an保存了更改。

# history -d offset

举些例子:

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget username:[email protected]/secret/file.tar.gz
 6 mkdir

现在,如果要删除MKDIR命令的第六个条目只需使用:

# history -d 5
# history -w

看结果

# history
 1 cd
 2 history
 3 ls -alhF
 4 history
 5 wget username:[email protected]/secret/file.tar.gz
 6 history
 7 history -d 6

我们可以看到我们没有上面没有mkdir命令条目。

2)完全清除BASH

要在服务器上完全清除BASH历史记录,替代解决方案是将"~/.bash_history""链接到"/DEV/NULL"链接

$ln -sf /dev/null ~/.bash_history

但是,一个令人讨厌的副作用是历史条目与内存链接,当我们注销时,它将互化回文件。
要解决此问题,我们可以使用以下命令:

ln -sf /dev/null ~/.bash_history && history -c && exit

3)关闭Bash历史

我们可以使用两种方式之一停止日志记录历史记录:将其关闭为所有用户,或者关闭单个用户的日志记录。

关闭所有用户

我们可以在"/etc/profile"文件中添加unset HISTFILE行,关闭所有用户的BASH历史记录。
此行取消了系统上每个用户的历史文件

# echo "unset HISTFILE" >> /etc/profile

我们需要有权应用上面的命令

关闭特定用户

上面的命令,可以关闭特定用户的BASH历史记录。
我们只需要指示他的bash_profile文件。

# echo "unset HISTFILE" >> /home/papso/.bash_profile

每个用户都将登录,他的历史将重置如下。
他的所有历史记录命令都将保存到用户注销

$history 
 1 history

编辑.bashrc.

我们可以通过编辑历史命令参数的两个值来删除历史命令。

  • HistSize是在历史列表中存储在历史列表中的线条或者命令的数量
  • HISTFILESIZE在写入历史记录文件时保存为历史堆栈的线路数量。

要做,请编辑.bashrc并添加

HISTFILESIZE=0
HISTSIZE=0

现在,我们可以使用任何上述命令成功删除BASH历史记录甚至停止登录BASH历史记录。

4)删除一些记录

我们可以使用"历史记录-d偏移量",其中内置删除当前shell历史记录的特定行。
如果要删除一系列线路,它并不是很实际的,因为它只需要一个偏移作为参数,但我们可以将其包装为循环。

命令提示符中的此衬班是有帮助的。

for i in {1..N}; do history -d START_NUM; done

其中start_num是历史记录中条目的起始位置,n是我们可能想要删除的条目。

让我们查看历史命令的最后15个条目

# history 15
 986 stat synchro 
 987 ln synchro sync2
 988 stat synchro 
 989 debugfs /dev/vda
 990 exit
 991 echo $HISTFILE
 992 cat .bash_history 
 993 ls -l .bash_history 
 994 passwd patrick
 995 echo "unset HISTFILE" >> /home/papso/.bash_profile
 996 history 
 997 for i in {1..10}; do history -d 867; done
 998 history 
 999 history 30
 1000 history 15

现在让我们在986行开始删除13个条目

"在{1..13}中为我;历史--d 986;完成了

现在让我们检查结果

# history 15
 975 ls -il /home/bobbin/sync.sh filesync 
 976 ls -l /home/bobbin/sync.sh filesync 
 977 ln /home/bobbin/sync.sh synchro
 978 ls -il /home/bobbin/sync.sh synchro 
 979 cp script script2
 980 ls -il script script2
 981 stat script
 982 stat script2
 983 man find
 984 find/-inum 517333
 985 stat filesync 
 986 history 30
 987 history 15
 988 for i in {1..13}; do history -d 986; done
 989 history 15

我们可以看到我们没有相同的结果。

当用户使用登录名或者交互式/非登录shell登录时,打开用户的.bash_history文件。
现在可以使用一些有用的命令在此文件上运行。
该历史可用于检索之前使用的命令,但我们可能需要删除使用的一些使用的命令的某些条目。
"~/.bash_history"文件不会响应其他程序的提示记录我们输入的内容,只是我们在Bash提示符本身中键入的内容。