How to Fix "No Space Left on Device" Error in Linux
The "No space left on device" error is common when your disk fills up. But sometimes it appears even when you have free space, due to inode exhaustion. Here is how to diagnose and fix both issues.
Fix 1: Check Disk Usage
Run df -h to see disk usage on all mounted filesystems. Look for partitions at 100% usage. The output shows size, used space, available space, and mount point. If a partition is full, you need to free up space or expand the partition. Run du -sh /* to find the largest directories consuming space.
Fix 2: Clean Package Manager Cache
Package managers store downloaded packages in cache. Clean the APT cache with sudo apt clean and sudo apt autoremove. For DNF, use sudo dnf clean all. For Pacman, use sudo pacman -Sc. This can free up several gigabytes of space, especially if you have not cleaned the cache in months.
Fix 3: Remove Old Kernels
Ubuntu and Debian keep multiple old kernel versions that take significant space. List installed kernels with dpkg --list | grep linux-image. Remove old kernels with sudo apt autoremove --purge. On Red Hat systems, use sudo package-cleanup --oldkernels --count=2 to keep only the last two kernels.
Fix 4: Check Inode Usage
If df -h shows free space but you still get the error, you may have exhausted inodes. Run df -i to check inode usage. If inodes are at 100%, you have too many small files. Find directories with millions of files using find / -xdev -printf '%h\n' | sort | uniq -c | sort -k1 -rn | head -10.