Skip to content
ShellOps Daily

Linux server hardening, systemd, and Bash automation tips

Disk & Logs

Fix No Space Left on Device When Df Shows Free Space

Victor Novak7 min read

Encountering the error 'No space left on device' while df shows free space is a frustrating and common issue in Linux environments. This paradox often stems from inode exhaustion rather than actual disk space depletion. Inodes are metadata entries that track files, and when they're filled, the system can't create new files—even if there's still space available. Understanding this distinction is crucial for resolving the issue effectively.

This article walks through the steps to diagnose and fix inode exhaustion on Linux servers. We’ll explore how to identify inode usage with df -i, compare it with block space, and locate problematic files or processes that consume inodes. Practical solutions include removing orphaned files, using lsof to find open files, and implementing automation to prevent future issues.

Whether you're managing a VPS or a production server, the key to avoiding this error lies in monitoring both block and inode usage. We’ll also cover advanced tools like journalctl and inodetop for deeper troubleshooting. By addressing inode exhaustion proactively, you can ensure smoother system operations and avoid unexpected outages.

Understanding the Paradox of Free Space and No Space Left

The error 'no space left on device' can occur even when df -h indicates available disk space. This paradox is typically caused by inode exhaustion, not a lack of block space. Understanding the distinction between block space and inodes is crucial for diagnosing and resolving this issue effectively.

What Is an Inode and Why It Matters

An inode is a data structure that stores metadata about a file, such as its permissions, ownership, and location on the disk. Each file, directory, and symbolic link consumes one inode, making inodes a critical resource for file system operations. When inodes are exhausted, the system can no longer create new files or directories, even if there is free block space available.

The Difference Between Block Space and Inode Usage

Block space refers to the actual storage allocated for file data, while inodes track the metadata necessary for file management. df -h displays block space usage, but df -i shows inode usage. A system can report free block space but still fail to create new files due to inode exhaustion. This distinction is vital for troubleshooting, as it highlights the need to monitor both resources separately.

Diagnosing the Root Cause of the Error

Diagnosing the root cause of the 'No space left on device' error requires a careful examination of both block space and inode usage. While df -h provides information about disk space, df -i reveals whether the system is running out of inodes, which are essential for tracking files. Understanding this distinction is crucial for identifying the true source of the issue.

Run Df -i to Check Inode Usage

To check inode usage, run df -i. This command displays the total number of inodes, used inodes, and the percentage of inodes used. If the percentage is close to 100%, it indicates that the system has exhausted its inode limit, which can cause the 'No space left on device' error even when there is free block space available.

Compare Df -h and Df -i Output

Compare the output of df -h and df -i to get a full picture of disk usage. df -h shows how much disk space is used and available, while df -i highlights inode exhaustion. If df -h shows free space but df -i shows high inode usage, the issue is likely related to a large number of small files. This can happen when temporary files, logs, or caches are not being properly cleaned up.

Look for processes or services that might be generating excessive temporary files. Use tools like lsof to identify open files and determine which applications are consuming inodes. This step helps pinpoint the source of the problem and guides the next phase of troubleshooting.

Fixing Inode Exhaustion on Linux Systems

Fixing inode exhaustion requires identifying and eliminating unnecessary files that consume inodes. This involves locating orphaned files and using tools to find open files that are still using inodes. Manual removal and automated cleanup are both essential to prevent future issues.

  1. Identify and remove temporary or unused files that consume inodes.
  2. Use tools like lsof and find to locate and delete orphaned files.
  3. Consider increasing the inode limit if the system is regularly running out of them.

Locate and Remove Orphaned Files

Orphaned files are those that no process is actively using but still occupy inodes. These files can accumulate over time, especially from incomplete operations or failed backups. To remove them, use the find command with the -type f option to locate all regular files, then filter by size or modification time to narrow down the list. Once identified, delete the files using rm or a safer alternative like rm -i to avoid accidental removal of important data.

Use Lsof to Find Open Files Using Inodes

The lsof command can help identify open files that are still using inodes. Run lsof -i to list all open files associated with network connections, or lsof -u to filter by user. For a more focused search, use lsof with specific file types or paths. Once identified, terminate the associated process or manually close the file to free up the inode. This approach is useful for resolving issues where files are still open but not actively used.

Preventing Future Inode Exhaustion

Preventing future inode exhaustion requires proactive maintenance strategies that ensure consistent disk space and inode management. By automating cleanup processes and configuring tools like logrotate, system administrators can avoid the pitfalls of uncontrolled inode usage and maintain stable server environments.

Automate File Cleanup with Cron Jobs

Automate File Cleanup with Cron Jobs is an effective way to ensure regular removal of temporary and unnecessary files that consume inodes. Create a script that deletes files in directories such as /tmp, /var/tmp, and user-specific temporary folders. Schedule this script using cron to run at intervals, such as daily or weekly, to prevent inode buildup. This method ensures that system resources are consistently freed up without manual intervention.

Set Up Logrotate for Log File Management

Set Up Logrotate for Log File Management is crucial for preventing inode exhaustion caused by excessive log file creation. Configure logrotate to periodically rotate, compress, and remove old log files, ensuring that they do not consume inodes indefinitely. Define retention policies and specify which log files should be rotated, and ensure that the rotation process is set to occur automatically. This helps maintain a balance between log data retention and inode availability, supporting long-term system stability.

Advanced Troubleshooting and Monitoring

Advanced troubleshooting and monitoring tools can help identify and resolve persistent inode exhaustion issues. By leveraging systemd's journaling capabilities and specialized monitoring utilities, administrators can proactively manage inode usage and prevent future out-of-space errors.

Use Journalctl to Debug Systemd Services

Use journalctl to Debug Systemd Services: Journalctl provides detailed logs of systemd service activities, which can reveal services generating excessive files or consuming inodes. Run 'journalctl -u <service-name>' to inspect logs for specific services. Look for patterns of file creation or unexpected behavior that may indicate misconfigured services or rogue processes. This helps pinpoint the source of inode exhaustion and allows for targeted fixes.

Monitor Inode Usage with Iotop or Inodetop

Monitor Inode Usage with iotop or inodetop: Tools like iotop and inodetop offer real-time insights into process activity and inode usage. iotop focuses on disk I/O, while inodetop tracks inode operations. Use these tools to identify processes that are creating or accessing a high number of inodes, which can help isolate the cause of the issue. Integrating these tools into regular monitoring routines ensures early detection and intervention, minimizing downtime and system instability.

Frequently Asked Questions

Why does df -h show free space but I get 'no space left on device'?

df -h shows free block space, but 'no space left on device' often indicates inode exhaustion. Inodes track files, not data, so even with free blocks, inodes can be full. Check with df -i to confirm.

How do I check if my system is running out of inodes?

Use the df -i command to check inode usage. This will display the number of inodes used and available, helping to identify if inodes are the limiting factor.

Can I increase the number of inodes on my Linux filesystem?

Inodes are defined at filesystem creation and cannot be increased without reformatting the disk. This is a rare but necessary action for critical systems.

What tools can I use to find and remove orphaned files consuming inodes?

Tools like lsof and find can help locate and remove orphaned files. lsof can find open files using inodes, while find can search for deleted files still consuming inodes.

Final Thoughts

Understanding the difference between block and inode usage is key to resolving space issues. Regularly monitor inode usage and implement automated cleanup tools to prevent future problems.

By using commands like df -i and lsof, you can effectively identify and remove files consuming inodes. Establish a routine for file management to maintain system stability.