在 Linux 中,“历史记录”通常指 shell 命令历史,但也可能涉及 日志、缓存、操作痕迹 等。下面按常见场景给你一个完整、实用、可安全操作的清理指南。
history
history -c
只清理内存中的历史,不会立即删除
~/.bash_history
rm ~/.bash_history
或覆盖清空:
> ~/.bash_history
history -w
✅ 推荐组合(彻底清理)
history -c
> ~/.bash_history
history -w
rm ~/.zsh_history
或
> ~/.zsh_history
unset HISTFILE
在命令前加空格(部分 shell 需配置):
echo "这条命令不记录"
编辑 ~/.bashrc:
unset HISTFILE
或
HISTSIZE=0
⚠️ 注意:日志是系统重要组件,不要随意删除整个目录
> /var/log/wtmp
> /var/log/btmp
> /var/log/lastlog
journalctl --vacuum-time=1d
或手动清理:
> /var/log/syslog
> /var/log/messages
❗ 不建议普通用户直接操作,可能导致系统异常
rm ~/.ssh/known_hosts
查看 SSH 登录记录:
last
rm -rf /tmp/*
rm -rf ~/.cache/*
who
w
last
✅ 快速清理 Bash 历史
history -c
> ~/.bash_history
history -w
✅ 禁用历史
unset HISTFILE
✅ 安全建议
HISTCONTROL=ignorespace如果你愿意,可以告诉我:
我可以给你更精准、安全的清理方案 ✅