How to Resolve 'No Space Left on Device' Error on Linux

Published July 03, 2024

Problem: "No Space Left on Device" Error in Linux

The "No Space Left on Device" error in Linux happens when a system runs out of storage space. This issue can stop you from saving files, installing software, or doing other tasks that need disk space. It can cause system instability and slow performance, making it a problem for Linux users.

Diagnosing the Problem: Checking Disk Space and Inodes

Using df Command to Check Disk Space

The df command checks disk space usage in Linux. It shows information about used and available space on file systems. To use df, open a terminal and type 'df' with any options.

When you run df, it shows a table with columns for file system, total size, used space, available space, and mount point. The '-h' option makes the output easier to read by showing sizes in KB, MB, or GB.

Examining Inode Usage with df -i

Inodes store data about files in Linux file systems. Each file uses one inode, no matter its size. To check inode usage, use the 'df -i' command.

This command shows a table like the regular df output, but with inode information instead of disk space. The columns include total inodes, used inodes, free inodes, and the percentage of inodes used. If you see a high percentage of inodes used, even with free disk space, you might have an inode problem.

Methods to Resolve the 'No Space Left on Device' Error

Clearing Disk Space on Linux

To free up disk space on Linux, find large files and directories. Use the 'du' command with options like '-h' for readable sizes and '-s' for summary. For example, 'du -sh /path/to/directory/*' shows the size of each item in a directory.

After finding large files and directories, remove unneeded data. Delete old log files, temporary files, and cached data. Clean up package manager caches with commands like 'apt clean' for Debian-based systems or 'yum clean all' for Red Hat-based systems. Remove old kernels and unused applications to create more space.

Dealing with Inode Exhaustion

When facing inode exhaustion, focus on deleting small files. Many small files can use up inodes even if they don't take much disk space. Use 'find' to locate and remove unneeded small files. For example, 'find /path -type f -size -1M -delete' removes files smaller than 1MB.

Increasing the inode count is possible but requires reformatting the file system. This is a last option and should be done carefully, as it involves backing up data, recreating the file system with more inodes, and restoring the data.

Handling Deleted but Open Files

Sometimes, files deleted by the system are still held open by running processes, taking up space. Use the 'lsof' command to find these files. Run 'lsof | grep deleted' to list deleted files still in use.

To release the file handles, restart the processes using these files. If it's safe, you can also reboot the system to close all file handles. For critical systems, identify the specific process holding the file open and restart only that process to minimize downtime.

Alternative Solutions for Storage Issues

Expanding File System or Partition Size

You can add new disks or partitions to your Linux system when you need more storage space. To add a new disk, connect it physically, then use 'fdisk' or 'parted' to create partitions. After creating partitions, format them with a file system using 'mkfs' and mount them to make them accessible.

You can also resize existing partitions. Use tools like 'gparted' (graphical) or 'parted' (command-line). Always back up your data before resizing. For non-root partitions, unmount them first. For root partitions, use a live CD or USB to perform the resize operation.

Utilizing Network Attached Storage (NAS)

Network Attached Storage (NAS) expands storage over a network. NAS systems provide large amounts of storage that multiple devices can access. They're useful for centralized file storage and backup.

To mount a NAS on Linux, use protocols like NFS or SMB. For NFS, use the 'mount' command with the NFS share details. For SMB shares, use 'mount.cifs' or add an entry to '/etc/fstab' for automatic mounting at boot. NAS systems offer flexibility for storage needs.

Preventive Measures to Avoid Future 'No Space Left on Device' Errors

Set Up Regular Disk Space Monitoring

To prevent "No Space Left on Device" errors, set up alerts for disk space usage. Use tools like Nagios or Zabbix to monitor disk space and send notifications when usage reaches a set level. For example, set alerts to trigger when disk usage goes over 80%.

Monitoring tools show your system's storage health. Tools like Glances or Netdata give real-time views of disk usage trends. These tools can help you spot issues before they become problems, letting you act quickly.

Optimize Storage Usage in Linux

Use good practices for storage management in Linux. Often review and remove unneeded files, like old backups or temp files. Use quotas to limit user disk space use. Set up automatic cleanup tasks with cron jobs to remove old logs or cache files.

Compression can save space. Use tools like gzip or bzip2 for files you rarely use. For databases, try compression features from the database system. File systems like Btrfs or ZFS have built-in compression, which can save space without extra work.