Skip to content
Français
Sync not enabled: your progress stays in this browser My learning
Select theme
Français

Control disk capacity and quotas

By updated

Improve this page

A full disk can bring a server to its knees: SSH sometimes refuses connections, and databases stop. This guide shows how to monitor space and inodes with df, find what takes up the space with du and ncdu, unblock the trap of the deleted file that is still open, and reclaim space without breaking the system, starting with what the system can recreate: logs, caches, old kernels. For beginner to intermediate administrators. Each output names, in its title, the machine it was captured on.

What you will learn

  • Monitor space and inodes with df
  • Find the largest directories with du and ncdu, and large files with find
  • Unblock a deleted file that is still open
  • Understand ext4 reserved blocks and keep the systemd journal in check
  • Reclaim space safely, in the right order

See free space with df

df (disk free) summarizes the usage of each mounted file system. The -h option makes sizes readable, -T adds the type, and excluding tmpfs removes the noise of virtual file systems:

Terminal window
df -hT -x tmpfs -x devtmpfs
Output (AlmaLinux)
Filesystem Type Size Used Avail Use% Mounted on
/dev/sdb2 ext4 20G 1.2G 19G 6% /
/dev/sdb1 vfat 99M 8.8M 90M 9% /boot/efi

The Use% column spots at a glance the volumes close to saturation. It is the first command to run when a "disk full" alert comes in.

Read the df columns

Each column answers a precise question. The one that surprises is Avail: it is not always Size - Used, because df subtracts the blocks reserved for root on ext4, explained further down.

ColumnMeaning
SizeTotal size of the file system
UsedSpace in use
AvailSpace actually available, reserved blocks excluded
Use%Percentage in use
Mounted onMount point

Target a mount point

df reasons per mount point: a server can have / at 40% and /var at 100%, and only checking /, /var, /tmp and /home separately shows it. When you pass a path, df displays only the file system that contains it. If df -h /var returns the same line as df -h /, /var is not on a separate partition.

Terminal window
df -h /var
df -h /tmp

Monitor usage in a script

For an automated alert, extract the single useful value. NR==2 skips the header line and $5 designates the Use% column.

Terminal window
usage=$(df -h / | awk 'NR==2 {gsub(/%/,""); print $5}')
if [[ "$usage" -gt 90 ]]; then
echo "ALERT: / is at ${usage}%"
fi

The script only checks /: if /var is a separate partition, add it to the check, otherwise saturation goes unnoticed.

Watch inodes: the trap of the "full" disk

A file system can show free space and still refuse to write: it has run out of inodes, the structures that describe each file. Their number is fixed at creation of the file system. Millions of small files (PHP sessions, caches, mail) can exhaust them well before the space. Check with df -i:

Terminal window
df -i -x tmpfs -x devtmpfs
Output (AlmaLinux)
Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sdb2 2557440 36172 2521268 2% /

If IUse% is at 100% while df -h shows space, the culprit is an inode shortage, not a lack of space. The fix is to delete the small files involved (or to recreate the file system with more inodes).

When to suspect inode exhaustion

The No space left on device message is the same when the space is full and when the inodes are exhausted. Three signals point to inodes.

  • df -h shows free space, but touch /tmp/test answers No space left on device. This signal points to inodes, provided df -h was read on the same mount point where the write fails (df -h /tmp in this example).
  • A directory contains millions of small files: cache, PHP sessions, queued mail.
  • IUse% exceeds 90% in df -i.

Find the directories that hold the most files

There is no equivalent of du for inodes: you have to count files directory by directory. The command ranks directories by number of direct files. It is slow on a big disk, and -xdev at least limits the walk to the current file system.

Terminal window
sudo find / -xdev -type d -exec sh -c 'echo "$(find "$1" -maxdepth 1 -type f | wc -l) $1"' _ {} \; 2>/dev/null | sort -rn | head -10

Read the first column: it is the number of files in the directory. The first lines point to the folder to empty or to rotate (rotation, cache purge).

Find what takes up the space with du

df says how much; du (disk usage) says where. To find the big items of a directory, list the first level and sort:

Terminal window
du -h --max-depth=1 /usr | sort -h
Output (AlmaLinux)
38M /usr/sbin
64M /usr/bin
169M /usr/lib64
241M /usr/share
505M /usr/lib
1022M /usr

