Bash Shell 中 Linux“历史命令”的强大功能
我们在日常工作中经常使用 history 命令来检查命令的历史记录或获取有关用户执行的命令的信息。在这篇文章中,我们将了解如何有效地使用历史命令来提取用户在 Bash shell 中执行的命令。这对于审计目的或找出在日期和时间执行了哪些命令可能很有用。
默认情况下,执行历史记录命令时不会看到日期和时间戳。然而,bash shell 提供了CLI工具来编辑用户的命令历史记录。让我们看看一些方便的提示和技巧以及 history 命令的力量。
1. 列出 Linux 中最后/所有执行的命令
从终端执行简单的 history 命令将向您显示上次执行的命令的完整列表以及行号。
[narad@tecmint ~]$ history
1 PS1='\e[1;35m[\u@\h \w]$ \e[m '
2 PS1="\e[0;32m[\u@\h \W]$ \e[m "
3 PS1="\u@\h:\w [\j]$ "
4 ping google.com
5 echo $PS1
6 tail -f /var/log/messages
7 tail -f /var/log/messages
8 exit
9 clear
10 history
11 clear
12 history
2. 列出所有带有日期和时间戳的命令
如何根据命令查找日期和时间戳?使用带有变量的‘export’命令将显示历史命令以及命令执行时相应的时间戳。
[narad@tecmint ~]$ export HISTTIMEFORMAT='%F %T '
1 2013-06-09 10:40:12 cat /etc/issue
2 2013-06-09 10:40:12 clear
3 2013-06-09 10:40:12 find /etc -name *.conf
4 2013-06-09 10:40:12 clear
5 2013-06-09 10:40:12 history
6 2013-06-09 10:40:12 PS1='\e[1;35m[\u@\h \w]$ \e[m '
7 2013-06-09 10:40:12 PS1="\e[0;32m[\u@\h \W]$ \e[m "
8 2013-06-09 10:40:12 PS1="\u@\h:\w [\j]$ "
9 2013-06-09 10:40:12 ping google.com
10 2013-06-09 10:40:12 echo $PS1
HISTTIMEFORMAT 变量的含义
%F Equivalent to %Y - %m - %d
%T Replaced by the time ( %H : %M : %S )
3. 过滤历史命令
正如我们所看到的,在上面的输出中相同的命令被重复了很多次。如何过滤历史记录中的简单或非破坏性命令?通过在 HISTIGNORE='ls -l:pwd:date:' 中指定命令来使用以下“导出”命令不会被系统保存,也不会显示在历史命令中。
[narad@tecmint ~]$ export HISTIGNORE='ls -l:pwd:date:'
4.忽略历史中的重复命令
使用以下命令将帮助我们忽略用户输入的重复命令。如果用户在 Bash 提示符中多次执行同一命令,则历史记录中只会显示单个条目。
[narad@tecmint ~]$ export HISTCONTROL=ignoredups
5.取消设置导出命令
即时取消设置导出命令。对于export命令导出的任何命令,都一一执行带变量的unset export命令。
[narad@tecmint ~]$ unset export HISTCONTROL
6. 永久保存导出命令
在.bash_profile中进行如下条目以永久保存export命令。
[narad@tecmint ~]$ vi .bash_profile
.bash_profile
Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
User specific environment and startup programs
export HISTCONTROL=ignoredups
PATH=$PATH:$HOME/bin
export PATH
7. 列出特定用户执行的命令
如何查看特定用户执行的命令历史记录。 Bash 将历史记录保存在 ‘~/.bash_history’ 文件中。我们可以查看或打开文件来查看命令历史记录。
[narad@tecmint ~]$ vi .bash_history
cd /tmp/
cd logstalgia-1.0.3/
./configure
sudo passwd root
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc
./configure
make
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc++
apt-get install libsdl1.2-dev libsdl-image1.2-dev libpcre3-dev libftgl-dev libpng12-dev libjpeg62-dev make gcc
apt-get install make
mysql -u root -p
apt-get install grsync
apt-get install unison
unison
8. 禁用存储命令历史记录
由于组织的安全策略,某些组织不保留命令历史记录。在这种情况下,我们可以编辑用户的 .bash_profile 文件(它是隐藏文件)并创建如下条目。
[narad@tecmint ~]$ vi .bash_profile
.bash_profile
Get the aliases and functions
if [ -f ~/.bashrc ]; then
. ~/.bashrc
fi
User specific environment and startup programs
PATH=$PATH:$HOME/bin
HISTSIZE=0
export PATH
.bash_profile (END)
使用以下命令保存文件并加载更改。
[narad@tecmint ~]$ source .bash_profile
注意:如果您不希望系统记住您输入的命令,只需执行以下命令即可禁用或停止即时记录历史记录。
[narad@tecmint ~]$ export HISTSIZE=0
提示:搜索“HISTSIZE”并使用超级用户在“/etc/profile”文件中进行编辑。文件的更改将在全球范围内生效。
9. 删除或清除命令历史记录
通过向上和向下箭头,我们可以看到以前使用过的命令,这些命令可能对您有帮助,也可能会激怒您。使用“-c”选项删除或清除 bash 历史列表中的所有条目。
[narad@tecmint ~]$ history -c
10.使用Grep命令搜索历史命令
通过将历史文件传输到“grep”中,通过“.bash_history”搜索命令,如下所示。例如,以下命令将从历史列表中搜索并找到“pwd”命令。
[narad@tecmint ~]$ history | grep pwd
113 2013-06-09 10:40:12 pwd
141 2013-06-09 10:40:12 pwd
198 2013-06-09 15:46:23 history | grep pwd
202 2013-06-09 15:47:39 history | grep pwd
11. 搜索最后执行的命令
使用‘Ctrl+r’命令搜索先前执行的命令。找到所需的命令后,按“Enter”执行相同的操作,否则按“esc”取消。
(reverse-i-search)`source ': source .bash_profile
12. 调用最后执行的命令
回忆一下以前使用过的特定命令。 Bang 和 8 (!8) 命令的组合将调用您执行过的 8 命令。
[narad@tecmint ~]$ !8
13. 调用最后执行的特定命令
回想一下以前使用的命令(netstat -np | grep 22),其中带有“!”,后跟该特定命令的一些字母。
[narad@tecmint ~]$ !net
netstat -np | grep 22
(No info could be read for "-p": geteuid()=501 but you should be root.)
tcp 0 68 192.168.50.2:22 192.168.50.1:1857 ESTABLISHED -
tcp 0 0 192.168.50.2:22 192.168.50.1:2516 ESTABLISHED -
unix 2 [ ] DGRAM 12284 - @/org/freedesktop/hal/udev_event
unix 3 [ ] STREAM CONNECTED 14522 -
unix 2 [ ] DGRAM 13622 -
unix 3 [ ] STREAM CONNECTED 12250 - @/var/run/hald/dbus-ujAjOMNa0g
unix 3 [ ] STREAM CONNECTED 12249 -
unix 3 [ ] STREAM CONNECTED 12228 - /var/run/dbus/system_bus_socket
unix 3 [ ] STREAM CONNECTED 12227 -
我们试图强调历史指挥的力量。然而,这还没有结束。请通过下面的评论框与我们分享您的历史命令经验。