正常情况下,Linux系统中输入 history 只显示序号和历史命令如下图,但是当我们想要根据历史命令来排查一些故障问题时,无法精确获取该命令执行的详细信息,包括执行时间、执行的用户、是哪个终端上执行的,想要获取详细的历史记录信息,需要手动配置一下,下面我将逐个介绍:
[root@192 ~]# vim /etc/bashrc export HISTTIMEFORMAT="%Y-%m-%d:" [root@192 ~]# source /etc/bashrc [root@192 ~]# history 1 2022-02-27:cd / 2 2022-02-27:vim /influxdb 3 2022-02-27:mkdir /oinfluxdb 4 2022-02-27:ls 5 2022-02-27:cd /oinfluxdb 6 2022-02-27:ls 7 2022-02-27:ll 8 2022-02-27:vim influxdb.sh 9 2022-02-27:/bin/bash influxdb.sh 10 2022-02-27:systemctl status influxdb
或者:
[root@192 ~]# vim /etc/bashrc export HISTTIMEFORMAT="%F %T:" [root@192 ~]# source /etc/bashrc [root@192 ~]# history 1 2022-02-27 13:26:38:cd / 2 2022-02-27 13:26:38:vim /influxdb 3 2022-02-27 13:26:38:mkdir /oinfluxdb 4 2022-02-27 13:26:38:ls 5 2022-02-27 13:26:38:cd /oinfluxdb 6 2022-02-27 13:26:38:ls 7 2022-02-27 13:26:38:ll 8 2022-02-27 13:26:38:vim influxdb.sh 9 2022-02-27 13:26:38:/bin/bash influxdb.sh 10 2022-02-27 13:26:38:systemctl status influxdb
或者:
[root@192 ~]# vim /etc/bashrc export HISTTIMEFORMAT="%Y-%m-%d %H:%M:%S `whoami` " [root@192 ~]# source /etc/bashrc [root@192 ~]# history 1 2022-02-27 13:26:38 root cd / 2 2022-02-27 13:26:38 root vim /influxdb 3 2022-02-27 13:26:38 root mkdir /oinfluxdb 4 2022-02-27 13:26:38 root ls 5 2022-02-27 13:26:38 root cd /oinfluxdb 6 2022-02-27 13:26:38 root ls 7 2022-02-27 13:26:38 root ll 8 2022-02-27 13:26:38 root vim influxdb.sh 9 2022-02-27 13:26:38 root /bin/bash influxdb.sh 10 2022-02-27 13:26:38 root systemctl status influxdb
[root@192 ~]# vim /etc/bashrc export HISTSIZE=1000 [root@192 ~]# source /etc/profile
[root@192 ~]#vim /etc/bashrc export HISTFILE=/var/log/.cmd_log [root@192 ~]# source /etc/bashrc
[root@192 ~]# vim /etc/bashrc export HISTCONTROL=ignoredups [root@192 ~]# source /etc/bashrc
[root@192 ~]# shopt -s histappend
!number
执行第几条命令!command
从最近的命令查到以command
开头的命令执行!!
执行上一条