虚拟机清理痕迹与压缩镜像

发布于: 2026-08-10 12:49

1. 清理 Shell 历史

history -c
history -w
unset HISTFILE
rm -f /root/.bash_history
ln -sf /dev/null /root/.bash_history

2. 清理 SSH 痕迹

rm -f /root/.ssh/known_hosts
rm -f /home/*/.ssh/known_hosts

3. 清理系统与日志痕迹

# 临时文件
rm -rf /tmp/*

# 编辑器记录
rm -f /root/.lesshst /root/.viminfo

# apt 缓存
apt clean

# systemd 日志
journalctl --rotate
journalctl --vacuum-size=10M

# 应用日志
find /var/log -type f -name "*.log" -exec truncate -s 0 {} \;

# 登录记录
truncate -s 0 /var/log/wtmp /var/log/btmp /var/log/lastlog

4. 零填充空闲空间

# 用零填充空闲空间,便于压缩
dd if=/dev/zero of=/zero bs=1M || true; rm -f /zero