Linux 終端機的指令歷史(上下鍵調用)與圖形介面的「最近使用檔案」記錄,皆儲存在使用者家目錄下的隱藏檔案中。若帳號共用或租用主機,清除這些紀錄可保護隱私。本文說明紀錄位置與一鍵清除腳本。
| 檔案 | 記錄內容 | 所屬 |
|---|---|---|
~/.bash_history | 終端機/文字模式下執行過的所有指令 | Bash Shell |
~/.recently-used(或 ~/.local/share/recently-used.xbel) | GNOME/KDE 等桌面環境記錄的最近開啟檔案 | GTK/Qt 檔案選擇器 |
各用戶獨立,互不干擾。Root 帳號同理(/root/.bash_history 等)。
> ~/.bash_history # 清空指令歷史(保留檔案)
> ~/.recently-used # 清空最近使用檔案記錄
# 或
rm -f ~/.bash_history ~/.recently-used # 直接刪除檔案,下次登入自動重建
> 為 Shell 重導向截斷檔案,等同 : > file 或 truncate -s 0 file。
編輯 /etc/rc.local(或 systemd 服務),加入:
> /root/.bash_history
> /root/.recently-used
# 若需清除所有用戶,可寫腳本遍歷 /home/*
系統啟動時自動執行,確保無殘留紀錄。注意:rc.local 需有執行權限且啟用 rc-local.service。
history -d 行號(需先 history -r 讀入)。set +o history 或 unset HISTFILE(當前 Session 生效)。Settings → Privacy → File History & Trash 可關閉或調整保留天數。/var/log/secure、audit.log)等伺服器端證據。auditd、syslog 集中日誌審計,而非依賴客戶端清除。sudo 授權。清除 ~/.bash_history 與 ~/.recently-used 只需兩行指令,可手動或開機自動執行。但真正的安全在於帳號隔離、審計日誌與最小權限原則,清除紀錄僅是輔助手段。