安全稽核、隱私保護或測試環境重置時,需清除登入成功/失敗紀錄與命令歷史。本文提供標準指令與腳本化範例。
> /var/log/wtmp
last # 驗證:應無輸出
> /var/log/btmp
lastb # 驗證:應無輸出
history -c
> ~/.bash_history
# 1. 備份想保留的指令
history > /root/keep_history.txt
# 2. 清空記憶體內歷史
history -c
# 3. 讀回保留指令
history -r /root/keep_history.txt
# 4. 驗證
history
#!/bin/bash
# 需 root 執行
> /var/log/wtmp
> /var/log/btmp
> /var/log/secure # 視需求保留
for u in /home/* /root; do
[ -f "$u/.bash_history" ] && > "$u/.bash_history"
done
echo "Logs cleared at $(date)" >> /var/log/admin_clean.log
加入 crontab 定期執行(謹慎使用)。
/var/log/wtmp、/var/log/btmp 為二進位檔,> 截斷即可,不需 rm。journalctl --vacuum-time=7d 清理。