No Space Left on Device in Linux: Causes, Solutions, and Prevention
When working with Linux systems, encountering the "No space left on device" error can be frustrating and disruptive. Here's the thing — this message typically indicates that your storage partition has reached its capacity, preventing further file writes or system operations. Consider this: while it may seem like a critical issue, understanding its root causes and implementing effective solutions can resolve it efficiently. This guide explains how to diagnose, fix, and prevent this error in Linux environments.
We're talking about where a lot of people lose the thread.
Understanding the Error
The "No space left on device" error occurs when the filesystem hosting your data (e.Day to day, g. This can result from accumulated large files, log files, package caches, or temporary files. , /, /home, or /var) becomes full. Unlike Windows, where storage is managed through a single drive, Linux systems often use multiple partitions, and a full partition can halt specific operations even if other partitions have space The details matter here..
Step 1: Check Disk Usage with df and du
1.1 Use df to Identify the Full Partition
The df (disk free) command shows the usage of all mounted filesystems:
df -h
The -h flag provides human-readable output (e.And g. Consider this: , GB instead of blocks). Look for partitions with 100% usage under the "Use%" column Worth keeping that in mind..
Filesystem Size Used Avail Use% Mounted on
/dev/sda1 50G 50G 0B 100% /
Here, the root partition (/) is full.
1.2 Use du to Locate Large Directories
While df shows partition usage, du (disk usage) identifies which directories are consuming space. Start by checking the largest directories:
du -sh /* 2>/dev/null | sort -h
This lists top-level directories sorted by size. For deeper analysis:
du -sh /var/* 2>/dev/null | sort -h
Common culprits include /var/log, /var/cache, and /home.
Step 2: Locate Large Files with find
2.1 Search for Large Files
Use the find command to locate files larger than a specific size (e.g., 100MB):
find / -type f -size +100M -exec ls -lh {} \; 2>/dev/null
This scans the entire filesystem for files exceeding 100MB. Review the output to identify candidates for deletion or archiving.
2.2 Check for Hidden Files
Hidden files (starting with .) can also consume space. Use:
du -sh /home/* 2>/dev/null | sort -h
For hidden directories:
du -sh /home/.* 2>/dev/null | sort -h
Step 3: Clear Package Caches
3.1 Debian/Ubuntu (APT)
APT caches downloaded packages in /var/cache/apt/archives. Clean them with:
sudo apt-get clean
sudo apt-get autoclean
For a full cleanup:
sudo apt-get purge $(dpkg -l | awk '/^rc/ {print $2}')
3.2 RHEL/CentOS/Fedora (YUM/DNF)
Clear cached packages:
Here's a thinking process:
1. **Analyze the User's Request:**
- **Task:** Continue the article smoothly.
- **Constraint:** Do not repeat previous text.
- **Constraint:** Finish with a proper conclusion.
- **Context:** The user provided a partial article about "No space left on device" error in Linux, covering steps 1-3 (checking disk usage, finding large files, clearing package caches). The last line shown is for RHEL/CentOS/Fedora (YUM/DNF) but cuts off.
2. **Identify the Missing Piece:**
- The last line is: `### 3.2 RHEL/CentOS/Fedora (YUM/DNF)` followed by `Clear cached packages:` and then the text cuts off.
- I need to continue from there, completing step 3, then moving to step 4, step 5, and finally a conclusion.
- I must not repeat any of the already-shown text. I'll write fresh content that logically continues the article.
3. **Determine the Structure for the Continuation:**
- Complete step 3 (package cache cleanup for YUM/DNF).
- Add step 4: Manage logs and temporary files.
- Add step 5: Handle log rotation, cleanup old kernels, etc.
- Provide a conclusion that summarizes and wraps up the guide.
Let me outline the remaining steps mentally:
- Step 3.Day to day, use `tmpwatch`, `logrotate`, clean `/tmp`, journal logs. Log rotation, monitoring, cleanup scripts, autovacuum (if applicable), managing home directories.
So - Step 5: Preventive measures. - Step 4: Clear temporary files and logs. Day to day, 2 continuation: Clear cached packages (dnf clean all, yum clean all, maybe discuss rpm newt or pacman for Arch, but I'll stick to what's relevant or keep it general). - Conclusion: Summarize, stress root cause analysis, prevention is better than cure, command references.
Wait, the user said "Do not repeat previous text.2 completion, then do steps 4 and 5, then conclusion. So naturally, " So I shouldn't restate steps 1-3. I'll continue from step 3.I need to be careful not to copy the existing text.
You'll probably want to bookmark this section.
Let me re-read the provided text to understand the exact flow and tone. The text is structured with headings, code blocks, and bullet points. I'll match that style.
Key thing: The last visible line is `### 3.So 2 RHEL/CentOS/Fedora (YUM/DNF)` and `Clear cached packages:`. Day to day, the text cuts off there. I need to complete that thought and move on.
I'll write:
- Complete the `dnf clean all` / `yum clean all` command and maybe add pacman or snap cleanup if relevant, but I'll keep it to the two main ones or generalize.
- Actually, I'll just complete the dnf/yum section, then move to step 4.
Let me draft mentally:
**Step 3: Clear Package Caches** (but I can't repeat the heading exactly if it's considered previous text? The user said "Do not repeat previous text." I think it means don't copy-paste the exact same paragraphs. I can reuse the structure or headings if needed, but better to continue the content fluidly. I'll start from where it left off, completing the YUM/DNF section, then introduce new steps.)
Actually, safer: I'll continue the article from the cut-off point, completing step 3, then adding step 4, step 5, and conclusion. " I interpret that as: don't repeat the content that's already in the prompt. Practically speaking, i'll avoid repeating the exact headings "Step 1", "Step 2" etc. , or I can use them if they're part of the natural flow, but the instruction says "Do not repeat previous text.I can write new text that continues the article.
Let me draft:
...continuing from the cut-off:
"Clear cached packages:
```bash
sudo dnf clean all
or for YUM-based systems:
sudo yum clean all
```"
Then step 4: Managing logs and temporary files.
Step 5: Preventive maintenance.
Conclusion.
I'll make sure each section is distinct and moves forward.
Let me write it out properly. The prompt's last line is `### 3.So naturally, i'll start right after the cut-off point. Practically speaking, 2 RHEL/CentOS/Fedora (YUM/DNF)` and then `Clear cached packages:`. I'll complete that sentence/command, then proceed.
Actually, looking at the prompt's formatting, it's:
3.2 RHEL/CentOS/Fedora (YUM/DNF)
Clear cached packages:
And then it cuts off. So I need to complete the command and the thought.
I'll write:
```bash
sudo dnf clean all
sudo yum clean all
Then continue.
Let me structure the continuation:
Completing Step 3: Clear the cache... Move to Step 4: Managing system logs and temporary files. Step 5: Preventive measures and cleanup scripts. Conclusion: Wrap up.
I'll make sure not to repeat the exact wording from the first part. I'll use fresh phrasing but same topic.
Draft:
sudo dnf clean all
This removes