Linux基礎知識之history的詳細說明


背景:history是Linux中常會用到內容,在工作中一些用戶會突然發現其安裝不了某個軟件,於是尋求運維人員的幫助,而不給你說明他到底做了哪些坑爹的操作。此時你第一件要做的就是要查看其history命令歷史。查看后興許你就會發現他到底干了什么坑爹的操作。
history可以讓你在一定情況下快速定位問題所在。

本文的history介紹及其實踐都是來自CentOS7.2環境
[root@localhost ~]# cat /etc/RedHat-release  
CentOS Linux release 7.2.1511 (Core)

history的介紹
history是shell的內置命令,其內容在系統默認的shell的man手冊中。
history是顯示在終端輸入並執行的過命令,系統默認保留1000條。
[root@localhost ~]# history 
    1  ls
    2  vi /etc/sysconfig/network-scripts/ifcfg-eno16777728 
    3  service network restart 
    4  ifconfig
    5  ping www.linuxidc.com 
    6  systemctl disable firewalld.service  
    7  systemctl stop firewalld.service  
    8  exit
    9  ls
  10  type which
  11  which ls
  12  file /usr/bin/ls
  13  which clock 
  14  file /usr/sbin/clock
  15  man which
  16  man what 
  17  man who
  18  who
  19  man who
  20  man w 
  21  man who
  22  who -q 
  23  man w 
  ... 
  .. 
  .

系統在關閉后會將現有history內容保存在文件~/.bash_history

  與history相關的環境變量
HISTFILE          指定存放歷史文件位置,默認位置在~/.bash_profile(針對用戶)、 
      /etc/profile(針對全局,如果~/.bash_profile內沒有相關環境變量內容則使用全局變量設置) 
HISTFILESIZE      命令歷史文件記錄歷史的條數 
HISTSIZE          命令歷史記錄的條數,默認為1000 
HISTTIMEFORMAT="%F %T"  顯示命令發生的時間 
HISTIGNORE="str1:str2:..." 忽略string1,string2歷史 
HISTCONTROL      包含一下4項,讓哪一項生效只需要讓其=下面一項即可 
ignoredups:  忽略重復的命令;連續且相同方為“重復” 
ignorespace:  忽略所有以空白開頭的命令 
ignoreboth:ignoredups,ignorespace 
erasedups:    刪除重復命令

讓上述環境變量生效方式:
1、直接在當前shell內輸入相關變量,比如我們不想留存命令歷史,我們把HISTSIZE設置為0
[root@localhost ~]# HISTSIZE=0 
[root@localhost ~]# history

經測試,成功。不過這種設置的局限性是其作用范圍僅僅針對當前shell及其子shell,如果切換用戶或登出再登入其設置失效。不過其特點是設置完立刻生效。
下面通過實驗說明這個問題
[root@localhost ~]# bash 
[root@localhost ~]# history 
[root@localhost ~]# history

以上結果說明在當前shell內設置history的環境變量后,其作用范圍為當前shell及子shell
Last login: Fri Jul 29 17:26:41 2016 from 10.1.250.62 
[root@localhost ~]# history 
    1  history

重新登陸后原有的history環境變量失效
2、另一種讓history環境變量生效的方式是修改~/.bash_profile文件
[root@localhost ~]# vi ~/.bash_profile  
# .bash_profile 
  
# Get the aliases and functions 
if [ -f ~/.bashrc ]; then
        . ~/.bashrc 
fi
  
# User specific environment and startup programs 
  
PATH=$PATH:$HOME/bin
HISTTIMEFORMAT="%F %T "        此為新添加的history環境變量,我添加了發生命令操作的時間 
export PATH

wq保存退出。
[root@localhost ~]# history            
    1  history
    2  ll 
    3  cd
    4  hostory 
    5  history
    6  vi ~/.bash_profile  
    7  history

由結果可知變量並沒有生效,我們重新登錄再嘗試下。
[root@localhost ~]# history 
    1  2016-07-29 20:00:29 history
    2  2016-07-29 20:00:29 ll 
    3  2016-07-29 20:00:29 cd
    4  2016-07-29 20:00:29 hostory 
    5  2016-07-29 20:00:29 history
    6  2016-07-29 20:00:29 vi ~/.bash_profile  
    7  2016-07-29 20:00:29 history
    8  2016-07-29 20:00:29 logout
    9  2016-07-29 20:00:33 history

