命令行历史
history
登录shell时,会读取命令历史文件中记录下的命令:~/.bash_history
登录进shell后新执行的命令只会记录在缓存中;这些命令会用户退出时“追加”至命令历史文件中;
history:
-a: 追加本次会话新执行的命令历史列表至历史文件中;-d: 删除历史中指定的命令; -c: 清空命令历史;
快捷操作:
!#:调用历史中第#条命令;
!string:调用历史中最近一个以string开头的命令;
!!:上一条命令
让history记录显示时间
效果:
[root@localhost ~]# history 5365 [-01-27 20:50:11]vim .bash_profile 366 [-01-27 20:50:50]vim /etc/profile367 [-01-27 20:51:34]source /etc/profile368 [-01-27 20:51:41]history 369 [-01-27 20:57:37]history 5
配置:
在全局配置文件/etc/profile或者用户家目录.bash_profile的文件末尾添加一行配置,并使配置生效
# vim /etc/profile或.bash_profileexport HISTTIMEFORMAT="[%F %T]"####################################使配置立即生效[root@localhost ~]# source [/etc/profile|.bash_profile]
还可以显示执行的用户及其IP
# vim /etc/profile #在文件最后加上如下内容USER_IP=`who -u am i 2>/dev/null| awk '{print $NF}'|sed -e 's/[()]//g'` #获取用户登录IPif [ "$USER_IP" = "" ]thenUSER_IP=`hostname`fiexport HISTTIMEFORMAT="[%F %T][`whoami`][${USER_IP}] " #定义历史命令显示格式####################################[root@localhost ~]# source /etc/profile查看历史命令,已生效[root@localhost ~]# history 5433 [-01-27 21:39:40][root][192.168.0.106]source /etc/profile434 [-01-27 21:39:42][root][192.168.0.106]history 5435 [-01-27 21:39:49][root][192.168.0.106]reboor436 [-01-27 21:39:51][root][192.168.0.106]reboot437 [-01-27 21:43:13][root][192.168.0.106]history 5