一、需求背景

默认情况下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

查看

关注公众号「原宏Cloud运维栈」,带你学习更多实战经验!