The sort -h sort (human-numeric) puts the biggest at the bottom, immediately visible. For the total of a folder alone, du -sh /var. Going down from directory to directory, you trace back to the source of the growth.

Start with the overall breakdown

On a machine you are discovering, start with a command whose output fits in about ten lines and points to the branch to explore. sudo avoids missing the directories your account cannot read, and 2>/dev/null keeps the output usable.

Terminal window
sudo du -sh /* 2>/dev/null | sort -rh

To rank the ten biggest items under /, files included, -a adds files and -x stops du from crossing into other mounted file systems. The sort -rh sort understands the G, M and K suffixes, and the first line is always the total of the starting point.

Terminal window
sudo du -ahx / 2>/dev/null | sort -rh | head -10
Output (Linux workstation)
188G /
166G /home
87G /home/bob/Projets
35G /home/bob/.cache
29G /home/bob/Projets/lab

Once you have found the branch, du -sh on its content goes down one level:

Terminal window
sudo du -sh /var/log/* 2>/dev/null | sort -rh | head -5
Output (author's test machine)
633M /var/log/journal
13M /var/log/pods
13M /var/log/crowdsec_api.log
2.5M /var/log/harbor
1.5M /var/log/libvirt

Do not compare the total of a du with the Size of a df taken on another machine: the gap between df and du can only be read on one and the same machine, and it is handled with the deleted files that are still open, further down.

Explore interactively with ncdu

For a faster diagnosis, ncdu (NCurses Disk Usage) combines du and a navigable interface: sorted sizes, arrow-key navigation, deletion with the d key.

Terminal window
sudo apt install ncdu
Terminal window
sudo ncdu /var

Interactive exploration saves precious time on a server under alert: you go down visually to the faulty folder instead of chaining du commands.

Find large files with find

du points to directories, find points to files. The switch is useful when a single file explains the saturation: a forgotten disk image, a database dump, a log that was never rotated. Three criteria cover most searches: size (-size), modification date (-mtime) and last access date (-atime).

Files over 100 MB

-xdev stops find from descending into other mounted file systems, which avoids walking a network share or a backup disk. The sort uses the fifth column of ls -lh, the size column.

Terminal window
sudo find / -xdev -type f -size +100M -exec ls -lh {} \; 2>/dev/null | sort -k5 -rh | head -10

Large files modified recently

Crossing size and freshness answers the question "what is growing right now?". The sign of -mtime reverses the meaning: -7 means the last 7 days, +7 means older than that.

Terminal window
sudo find /var -xdev -type f -size +10M -mtime -7 -exec ls -lh {} \; 2>/dev/null

The command lists files over 10 MB modified within the week under /var, useful to find a log that grows fast.

Old temporary files

-atime relies on the last access date, which says more than the modification date about what is no longer used. That date is not reliable if the file system is mounted with noatime, common on servers to save writes: in that case -atime proves nothing and you have to fall back on -mtime.

Always start with a preview using -print. -xdev keeps find on the file system of /tmp, without following a mount point placed below it.

Terminal window
find /tmp -xdev -type f -atime +30 -print

Read the list. If it only contains files you can do without, run the same command again, replacing -print with -delete. The deletion is irreversible.

The trap of the deleted file still open

A classic and puzzling symptom: you delete a big file, but df still shows the disk full. The cause: a process keeps the file open. As long as it runs, the space is not freed, even though the file has disappeared from the tree. Find these files:

Terminal window
sudo lsof +L1

The last column lists the (deleted) files still open, with the process and its PID. The typical case is an application log deleted without reloading the service that writes it. The kernel keeps the blocks as long as a descriptor remains open on the file: du no longer sees anything, df shows the file system full.

Typical output (nginx, illustrative PID and size)
COMMAND PID USER FD TYPE DEVICE SIZE/OFF NLINK NODE NAME
nginx 1234 www 4w REG 253,0 2.1G 0 131074 /var/log/nginx/access.log (deleted)

Look at the NAME column for the file, PID for the process to deal with, and FD for the descriptor number (here 4), which you will need to empty it. As soon as the last descriptor closes or the file is truncated, df -h reflects the reclaimed space.

Do not pass a mount point as an argument: contrary to what you would expect, it filters nothing. Measured on AlmaLinux 10, lsof +L1 alone returns 9 lines, lsof +L1 /tmp returns 17, and lsof +L1 / returns 1381. The argument selects processes, all of whose entries are then listed, libraries from other file systems included. To narrow down, filter the output:

Terminal window
sudo lsof +L1 | grep '/var/log'

Three ways out, in order of preference:

  1. Empty the file through its descriptor, without stopping the process. It is often the only acceptable option in production:

    Terminal window
    sudo ls -l /proc/<PID>/fd | grep '(deleted)'
    sudo sh -c ': > /proc/<PID>/fd/<FD>'

    The space is given back immediately, the service keeps running and writing to its descriptor.

  2. Cleanly restart the service involved with systemctl restart. The restart interrupts the service, even briefly: check its impact before doing it in production.

  3. Kill the process, as a last resort. Prefer a normal termination signal to kill -9, which gives the service no chance to close its files cleanly.

The deleted file is not the only cause of a disk that fills up for no visible reason. On a machine managed by systemd, the journal also grows continuously, in /var/log/journal, and it is purged with its own commands rather than with an rm that could corrupt it.

Contain the systemd journals

The systemd journals grow over time and can take up several hundred MB. They are measured and purged with journalctl, never with rm. Before purging, check that no ongoing investigation needs these logs: the purge is final.

Terminal window
journalctl --disk-usage
Output (author's test machine)
Archived and active journals take up 632.5M in the file system.

Two commands reduce this size: the first limits the volume, the second the age.

Terminal window
sudo journalctl --vacuum-size=200M # keep at most 200 MB
sudo journalctl --vacuum-time=7d # or keep only the last 7 days

A one-off purge is not enough if the journal grows back. To make the limit persistent, set SystemMaxUse in /etc/systemd/journald.conf, then restart systemd-journald and check with journalctl --disk-usage.

[Journal]
SystemMaxUse=200M
Terminal window
sudo systemctl restart systemd-journald

Understand ext4 reserved blocks

On ext4, 5% of the space is reserved for root by default. This reserve explains the gap between Size, Used and Avail in df, and it lets root keep writing (system logs, critical processes) when a normal user gets No space left on device. The number of reserved blocks is read with tune2fs -l, filtering on reserved:

Terminal window
sudo tune2fs -l /dev/mapper/ubuntu--vg-ubuntu--lv | grep -i reserved
Output (Ubuntu server, LVM volume)
Reserved block count: 10002749
Reserved blocks uid: 0 (user root)
Reserved blocks gid: 0 (group root)

The reserved block count is the first line; the next two say the reserve belongs to root (UID and GID 0).

Reduce the reserve on a data volume

On a pure data volume, which does not hold /, the 5% reserve is often too generous. tune2fs -m sets the reserved percentage:

Terminal window
sudo tune2fs -m 1 /dev/mapper/data-lv

This command brings the reserve down from 5% to 1%. Do not do it on /: system processes need that margin to keep writing when the disk is nearly full.

Free up space in the right order

On a running server, the order of operations matters as much as the commands. You diagnose before deleting, you start with the targets whose loss does not break the system (caches, old logs, orphaned packages), and you only touch application data as a last resort.

  1. Diagnose: space or inodes?

    Terminal window
    df -h /
    df -i /
  2. Locate the directories that consume the most:

    Terminal window
    sudo du -sh /var/* /tmp/* /home/* 2>/dev/null | sort -rh | head -10
  3. Look for deleted files that are still open:

    Terminal window
    sudo lsof +L1
  4. Clean the targets whose loss does not break the system:

    Terminal window
    # Old journals
    sudo journalctl --vacuum-time=7d
    # Package cache
    sudo apt clean # Debian/Ubuntu
    sudo dnf clean all # RHEL/Fedora
    # Old temporary files: preview first, delete afterwards
    find /tmp -xdev -type f -atime +30 -print
    sudo find /tmp -xdev -type f -atime +30 -delete
    # Orphaned packages and old kernels
    sudo apt autoremove --purge # Debian/Ubuntu
    sudo dnf autoremove # RHEL/Fedora
  5. Check the result:

    Terminal window
    df -h /

On Debian and Ubuntu, apt autoremove --purge removes orphaned packages, with their configuration files, and the old kernels that are no longer needed. On RHEL, dnf autoremove removes orphaned dependencies, and the number of kernels kept is set with installonly_limit in /etc/dnf/dnf.conf (the dnf guide gives the example installonly_limit=3). The details are in the dnf guide and the apt guide.

For an application log in /var/log that a service is still writing, do not delete it, or you fall back into the trap of the deleted file still open. Empty it instead with truncate -s 0 /var/log/file.log, which frees the space without touching the service's descriptor.

Reclaim space for good

When cleaning is no longer enough, you have to grow the volume. The approach depends on the underlying layer, detailed in the dedicated guides:

  • LVM volume: lvextend -r grows volume and file system online (see LVM).
  • ext4 partition: grow the partition then resize2fs.
  • XFS partition: grow then xfs_growfs (an XFS is not shrunk in practice: shrinking is still experimental).

To limit consumption per user or group upstream, set up disk quotas, covered in the quotas guide.

Troubleshooting

A full disk almost always has one of the causes below, and each one is confirmed by a command before any deletion. Start with df -h and df -i, which already tell a lack of space from a lack of inodes.

SymptomLikely causeFix
Disk "full" but df -h shows spaceinodes exhausteddf -i, delete the small files involved
Space not freed after deletiondeleted file still openlsof +L1, then empty via /proc/<PID>/fd/<FD> or restart the service
lsof +L1 <mount-point> returns more lines than lsof +L1 aloneThe argument selects processes, not a file systemFilter the output with grep, the argument narrows nothing
Service restarted, space still not given backThe descriptor is inherited by child processeslsof +L1 lists parent and children with the same inode: killing the parent alone is not enough
df says 100% but root still writesext4 reserve of 5% kept for roottune2fs -l <dev> | grep -i reserved; tune2fs -m 1 to lower it
du -sh clearly underestimates the space usedUnreadable directories, errors swallowedRun again with sudo; without it, the Permission denied go by silently
/ fills up for no visible reasonsystemd journals or application logsjournalctl --vacuum-size, check /var/log
/boot full after updatesold kernels piled upapt autoremove --purge (Debian/Ubuntu); dnf autoremove and installonly_limit in /etc/dnf/dnf.conf (RHEL)
The alert script does not detect the saturationwrong mount point checkedcheck /var, /tmp and /home separately with df -h <path>
Avail smaller than Size - Usedblocks reserved for root (5% by default on ext4)normal; lower with tune2fs -m only on a data volume
/var fills up very fastlogs not rotated or systemd journal without a limitconfigure logrotate and SystemMaxUse in journald.conf
Partition full after container buildsimages and layers not cleaned updocker system prune or podman system prune, see Docker logs and disk space

Put it into practice

Facing a real full disk, theory is no longer enough: you must find the culprit fast and without breaking legitimate data. The lab confronts you with a saturated file system that you diagnose with df and du, before deleting the stray files and bringing usage back under the alert threshold.

Key points

  • df -h shows space; df -i shows the inodes (a disk can be "full" without lacking space).
  • du -h --max-depth=1 | sort -h finds the big directories; ncdu explores interactively.
  • A deleted file still open does not free space: lsof +L1, then empty it via /proc/<PID>/fd/<FD> or restart the service.
  • The systemd journals are purged with journalctl --vacuum-size / --vacuum-time, and SystemMaxUse in journald.conf makes the limit persistent.
  • ext4 reserved blocks (5% by default) explain the gap between Size, Used and Avail; tune2fs -m lowers them on a data volume, never on /.
  • find /tmp -xdev -atime +30 is tried with -print before -delete, and atime is not reliable with noatime.
  • You free up space in this order: diagnosis, location, deleted files still open, then the targets whose loss does not break the system.
  • To grow for good: lvextend -r (LVM), resize2fs (ext4) or xfs_growfs (XFS).
  • Quotas limit consumption per user upstream.

Knowledge check

Check that the essentials of this guide are acquired. The questions only cover what has just been explained here.

Knowledge check

Check what you have learned with this interactive quiz

10 questions
10 min
70% to pass

How it works

  • The timer starts when you click Start
  • Multiple choice, true/false and short answer questions
  • You can move between questions
  • Detailed results are shown at the end

Starts the quiz and the timer