ARTICLE DETAIL

建站实战干货

来自一线的建站与推广经验沉淀,每一条都经过真实交付验证。

Where Are All My Inodes Being Used? How to Identify Directories Chewing Up Inodes Free Up Space

2026/9/6 12:56:52 拓冰建站 浏览量
Where Are All My Inodes Being Used? How to Identify Directories Chewing Up Inodes  Free Up Space If you’ve ever encountered a No space left on device error despite having plenty of free disk space, you’re likely facing aninode exhaustionissue. Inodes are a critical but often overlooked part of Linux/Unix filesystems, responsible for tracking file metadata (permissions, ownership, timestamps, etc.). Each file or directory on your system consumes one inode, and if you run out of inodes, you won’t be able to create new files—even if there’s gigabytes of storage left.In this guide, we’ll demystify inodes, show you how to check inode usage, identify which directories are hoarding inodes, and walk through actionable steps to free them up. By the end, you’ll be equipped to diagnose and resolve inode-related issues like a pro.Discover moreComputer Drives StorageLanguage ResourcesDictionaries EncyclopediasTable of Contents#What Are Inodes?Signs of Inode ExhaustionHow to Check Inode UsageIdentifying Directories with High Inode CountFreeing Up Inodes: Practical SolutionsPreventive Measures to Avoid Future Inode ShortagesConclusionReferencesWhat Are Inodes?#Aninode(short for index node) is a data structure on Unix/Linux filesystems that stores metadata about files and directories. Each file/directory is assigned a unique inode, which contains:File type (regular file, directory, symlink, etc.).Permissions (read/write/execute for user, group, others).Owner and group IDs.Timestamps (creation, modification, access).File size.Pointers to the actual data blocks on the disk (where the file content lives).Crucially,inodes are a finite resource. When you create a filesystem (e.g., during OS installation), the number of inodes is fixed (based on the filesystem size and configuration). Unlike disk space (which you can expand with tools like LVM), inode limits are set at creation time (though some filesystems likeext4allow dynamic inode allocation in rare cases).Signs of Inode Exhaustion#Inode exhaustion often masquerades as a disk full error, but the symptoms are distinct. Watch for these red flags:No space left on device errorswhen creating files, even thoughdf -hshows free disk space.Failed file/directory creations (e.g.,touch newfile.txtreturns an error).Applications crashing or failing to write logs/data.df -i(check inode usage) showsIUse%(inode usage percentage) at or near 100%.How to Check Inode Usage#To confirm inode exhaustion, use thedfcommand with the-iflag (short for inodes):df -iSample Output:#Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 524288 499999 24289 96% / tmpfs 250000 1 249999 1% /dev/shm /dev/sdb1 1048576 12345 1036231 2% /dataKey Columns:Inodes: Total inodes available on the filesystem.IUsed: Inodes currently in use.IFree: Free inodes remaining.IUse%: Percentage of inodes used (critical—if this is ≥90%, investigate!).For a human-readable summary (e.g., K for thousands of inodes), use-hwith-i:df -ihIdentifying Directories with High Inode Count#Once you confirm inode exhaustion (viadf -i), the next step is to findwhich directories are consuming the most inodes. This requires drilling down into the filesystem to locate directories with excessive numbers of files (since each file 1 inode).Method 1: Usedu(Disk Usage) with Inode Counting#Theducommand (disk usage) can count inodes with the--inodesflag. Use it to scan directories and sort results by inode usage:Step 1: Navigate to the Affected Filesystem#Start with the mount point showing highIUse%(e.g.,/in the sampledf -ioutput above):cd /Step 2: List Inode Usage for Directories#Rundu --inodes -s *to get a summary of inodes used by each top-level directory:du --inodes -s * | sort -nr--inodes: Count inodes instead of disk space.-s: Show a summary for each directory (instead of recursing into subdirectories).sort -nr: Sort results numerically (-n) in reverse order (-r) to show the largest inode users first.Sample Output:#495000 var 25000 home 12000 usr 500 tmp ...Here,/varis using 495,000 inodes—likely the culprit.Method 2: Drill Deeper into Suspect Directories#Once you identify a high-inode directory (e.g.,/var), repeat the process to narrow it down:cd /var du --inodes -s * | sort -nrSample Output:#480000 log 10000 cache 5000 lib ...Now we see/var/logis the main offender. Continue drilling:cd /var/log du --inodes -s * | sort -nrSample Output:#450000 apache2 25000 mysql 5000 syslog ...Bingo!/var/log/apache2has 450,000 inodes—likely due to unrotated Apache access/error logs.Method 3: Usencdu(Interactive Inode Scanner)#For a faster, visual alternative, usencdu(NCurses Disk Usage), a terminal-based tool that scans directories and displays inode usage interactively.Installncdu:## Debian/Ubuntu sudo apt install ncdu # RHEL/CentOS sudo yum install ncdu # Fedora sudo dnf install ncduScan for Inodes withncdu:#Runncdu -x /to scan the root filesystem (/) and focus on inodes:ncdu -x /-x: Scan only the current filesystem (avoids crossing into mounted drives like/procor/mnt).Usingncdu:#Navigate with arrow keys.Directories with high inode counts are highlighted.Pressito toggle between disk space and inode mode (look for[I]in the status bar).ncduis far faster than manualducommands and ideal for large filesystems.Method 4:findCommand (For Advanced Users)#If you need to count files in a directory (and its subdirectories) directly, usefindwithwc -l(word count, line mode):find /var/log/apache2 -type f | wc -lThis returns the total number of files in/var/log/apache2(each file 1 inode).Note:findcan be slow on directories with millions of files. Usedu --inodesorncdufor faster results.Freeing Up Inodes: Practical Solutions#Once you’ve identified the directory hoarding inodes, it’s time to free them up. Here are actionable steps:1. Delete Unnecessary Files#The most direct fix is to delete unneeded files. Common targets include:Old Logs#Log files (e.g., Apache, Nginx, MySQL) often accumulate indefinitely if not rotated. Delete old logs in/var/log/service:# Example: Delete Apache logs older than 30 days sudo find /var/log/apache2 -name access.log.* -mtime 30 -delete-mtime 30: Target files modified more than 30 days ago.Cached Files#Application caches (e.g.,/var/cache/apt,/var/cache/yum) can grow large. Clean them with:# Debian/Ubuntu sudo apt clean # RHEL/CentOS sudo yum clean allOrphaned Temporary Files#Files in/tmp(temporary storage) are often left behind by crashed applications. Delete files older than 7 days:sudo find /tmp -type f -mtime 7 -delete2. Enable Log Rotation#Prevent log files from consuming inodes by configuringlogrotate, a tool that automatically rotates, compresses, and deletes old logs.ExamplelogrotateConfig for Apache:#Edit/etc/logrotate.d/apache2to set rotation rules:/var/log/apache2/*.log { daily # Rotate daily missingok # Ignore missing logs rotate 7 # Keep 7 days of logs compress # Compress old logs delaycompress # Delay compression until next rotation notifempty # Don’t rotate empty logs create 640 root adm }Test the config with:sudo logrotate -d /etc/logrotate.d/apache2 # d for dry run3. Clean Up Docker Resources#Docker (and container tools) often leave behind unused images, containers, or volumes, which consume inodes. Usedocker system pruneto clean up:# Delete all unused Docker resources (images, containers, networks, caches) sudo docker system prune -aFor volumes (which may contain many small files):# List volumes docker volume ls # Delete unused volumes docker volume prune4. Use Hard Links Instead of Copies#If you need multiple copies of a file, use hard links (ln) instead of copying (cp). Hard links share the same inode, so they don’t consume extra inodes:ln /path/to/original /path/to/link # Creates a hard link (no new inode)5. Resize the Filesystem or Reformat (Last Resort)#If inode limits are critically low and you cant delete enough files, you have two options:Option A: Resize the filesystem (for disk space, not inodes)If you need more disk space (not more inodes), resize the filesystem. This works only if the filesystem is on an LVM volume (allowing online resizing):sudo resize2fs /dev/sda1 # Replace /dev/sda1 with your deviceOption B: Reformat with more inodesForext4and most filesystems, inode counts are fixed at creation time—resize2fscannot increase inodes. To increase inodes, you must reformat the partition with a new filesystem, specifying a higher inode ratio:mkfs.ext4 -i 16384 /dev/sda1 # Allocate 1 inode per 16KB (default: 4KB for small disks)Alternatively, use filesystems that support dynamic inode allocation, such asXFSorbtrfs, which automatically allocate inodes as needed.Warning: Reformatting erases all data! Always back up first.Preventive Measures to Avoid Future Inode Shortages#Stop inode exhaustion before it happens with these proactive steps:1. Monitor Inode Usage#Use tools likePrometheus Node ExporterorNagiosto trackIUse%and alert when it exceeds 85%.Example Prometheus Query:#node_filesystem_inodes_used_percent{mountpoint/} 852. Automate Cleanup with Cron Jobs#Schedule regular cleanup of temporary files, logs, and caches usingcron.Example Cron Job (Clean/tmpWeekly):#Edit crontab withcrontab -eand add:0 0 * * 0 find /tmp -type f -mtime 7 -delete # Run weekly on Sunday at midnight3. Choose Filesystems with Flexible Inodes#When creating new filesystems, use types likexfsorbtrfs, which dynamically allocate inodes (unlikeext4, which sets a fixed number at creation). Forext4, increase inodes during creation with:mkfs.ext4 -i 16384 /dev/sda1 # Allocate 1 inode per 16KB (default: 4KB for small disks)Discover moreusedOperating SystemsLinux UnixConclusion#Inode exhaustion is a common pitfall, but with the right tools, you can quickly identify and resolve it. By checking inode usage withdf -i, scanning directories withdu --inodesorncdu, and cleaning up unnecessary files, you’ll free up inodes and restore system functionality. Pair this with preventive measures like log rotation and monitoring to avoid future issues.References#Linux Inode DocumentationdfMan Page:man dfduMan Page:man duncduOfficial Site: NCurses Disk UsagelogrotateMan Page:man logrotateDocker System Prune: Docker Docs