history实时写入带时间戳命令

    在bash中,所执行的命令可以用history显示出来,但是bash的机制是:先将命令写入baffer,退出shell时才会写入.bash_history文件中,所以最后.bash_history文件中的命令无法分辨出来时间,解决这个问题可以采用时间戳的方法。
    bash中提供了时间戳的参数HISTTIMEFORMAT

HISTTIMEFORMAT
              If this variable is set and not null, its value is used as a format string for strftime(3) to print the time stamp associated with each history entry displayed by the history builtin. If this variable is set, time stamps are written to the history file so they may be pre-served across shell sessions.


    有了这个参数,可以让执行的命令用history显示时,前面加上时间戳。举例如下:

 1011  root 192.168.1.119 2011-03-12 14:49:43 cd /
 1012  root 192.168.1.119 2011-03-12 14:49:45 ls
 1013  root 192.168.1.119 2011-03-12 14:49:53 vi /etc/profile
 1014  root 192.168.1.119 2011-03-12 14:49:59 vi /etc/hosts
 1015  root 192.168.1.119 2011-03-12 14:50:12 vi /etc/resolv.conf
 1016  root 192.168.1.119 2011-03-12 14:50:15 history
 1017  root 192.168.1.119 2011-03-12 14:51:23 cat /etc/profile
 1018  root 192.168.1.119 2011-03-12 14:51:30 cat /etc/hosts
 1019  root 192.168.1.119 2011-03-12 14:51:35 cat /etc/hosts.allow
 1020  root 192.168.1.119 2011-03-12 14:51:39 cat /etc/hosts.deny
 1021  root 192.168.1.119 2011-03-12 14:51:48 cat /etc/resolv.conf
 1022  root 192.168.1.119 2011-03-12 14:51:51 history

    另外就是需要让bash实时写入历史命令。在 /etc/profile   里添加下面的参数:

#用以实时写入history

export PROMPT_COMMAND="history -a"

#history带登录用户名、客户端IP、时间戳

export HISTTIMEFORMAT="$LOGNAME `echo $SSH_CLIENT | awk {'print $1'}` %F %T "            


  • 2
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

laolinux

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值