Linux No Space Left on Device — Inode Exhausted Fix
In this tutorial, you'll learn about Linux No Space Left on Device. We cover key concepts, practical examples, and best practices.
Linux throws "No space left on device" even when df -h shows free space. This happens when the filesystem runs out of inodes — the data structures that store file metadata. Each file consumes one inode, and small files can exhaust them quickly.
The Problem
df -h
Shows available space:
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 30G 20G 60% /
But creating a file fails:
touch test.txt
touch: cannot touch 'test.txt': No space left on device
Wrong Approach
# WRONG — looking at space when the problem is inodes
df -h # Shows free space — confusing
du -sh / # Also shows free space — misleading
Right Approach
# Check inode usage instead
df -i /
Expected output:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3.2M 3.2M 0 100% /
Step-by-Step Fix
Step 1: Check inode usage
df -i /
Step 2: Find the directory with the most files
for d in /*; do echo "$(find "$d" -xdev 2>/dev/null | wc -l) $d"; done | sort -rn | head -10
Step 3: Find and remove small unnecessary files
# Find directories with many files
find /var/spool -type f | wc -l
# Remove old log files
find /var/log -name "*.log" -mtime +30 -delete
# Clear package cache
sudo apt-get clean
sudo yum clean all
Step 4: Remove old kernels
# Debian/Ubuntu
sudo apt-get autoremove --purge
# RHEL/CentOS
sudo package-cleanup --oldkernels --count=2
Step 5: Clean up Docker containers and images
docker system prune -af
Step 6: Delete old temporary files
sudo rm -rf /tmp/*
sudo find /var/tmp -type f -atime +7 -delete
Step 7: Verify recovery
df -i /
Expected:
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 3.2M 1.5M 1.7M 47% /
Prevention Tips
- Monitor inode usage with
df -ialongsidedf -h - Set up log rotation with
logrotateto prevent log file accumulation - Store large numbers of small files on filesystems with high inode counts (XFS, ext4 with
-ioptions) - Use databases instead of files for high-volume data storage
- Set up monitoring alerts at 80% inode usage
Common Mistakes with no space left
- Forgetting
deriving (Show, Eq)on custom data types needed for debugging - Placing the wildcard pattern first in case expressions, making all subsequent patterns unreachable
- Using
headandtailinstead of pattern matching, causing runtime errors on empty lists
These mistakes appear frequently in real-world LINUX code. DodaTech's contributors have identified these patterns through analysis of open-source projects and production systems.
Practice Exercise
Write a pure function that safely divides two integers using Maybe, then test it with edge cases like division by zero and negative numbers.
This exercise reinforces the concepts covered in this guide. Try implementing it before checking online solutions.
FAQ
Built by the developers of DodaTech
Doda Browser, DodaZIP & Durga Antivirus Pro