Windows XP Windows 7 Windows 2003 Windows Vista Windows 綜合教學 Linux 系統教學
Windows 10 Windows 8 Windows 2008 Windows NT Windows Server 電腦軟體教學
您现在的位置: Windows教程網 >> Windows教程 >  >> Linux 系統教學 >> Linux 教學

Linux 磁碟空間滿載排查與清理實戰指南

編輯:Linux 教學

磁碟空間耗盡會導致服務崩潰、寫入失敗、日誌停寫。本文提供系統化排查流程:從文件系統級掃描到進程級定位,並給出清理與擴容策略。

一、建立基線與趨勢分析

# 定期掃描重要文件系統,生成文件清單對比
find /home -type f -printf "%p %s %TY-%Tm-%Td %TH:%TM\n" > files_$(date +%F).txt
diff files_old.txt files_new.txt

識別高增長目錄/文件,對低頻訪問大文件考慮壓縮(gzip/xz)或歸檔。

二、 inode 耗盡 vs 空間耗盡

df -i /home    # 查看 inode 使用率
df -h /home    # 查看空間使用率
  • inode 充足但空間滿 → 大文件佔用(日誌、備份、媒體)
  • inode 耗盡 → 海量小文件(緩存、session、郵件隊列、PHP 臨時文件)

三、定位大目錄與大文件

# 目錄級:顯示 /home 下佔用超過 100MB 的子目錄
du -h --max-depth=1 /home | awk '$1 ~ /[0-9]+M/ && $1+0 > 100'

# 文件級:查找超過 200MB 的文件
find /home -type f -size +200M -exec ls -lh {} \;

# 最近 24 小時修改/創建的文件
find /home -type f -mtime -1 -ls

四、常見垃圾清理目標

  • /var/log/:舊日誌(保留策略配置 logrotate)
  • /tmp/、/var/tmp/:臨時文件
  • /root/.cache/、/home/*/.cache/:用戶緩存
  • Docker:docker system prune -a、docker image prune
  • 包管理器緩存:yum clean all / apt-get clean / dnf clean all
  • 核心舊版本:package-cleanup --oldkernels --count=2 (CentOS)

五、緊急騰空空間技巧

  1. 刪除大日誌文件後,必須重啟對應服務或發送信號(如 systemctl reload rsyslog),否則文件句柄未釋放,空間不回收
  2. 用 lsof | grep deleted 找出已刪除但未釋放的文件,重啟持有進程
  3. 臨時掛載備用分區:mount /dev/sdb1 /mnt; mv /var/log/* /mnt/; mount --bind /mnt /var/log
  4. 軟鏈接遷移大目錄到空閒分區:mv /home/bigdir /data/; ln -s /data/bigdir /home/bigdir

六、進程級定位寫入者

# 按 I/O 寫入量排序
iotop -o -b -n 1 | head -20

# 查看進程打開的大文件
lsof -p  | awk '$7 > 100000000'

# 系統級 I/O 統計
pidstat -d 1 5

七、長期治理

  • 配置 logrotate:按大小/時間輪轉、壓縮、保留份數
  • 設置磁碟配額:quota 限制用戶/項目空間
  • 監控告警:df -h 定時檢查,閾值 80% 告警
  • 規劃 LVM/擴容策略:預留 20% 空間緩衝
Copyright © Windows教程網 All Rights Reserved