history实时写入带时间戳命令

来源:互联网 发布:多米音乐java 编辑:程序博客网 时间:2024/06/04 18:03
    在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 "            


原创粉丝点击