一、需求背景
默认情况下history输入命令如下:
992 ls
993 cd ..
994 cd conf/
995 ls
996 vim application-core.yml
997 vim application.yml
998 su root
999 history
不能看出什么时间执行、也不能知道执行的IP地址是多少。
二、加入时间和执行IP地址【全局生效、针对所有用户】
vim /etc/profile
source /etc/profile
在/etc/profile 末尾加入以下内容:
if [ `who am i| awk '{print $NF}'|awk -F'(' '{print NF}'` -eq 2 ];then
unset HISTTIMEFORMAT
export HISTTIMEFORMAT="[%F %T]-[`whoami`]- at [`who am i| awk '{print $NF}'`] "
else
unset HISTTIMEFORMAT
export HISTTIMEFORMAT="[%F %T]-[`whoami`]- at [`hostname`] "
fi
三、history其他优化
不同的终端可以通过上翻可查询到命令
vim /etc/bashrc
在末尾添加:
# Avoid duplicates
export HISTCONTROL=ignoredups:erasedups
# When the shell exits, append to the history file instead of overwriting it
shopt -s histappend
# After each command, append to the history file and reread it
export PROMPT_COMMAND="$PROMPT_COMMAND:+$PROMPT_COMMAND$\nhistory -a; history -c; history -r"
使其生效:
source /etc/bashrc
调整大小,增加到10000条
vim /etc/bashrc
末尾添加
export HISTSIZE=10000
刷新一下
source /etc/bashrc
[alios@XAYL home2]$ history
1 2024-02-01 08:40:10 127.0.0.1:alios history
2 2024-02-01 08:40:13 127.0.0.1:alios ls
3 2024-02-01 08:40:17 127.0.0.1:alios cd /home2/
4 2024-02-01 08:40:18 127.0.0.1:alios ls
5 2024-02-01 08:40:23 127.0.0.1:alios history