通過上面的兩個結果,我們可以發現第二種修改配置文件雖然也可以成功修改history環境變量但是其生效需要重新登陸,並不是改完就生效。但是它的特點是此修改始終有效,時效性為永久。

  history命令的使用
-c: 清空命令歷史
-d n: 刪除歷史中指定的命令,n表示命令號
#: 顯示最近的#條歷史
-a: 追加本次會話新執行的命令歷史列表至歷史文件,因為多終端所以如果想看當前都發生了什么操作就可以執行-a進行查看
-n: 讀歷史文件(本地數據)中未讀過的行到歷史列表(內存數據)
-r: 讀歷史文件(本地數據)附加到歷史列表(內存數據)
-w: 保存歷史列表(內存數據)到指定的歷史文件(本地數據)
-s: 展開歷史參數成一行,附加在歷史列表后。用於偽造命令歷史
下面來演示上面幾種命令的使用
[root@localhost ~]# history 
    1  2016-07-29 20:00:29 history
    2  2016-07-29 20:00:29 ll 
    3  2016-07-29 20:00:29 cd
    4  2016-07-29 20:00:29 hostory 
    5  2016-07-29 20:00:29 history
    6  2016-07-29 20:00:29 vi ~/.bash_profile  
    7  2016-07-29 20:00:29 history
    8  2016-07-29 20:00:29 logout
    9  2016-07-29 20:00:33 history
  10  2016-07-29 20:07:41  cd
  11  2016-07-29 20:07:44  ls
  12  2016-07-29 20:07:50  history
  13  2016-07-29 20:08:12 cat /etc/profile
  14  2016-07-29 20:12:10 HISTCONTROL=ignorespace 
  15  2016-07-29 20:27:28 history -p 
  16  2016-07-29 20:27:31 history
  17  2016-07-29 20:28:10 ls /etc
  18  2016-07-29 20:28:14 history
  19  2016-07-29 20:28:57 history

清空歷史
[root@localhost ~]# history -c 
[root@localhost ~]# history 
2016-07-29 20:29:26 history

從歷史文件讀取之前的命令歷史
[root@localhost ~]# history -r  
[root@localhost ~]# history 
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 ll 
    5  2016-07-29 20:30:59 cd
    6  2016-07-29 20:30:59 hostory 
    7  2016-07-29 20:30:59 history
    8  2016-07-29 20:30:59 vi ~/.bash_profile  
    9  2016-07-29 20:30:59 history
  10  2016-07-29 20:30:59 logout
  11  2016-07-29 20:31:01 history

刪除指定命令歷史
[root@localhost ~]# history -d 4 
[root@localhost ~]# history 
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 cd
    5  2016-07-29 20:30:59 hostory 
    6  2016-07-29 20:30:59 history
    7  2016-07-29 20:30:59 vi ~/.bash_profile  
    8  2016-07-29 20:30:59 history
    9  2016-07-29 20:30:59 logout
  10  2016-07-29 20:31:01 history
  11  2016-07-29 20:32:50 history -d 4 
  12  2016-07-29 20:32:52 history

偽造歷史命令
12345678910111213141516 [root@localhost ~]# history -s rm -rf /*  做下惡作劇 
[root@localhost ~]# history 
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 cd
    5  2016-07-29 20:30:59 hostory 
    6  2016-07-29 20:30:59 history
    7  2016-07-29 20:30:59 vi ~/.bash_profile  
    8  2016-07-29 20:30:59 history
    9  2016-07-29 20:30:59 logout
  10  2016-07-29 20:31:01 history
  11  2016-07-29 20:32:50 history -d 4 
  12  2016-07-29 20:32:52 history
  13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
  14  2016-07-29 20:34:00 history

我相信任誰輸入history看到這個命令都會嚇一身汗。


  調用歷史參數詳解

