Why Manage Disk Space?¶
Over time, server disks can fill up unnoticed, preventing new software installations, causing service errors, or even system crashes. For example, accumulating log files, uncleaned temporary files, and unremoved large files can all lead to disk shortages. Proper disk space management ensures server stability and avoids unnecessary downtime.
Basic Concepts: Understanding “Warehouse” and “ID”¶
To manage disks, it’s essential to grasp two core concepts: inode and block.
-
inode (ID Card): Each file/directory has an inode, which acts like an ID card storing metadata (size, modification time, permissions, etc.) but not the actual file content. A disk can run out of inodes even if there’s remaining space, causing failures when creating new files.
-
block: The smallest unit for data storage (e.g., 4KB per block). File content is split into blocks and stored on the disk, with the inode recording their locations.
How to Quickly Check Disk Space?¶
1. Overall Disk Usage (df command)¶
df -h
- -h parameter: Displays sizes in human-readable units (GB/MB) for readability.
- Output Explanation:
Filesystem: Disk device name (e.g., /dev/sda1)Size: Total disk capacityUsed: Used spaceAvail: Available spaceUse%: Usage percentage (alert if >85%)Mounted on: Mount point (e.g., /, /home)
2. Directory/File Size (du command)¶
du -sh /path/to/dir # Total size of a single directory (-s=summary, -h=human-readable)
du -ah /var | sort -hr | head -10 # Top 10 largest files/dirs in /var
du -sh /var/log: Quickly check log directory size (logs are common “space hogs”).
3. Inode Usage Check (df -i)¶
df -i
- If
IUse%is near 100%, new files cannot be created even if disk space is available (inodes are exhausted).
Common Issues: What to Do When Disk Space Fills Up?¶
1. Excessively Large Log Files (Most Common)¶
- Cause: Uncleaned service logs (e.g., Nginx, MySQL) grow to tens of GB.
- Solutions:
- Use
logrotate(system-built-in) to auto-manage logs by size/time. - Manual cleanup: Backup critical logs (e.g.,
cp /var/log/messages messages.bak), then clear with> /var/log/messages(logs recreate automatically).
2. Temporary File Accumulation (/tmp, /var/tmp)¶
- Cause: Uncleaned temporary files from programs (e.g., cache, download residuals).
- Solutions:
- Use
tmpwatchfor scheduled cleanup:tmpwatch 72 /tmp(deletes files unmodified for 72 hours). - Manual cleanup before reboot:
rm -rf /tmp/*(use carefully to avoid deleting in-use temp files).
3. Unremoved Large Files (Backups, Old Data)¶
- Cause: Unarchived backups, old code versions, or large logs.
- Solutions:
- Find large files:
find / -type f -size +100M -print0 | xargs -0 du -h. - Delete safely:
find / -name "*.old" -delete(target specific file types).
4. Poor Partition Design¶
- Cause: All data stored in the root partition (/), causing one partition fullness to affect the entire system.
- Solutions:
- Dynamic expansion: If using LVM, use
lvextend -L +10G /dev/mapper/root. - Separate partitions: Dedicated /var (logs), /home (user data) to isolate failures.
Long-Term Optimization: Keeping Disk Space “Healthy”¶
1. Regular Cleaning + Backup¶
- Weekly
cronjobs for temp file cleanup:0 3 * * 0 /usr/sbin/tmpwatch 7 /tmp. - Delete old backups:
find /backup -name "*.tar.gz" -mtime +30 -delete.
2. Offload Storage to External Systems¶
- Mount NAS/SAN: Replicate /home or /var/log to external storage.
- Example:
mount /dev/sdb1 /mnt/external_dataand update/etc/fstabfor permanent mounts.
3. Alerts + Monitoring¶
- Use Nagios/Zabbix to monitor disk usage, triggering alerts at 80%+ utilization.
- Simple check:
df -h | grep -vE 'tmpfs|udev' | awk '{print $5}' | sort -nr | head -1.
4. Pitfall Avoidance Tips¶
- Verify before deletion: Use
ls/fileto confirm file purpose; accidental deletion of config files causes service failures. - Don’t delete in-use logs: If a log is “tailed” (
tail -f), restart the service first to avoid write errors. - Inode-full strategy: For inode exhaustion, prioritize emptying small file directories (e.g., /tmp, /var/spool) over large files.
Conclusion¶
Disk space management is fundamental in Linux server maintenance. Key principles: regular checks, proactive cleanup, and long-term planning. Avoid waiting until disk full—use alerts, automate cleanup (logrotate, tmpwatch), partition strategically, and leverage external storage to ensure “always enough” disk space.