如何解决 Linux 中“磁盘空间不足”的问题
文件系统 磁盘空间
![](/data/attachment/album/201801/03/064323bm1b14cv9s4sm61l.jpg)


明明有很多剩余空间，但 Linux 系统依然提示没有空间剩余。为什么会这样呢？Linux 偶尔会有一些令人沮丧的模糊的错误消息出现，而这就是其中一种。不过这种错误通常都是由某几种因素导致的。


### 通过 du 和 df 检查磁盘空间


在开始行动前，最好先检查一下是否磁盘上是否确实还有空间剩余。虽然桌面环境的工具也很不错，但命令行上的工具更直接，要好的多。


![Linux Filesystem du](/data/attachment/album/201801/03/064331vo5gp46bgzfbjfy9.jpg "Linux Filesystem du")


首先让我们看看 `du` 命令。用它来检查问题磁盘所在的挂载点目录。本文假设出问题的分区挂载点为根目录。



```
sudo du -sh /

```

![Linux Filesystem df](/data/attachment/album/201801/03/064332cmgwxqwrd3uuirs4.jpg "Linux Filesystem df")


由于它要遍历磁盘中的所有文件，因此需要花费一点时间。现在再让我们试试 `df`。



```
sudo df -h

```

把根目录和在其中挂载的文件系统加在这条命令的后面。比如，若你的有一个独立的磁盘挂载到 `/home`，那么除了根目录之外，你也需要把它加进来。使用空间的总和应该跟你 `du` 命令得到的结果接近。否则的话，就说明可能有已删除文件的文件被进程占用。


当然，这里主要专注点在于这些命令的结果是否要小于磁盘的大小。如果确实小于磁盘大小，那么很明显有很多地方不对劲。