#cmd !^ : 利用上一個命令的第一個參數做cmd的參數 
#cmd !$ : 利用上一個命令的最后一個參數做cmd的參數 
#cmd !* : 利用上一個命令的全部參數做cmd的參數 
#cmd !:n : 利用上一個命令的第n個參數做cmd的參數 
#!n :調用第n條命令 
#!-n:調用倒數第n條命令 
#!!:執行上一條命令 
#!$:引用前一個命令的最后一個參數同組合鍵Esc,. 
#!n:^ 調用第n條命令的第一個參數 
#!n:$ 調用第n條命令的最后一個參數 
#!m:n 調用第m條命令的第n個參數 
#!n:* 調用第n條命令的所有參數 
#!string:執行命令歷史中最近一個以指定string開頭的命令 
#!string:^ 從命令歷史中搜索以string 開頭的命令,並獲取它的第一個參數 
#!string:$ 從命令歷史中搜索以string 開頭的命令,並獲取它的最后一個參數 
#!string:n 從命令歷史中搜索以string 開頭的命令,並獲取它的第n個參數 
#!string:* 從命令歷��中搜索以string 開頭的命令,並獲取它的所有參數

下面通過實驗來實踐上面的歷史參數的具體用法
[root@localhost ~]# history 
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 cd
    5  2016-07-29 20:30:59 hostory 
    6  2016-07-29 20:30:59 history
    7  2016-07-29 20:30:59 vi ~/.bash_profile  
    8  2016-07-29 20:30:59 history
    9  2016-07-29 20:30:59 logout
  10  2016-07-29 20:31:01 history
  11  2016-07-29 20:32:50 history -d 4 
  12  2016-07-29 20:32:52 history
  13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
  14  2016-07-29 20:34:00 history
  15  2016-07-29 20:40:32 ls
  16  2016-07-29 20:40:33 cd
  17  2016-07-29 20:40:45 ls /etc/passwd
  18  2016-07-29 20:41:35 history

我們先使用!!來調用上一條命令

[root@localhost ~]# !! 
history
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 cd
    5  2016-07-29 20:30:59 hostory 
    6  2016-07-29 20:30:59 history
    7  2016-07-29 20:30:59 vi ~/.bash_profile  
    8  2016-07-29 20:30:59 history
    9  2016-07-29 20:30:59 logout
  10  2016-07-29 20:31:01 history
  11  2016-07-29 20:32:50 history -d 4 
  12  2016-07-29 20:32:52 history
  13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
  14  2016-07-29 20:34:00 history
  15  2016-07-29 20:40:32 ls
  16  2016-07-29 20:40:33 cd
  17  2016-07-29 20:40:45 ls /etc/passwd
  18  2016-07-29 20:41:35 history

[root@localhost ~]# !h 
history
    1  2016-07-29 20:29:26 history
    2  2016-07-29 20:30:59 history -r  
    3  2016-07-29 20:30:59 history
    4  2016-07-29 20:30:59 cd
    5  2016-07-29 20:30:59 hostory 
    6  2016-07-29 20:30:59 history
    7  2016-07-29 20:30:59 vi ~/.bash_profile  
    8  2016-07-29 20:30:59 history
    9  2016-07-29 20:30:59 logout
  10  2016-07-29 20:31:01 history
  11  2016-07-29 20:32:50 history -d 4 
  12  2016-07-29 20:32:52 history
  13  2016-07-29 20:33:57 rm -rf /bin /boot /dev /etc /home /lib /lib64 /media /mnt /opt /proc /root /run /sbin /srv /sys /tmp /usr /var
  14  2016-07-29 20:34:00 history
  15  2016-07-29 20:40:32 ls
  16  2016-07-29 20:40:33 cd
  17  2016-07-29 20:40:45 ls /etc/passwd
  18  2016-07-29 20:41:35 history
  19  2016-07-29 20:47:03 history
  20  2016-07-29 20:48:22 history

大家感興趣可以一個個實驗。本文就介紹到這里。


  常用的快捷鍵
  重新調用前一個命令中最后一個參數:
  !$ 
  Esc, .(點擊Esc鍵后松開,然后點擊. 鍵)
這兩個很常用,特別是Esc,.
我們在創建文件后,通常會對其進行修改或其他的讀操作,這時候鍵入命令后利用上述快捷鍵即可快速補全所需命令。


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM