直接執行 history -c 會清空整個 .bash_history,管理員一眼就能發現異常。滲透測試或維護作業中,需精準控制留存內容。
# 登出前執行
export HISTFILE=/dev/null
export HISTSIZE=0
原理:將歷史檔導向空裝置,設定記憶體緩衝區大小為 0,當前 Session 所有指令不會寫入檔案。
# 登入時執行(可加入 .bashrc 或 Profile)
unset HISTORY HISTFILE HISTSAVE HISTZONE HISTLOG
export HISTFILE=/dev/null
export HISTSIZE=0
export HISTFILESIZE=0
# 直接編輯
vi ~/.bash_history
# 刪除特定行或替換敏感內容
# :%s/敏感IP/127.0.0.1/g
注意: 此檔為最終持久化存儲,直接刪除檔案會引起懷疑,建議替換而非清空。
# 將特定 IP 替換為本地回環
sed -i 's/192.168.100.252/127.0.0.1/g' ~/.bash_history
| 檢查項目 | 指令 | 異常徵兆 |
|---|---|---|
| 歷史檔大小突變 | ls -lh ~/.bash_history | 檔案大小驟減或為 0 |
| 時間戳不連續 | history | tail -20 | 指令編號跳躍、時間倒退 |
| 多 Session 衝突 | lsof ~/.bash_history | 多進程同時寫入 |
| 系統級審計 | ausearch -k bash_history | 需啟用 auditd |
auditd 監控 ~/.bash_history 寫入、設定 HISTCONTROL=ignorespace:erasedupsHISTIGNORE 忽略敏感指令(如 export HISTIGNORE="*passwd*:*secret*")chattr +a ~/.bash_history 僅允許附加、禁止覆寫/刪除