df和du的区别以及磁盘空间使用问题

本文主要介绍了du和df命令在统计磁盘使用情况时的差异。du命令递归统计指定目录或文件的磁盘使用量,有多个链接的文件只统计一次;df命令显示已挂载文件系统的磁盘占用、使用和可用空间等信息。当后台程序写入文件后文件被删除,df和du统计结果会有差异,df值更高。
solaris sparc
root

df -kb 结果
/dev/vx/dsk/home     12197220 11086515  988733    92%    /home

du -ks 查看/home分区情况为
10      dep
27      lost+found
395517  oracle
288     bigml.lsof
212309  wei
4041696 lan
346074  xue
520     xue.log
du相加的结果和df的结果不同?!


下面这篇文章给出答案

Document Id: 26928Synopsis: du and df Differences (originally published 8/91)
Update date: 2001-05-13Description: du and df Differences
-- --- -- -----------

This article explains how reporting disk usage du and reporting free disk space
on file systems df may show different numbers.

du
--

The du user command gives the number of kilobytes contained in all files and,
recursively, directories within each specified directory or file (filename).
If filename is missing, `.' (the current directory) is used.  A file which
has multiple links to it is only counted once.

EXAMPLE:

  system % du

  5    ./jokes
  33   ./squash
  44   ./tech.papers/lpr.document
  217  ./tech.papers/new.manager
  401  ./tech.papers
  144  ./memos
  80   ./letters
  388  ./window
  93   ./messages
  15   ./useful.news
  1211 .

Note that the last number, 1211 is the grand total (in kilobytes) for the
directory.

df
--

The df user command displays the following information:

  amount of disk space occupied by currently mounted file systems
  the amount of used and available space
  how much of the file system's total capacity has been used

Used without arguments, df reports on all mounted file systems.

EXAMPLE:

  system % df

  Filesystem  kbytes  used  avail  capacity  Mounted on
  /dev/ip0a    7445    4714 1986   70%       /
  /dev/ip0g   42277   35291 2758   93%       /usr

Note: used plus avail is less than the amount of space in the file system
(kilobytes) because the system reserves a fraction of the space in the file
system to allow its allocation routines to work well.  The amount reserved is
typically about 10%.  (This may be adjusted using the tunefs command.  Refer to
the man pages on tunefs(8) for more information.)  When all the space on a file
system, except for this reserve, is in use, only the super-user can allocate
new files and data blocks to existing files.  This, however, may cause the file
system to be over allocated.  When a file system is over allocated in this way,
df may report that the file system is more than 100% utilized.

If arguments to df are disk partitions (for example, /dev/ip0as or path names),
df produces a report on the file system containing the named file.  Thus, df
shows the amount of space on the file system containing the current directory.

Problem Definition
------- ----------

This section gives the technical explanation of why du and df sometimes report
different totals of disk space usage.

When a program that is running in the background writes to a file while the
process is running, the file to which this process is writing is deleted.
Running df and du shows a discrepancy in the amount of disk space usage.  The
df command shows a higher value.

Explanation Summary
----------- -------

When you open a file, you get a pointer.  Subsequent writes to this file
references this file pointer.  The write call does not check to see if the file
is there or not.  It just writes to the specified number of characters starting
at a predetermined location.  Regardless of whether the file exist or not, disk
blocks are used by the write operation.

The df command reports the number of disk blocks used while du goes through the
file structure and and reports the number of blocks used by each directory.  As
far as du is concerned, the file used by the process does not exist, so it does
not report blocks used by this phantom file.  But df keeps track of disk blocks
used, and it reports the blocks used by this phantom file.
<think>我们正在解决一个实际问题:用户执行 `df -h` 命令后显示磁盘空间已满(如使用率达到100%),但实际文件占用的空间并没有达到磁盘总容量。根据引用内容,这个问题可能由多种原因引起。下面将逐步分析并提供解决方案。 ### 问题分析 1. **文件系统空间不足**:`df -h` 显示磁盘使用率为100%,但实际文件占用空间可能小于磁盘容量,这通常是因为: - 存在大量小文件占用了inode(索引节点)资源(引用[2]提到inode使用问题) - 已删除文件仍被进程占用(空间未释放) - 文件系统存在预留空间(如root保留空间) - LVM逻辑卷未扩展文件系统(引用[3]提到扩展逻辑卷后需扩展文件系统) 2. **关键检查点**: - 检查inode使用率:`df -i` - 检查被进程占用的已删除文件:`lsof | grep deleted` - 检查文件系统预留空间(通常为5%,可通过`tune2fs`调整) - 确认逻辑卷是否扩展后未调整文件系统大小 --- ### 解决方案步骤 #### 📌 步骤1:确认磁盘使用情况 ```bash df -h # 查看各分区使用率 ``` - 重点关注使用率100%的分区(如示例中的 `/` 分区) #### 📌 步骤2:检查inode使用率 ```bash df -i # 检查inode是否耗尽(引用[2]) ``` - **现象**:如果 `IUse%` 达到100%,即使磁盘空间充足,系统也会报"磁盘满" - **解决方法**: - 清理小文件(如临时文件、缓存文件) - 将小文件归档压缩后删除原文件 #### 📌 步骤3:查找被进程占用的已删除文件 ```bash lsof | grep deleted # 查看哪些进程占用了已删除文件 ``` - **现象**:文件已被删除,但进程仍持有文件句柄,空间未释放 - **解决方法**: - 重启持有文件的进程(如Apache/Nginx服务) - 或强制关闭进程:`kill -9 <PID>` #### 📌 步骤4:检查文件系统预留空间 - **默认预留5%空间给root用户**(防止普通用户占满磁盘导致系统崩溃) - 查看预留比例: ```bash tune2fs -l /dev/sda1 | grep "Reserved block count" ``` - 调整预留比例(如改为1%): ```bash sudo tune2fs -m 1% /dev/sda1 # 将预留空间设为1% ``` #### 📌 步骤5:扩展逻辑卷文件系统(适用于LVM) - 如果磁盘已扩容但未扩展文件系统(引用[3]): ```bash sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv # 扩展ext4文件系统 ``` - 若是XFS文件系统: ```bash sudo xfs_growfs /dev/mapper/ubuntu--vg-ubuntu--lv ``` #### 📌 步骤6:清理大文件 使用工具定位大文件: 1. 扫描目录大小: ```bash sudo du -sh /* 2>/dev/null | sort -hr # 查看根目录下各文件夹大小 ``` 2. 逐级定位大文件: ```bash cd /var # 进入大目录 du -sh * | sort -hr ``` 3. 清理建议目录: - `/var/log`:日志文件(用 `journalctl` 清理或配置日志轮转) - `/tmp`:临时文件 - 用户目录:检查下载文件、缓存等 --- ### 🌰 操作示例 #### 场景:根分区(/)显示空间满,但实际文件不大 ```bash $ df -h Filesystem Size Used Avail Use% Mounted on /dev/sda1 50G 50G 0 100% / # ← 问题分区 ``` 1. **检查inode**: ```bash $ df -i /dev/sda1 Filesystem Inodes IUsed IFree IUse% Mounted on /dev/sda1 2.4M 2.4M 0 100% / # inode耗尽! ``` - **处理**:删除小文件(如邮件队列 `/var/mail` 或缓存 `/var/cache`) 2. **检查进程占用已删除文件**: ```bash $ sudo lsof | grep deleted apache2 1234 root 5u REG 8,1 10G 123456 /var/log/apache.log (deleted) ``` - **处理**:重启Apache释放空间 ```bash sudo systemctl restart apache2 ``` --- ### 预防措施 1. **设置日志轮转**:编辑 `/etc/logrotate.conf` 限制日志大小 2. **监控工具**:安装 `ncdu`(磁盘分析)或 `inotifywait`(文件监控) 3. **定期清理**:使用 `cron` 定时任务清理 `/tmp` --- §§ 相关问题 § 1. [如何减少 Linux 系统 inode 使用率?](#) 2. [LVM 逻辑卷扩容后如何扩展文件系统?](#) 3. [如何安全清理 Linux 系统日志文件?](#) > ⚠️ **重要提示**: > - 清理系统文件前**务必备份**重要数据! > - 生产环境操作应在维护窗口进行 > 引用说明:inode耗尽问题[^2],LVM扩展后文件系统调整[^3]。</think>### 解决 `df -h` 显示磁盘空间已满的完整指南 当 `df -h` 显示磁盘空间已满但实际未满时,通常由以下原因引起。以下是系统化的排查步骤解决方案: --- #### 🔍 **一、核心排查步骤** 1. **检查文件系统使用率** ```bash df -h # 查看各分区使用情况 ``` - 重点关注 `Use%` 列,确认哪个分区显示 100%(如 `/` 或 `/home`) 2. **分析实际文件占用** ```bash sudo du -sh /* 2>/dev/null | sort -hr # 扫描根目录下各文件夹大小 ``` - 若 `du` 显示总大小远小于 `df` 结果,说明存在隐藏占用(如进程占用文件) 3. **检查 inode 使用情况**(引用[2]) ```bash df -i # 查看 inode 使用率 ``` - **现象**:`IUse%` 达到 100% 但磁盘空间充足 - **原因**:大量小文件耗尽 inode(如日志、缓存文件) 4. **查找被进程占用的已删除文件** ```bash sudo lsof | grep deleted # 查看被进程占用的"已删除"文件 ``` - 输出示例: ```bash apache2 1234 root 5u REG 8,1 10G 123456 /var/log/apache.log (deleted) ``` - 这些文件虽被删除,但进程仍持有句柄,空间未释放 --- #### 🛠️ **二、解决方案** ##### ✅ 场景 1:inode 耗尽(`df -i` 显示 100%) - **清理小文件**: ```bash # 清理日志(保留最近7天) sudo journalctl --vacuum-time=7d # 清理临时文件 sudo rm -rf /tmp/* sudo rm -rf ~/.cache/* ``` - **查找 inode 占用大户**: ```bash # 扫描占用 inode 最多的目录 sudo find / -xdev -printf '%h\n' | sort | uniq -c | sort -k1 -nr | head -20 ``` ##### ✅ 场景 2:进程占用已删除文件 - **重启相关进程**: ```bash # 根据 lsof 输出找到进程名(如 apache2) sudo systemctl restart apache2 ``` - **强制释放空间**(慎用): ```bash # 结束占用文件的进程 sudo kill -9 $(lsof -t +L1) # 结束所有占用已删除文件的进程 ``` ##### ✅ 场景 3:LVM 逻辑卷未扩展文件系统(引用[3]) ```bash # 扩展物理卷 → 卷组 → 逻辑卷后,必须扩展文件系统 sudo lvextend -L +10G /dev/mapper/ubuntu--vg-ubuntu--lv # 扩展逻辑卷 sudo resize2fs /dev/mapper/ubuntu--vg-ubuntu--lv # 扩展ext4文件系统 # 若为xfs文件系统:sudo xfs_growfs / ``` ##### ✅ 场景 4:系统保留空间(通常 5%) - **调整保留比例**: ```bash sudo tune2fs -m 1% /dev/sda1 # 将保留空间从5%降至1% ``` --- #### 📊 关键命令对比 | **命令** | **作用** | 使用场景 | |------------------|-----------------------------------|----------------------------| | `df -h` | 查看磁盘空间使用率 | 初步定位满盘分区 | | `df -i` | 检查 inode 使用率 | 大量小文件导致空间"假满" | | `lsof \| grep deleted` | 查找被进程占用的已删除文件 | 空间未释放问题 | | `resize2fs` | 扩展文件系统 | LVM 扩容后未生效时使用[^3] | --- #### ⚠️ 注意事项 1. **清理系统文件前备份**: ```bash sudo cp /etc/fstab /etc/fstab.bak # 关键配置文件备份 ``` 2. **避免直接删除日志文件**: - 使用 `logrotate` 管理日志(配置文件:`/etc/logrotate.conf`) 3. **定期监控工具推荐**: - `ncdu`:交互式磁盘分析工具 - `inotifywait`:实时监控文件变化 ---
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值