**相关**：[使用 Agedu 分析硬盘空间使用状况](https://www.maketecheasier.com/agedu-analyze-hard-disk-space-usage-in-linux/ "Use Agedu to Analyze Hard Disk Space Usage in Linux") 


### 可能的原因


这里列出了一些产生这种情况的主要原因。若你发现 `du` 和 `df` 的结果之间有差别，那么可以直接检查第一项原因。否则从第二项原因开始检查。


#### 已删除文件被进程所占用


有时，文件可能已经被删掉了，但有进程依然在使用它。在进程运行期间，Linux 不会释放该文件的存储空间。你需要找出这个进程然后重启这个进程。


![Check processes for deleted files](/data/attachment/album/201801/03/064332vji6ttfids9qjvvz.jpg "Check processes for deleted files")


使用下面命令来定位进程。



```
sudo lsof / | grep deleted

```

这应该会列出出问题的进程了，然后重启该进程。



```
sudo systemctl restart service_name

```

#### i 节点不够了


![Linux check filesystem inodes](/data/attachment/album/201801/03/064332xceb9tj0f4bbqmqf.jpg "Linux check filesystem inodes")


文件系统中有一些称为 “<ruby> i 节点 <rt>  inode </rt></ruby>” 的元数据，其用来保存文件的相关信息。很多文件系统中的 i 节点数量是固定的，因此很可能 i 节点已经耗尽了而文件系统本身还没有用完。你可以使用 `df` 来检查。



```
sudo df -i /

```

比较一下已用的 i 节点和总共的 i 节点数量。如果没有可用的 i 节点了，那么很不幸，你也无法扩充 i 节点。删除一些无用的和过期的文件来释放一些 i 节点吧。


#### 坏块


最后一个很常见的问题就是坏的文件系统块。除非另有标记，否则操作系统很可能会认为这些块都是可用的，这会导致文件系统损坏或者硬盘坏死。最好是使用带 `-cc` 标志的 `fsck` 搜索并标记出这些块。记住，你不能使用正在使用的文件系统（LCTT 译注：即包含坏块的文件系统）中的 `fsck` 命令。你应该会要用到 live CD。



```
sudo fsck -vcck /dev/sda2

```

很明显，这里需要使用你想检查的磁盘路径取代命令中的磁盘位置。另外，要注意，这恐怕会花上很长一段时间。


**相关**：[使用 fsck 检查并修复你的文件系统 [Linux]](https://www.maketecheasier.com/check-repair-filesystem-fsck-linux/ "Check and Repair Your Filesystem With fsck [Linux]")


希望这些方案能解决你的问题。这种问题在任何情况下都不是那么容易诊断的。但是，在运气好的情况下，你可以把文件系统清理干净并让你的硬盘再次正常工作。




---


via: <https://www.maketecheasier.com/fix-linux-no-space-left-on-device-error/>


作者：[Nick Congleton](https://www.maketecheasier.com/author/nickcongleton/) 译者：[lujun9972](https://github.com/lujun9972) 校对：[wxy](https://github.com/wxy)


本文由 [LCTT](https://github.com/LCTT/TranslateProject) 原创编译，[Linux中国](https://linux.cn/) 荣誉推出


OK
![No Space On Device Feature](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-feature.jpg)

Most of the time, Linux is pretty specific with its error messages. “This didn’t work – try installing this package.” However, there’s one in particular that really doesn’t help me very much: “No space left on device.” What causes that? I thought I had 2 TB of storage, how can it be full? Where do I start looking? Today, we’ll be walking you through all of that in our guide on how to fix the “No space left on device” error on Linux.

**Also read:** [How to Easily Rename Files in Linux](https://www.maketecheasier.com/rename-files-in-linux/)

## Check the Space Left on Device

Before you go any further, it’s a good idea to check that there really is space left on the disk. While the tools in your desktop environment are good, it can be faster to use the direct ones from the command line.

If you’d like to use the tools in your desktop environment, they’ll give you easy-to-read representations of the information you can find with these tools. I’m using Fedora with GNOME, and the GNOME Disk Usage Analyzer tool shows me the following.

![No Space On Device Disk Analyzer](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-disk-analyzer.jpg)

Begin with `du`

. Point it to the [base directory](https://www.maketecheasier.com/delete-directory-linux/) on the drive that’s having the problem. Let’s assume it’s the partition with `/`

.

sudo du -sh /

![Linux Filesystem df No Space On Device Du](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-du-e1617063263666.png)

It’ll take some time to go through everything. Now, try with `df`

.

sudo df -h

![No Space On Device Df](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-df-e1617063230208.png)

Add `/`

and the filesystems mounted under it. For example, if you have `/home`

on a separate drive, add that in with the reading for `/`

. The total should come out close to what you had with `du`

. If not, that might point toward a deleted file being used by a process.

Of course, the main concern here is whether or not the results of these commands come in under the size of the drive. If it did, there’s obviously something wrong.

**Also read:** [How to Run Bash Commands in the Background in Linux](https://www.maketecheasier.com/run-bash-commands-background-linux/)

## No Space on Device Possible Causes

There are a couple of main causes here. If you saw a discrepancy between `du`

and `df`

, you can jump down to the first option here. Otherwise, start at the second one.

### Deleted File Reserved by Process

Occasionally, a file will be deleted, but a process is still using it. Linux won’t release the storage associated with the file while the process is still running, but you can find the process and restart it.

![Check processes for deleted files No Space On Device Lsof Deleted](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-lsof-deleted-e1617063333273.png)

Try to locate the process.

sudo lsof / | grep deleted

The problematic process should be listed. Just restart it.

`sudo systemctl restart service_name`

If it’s not immediately evident, do a full daemon reload.

`sudo systemctl daemon-reload`

**Also read:** [Fixing “username is not in the sudoers file. This incident will be reported” Error In Ubuntu](https://www.maketecheasier.com/fixing-sudo-error-in-ubuntu/)

### Not Enough Inodes

There is a set of metadata on filesystems called “inodes.” [Inodes](https://www.maketecheasier.com/how-do-linux-inodes-work/) track information about files. A lot of filesystems have a fixed amount of inodes, so it’s very possible to fill the max allocation of inodes without filling the filesystem itself. You can use `df`

to check.

sudo df -i /

![Linux check filesystem inodes No Space On Device Inodes](https://www.maketecheasier.com/assets/uploads/2021/03/no-space-on-device-inodes-e1617063405626.png)

Compare the inodes used with the total inodes. If there’s no more available, unfortunately, you can’t get more. Delete some useless or out-of-date files to clear up inodes.

### Bad Blocks

The last common problem is bad filesystem blocks. Filesystems can become corrupt over time, and hard drives die. Your operating system will most likely see those blocks as usable unless they’re otherwise marked. The best way to find and mark those blocks is by using `fsck`

with the `-cc`

flag. Remember that you can’t use `fsck`

from the same filesystem that you’re testing, so you’ll probably need to use a live CD.

sudo fsck -vcck /dev/sda2

Obviously, replace the drive location with the drive that you want to check. You can find that by using the `df`

command from earlier. Also, keep in mind that this will probably take a long time, so be prepared to grab a coffee.

**Also read:** [Check and Repair Your Filesystem With fsck [Linux]](https://www.maketecheasier.com/check-repair-filesystem-fsck-linux/)

Hopefully, one of these solutions solved your problem. This issue isn’t exactly easy to diagnose in every instance. With any luck, though, you can get it cleared up and have your system working again normally.

If you’re looking for more Linux pointers, see our guide on [how to set up Bluetooth in Linux](https://www.maketecheasier.com/setup-bluetooth-in-linux/). Or, for something a little different, see [how to install Mac’s Safari browser in Linux](https://www.maketecheasier.com/install-safari-on-linux/). Enjoy!

**Also read:** [4 Ways to Clone an Entire Hard Drive on Linux](https://www.maketecheasier.com/clone-entire-hard-drive-linux/)

Our latest tutorials delivered straight to your inbox