Chapter 7 · Lesson 11 of 11
Linux for servers

Disk usage and mounts on Linux (df, du, lsblk, fstab)

df shows free space per filesystem, du shows what uses it, lsblk and blkid identify disks, and /etc/fstab tells Linux which filesystems to mount at boot, where a bad line can stop the server booting.

By the end you will

Find what uses disk space and inodes, identify a disk by UUID, and add a mount to fstab with a backup and a test that avoids a failed boot.

Time
30 min
Level
Intermediate
Check
4 questions
You need
  • An Ubuntu or Debian server you can reach over SSH
  • A user with sudo access
  • Console or rescue access from your hosting provider, in case a mount breaks boot

Two disk problems hit servers most: running out of space, and breaking boot with a bad mount. The first is fixed by measuring correctly. The second is fixed by a short, careful routine before you ever touch /etc/fstab. Use this together with check disk, memory and CPU usage and fix a full disk on a Linux server.

How it works#

Linux presents one tree, and each disk or partition becomes part of it by being mounted at a directory (a mount point). A block device such as /dev/sda1 holds a filesystem such as ext4. A mount connects that filesystem to a path such as /srv/data.

At boot, systemd reads /etc/fstab and mounts what it lists. A mount that fails at boot can stop the whole boot into emergency mode, which on a remote server means you need the provider’s console to recover. That is why fstab changes need care.

df asks the filesystem how much is used. du walks the files and adds them up. The two answers can differ, and each tells you something different.

Step 1: See free space and inodes#

bash
df -h
df -h /var
df -i
df -hT

df -h prints human-readable size, used, available, use percentage and mount point. Ignore tmpfs and udev rows when you look for disk problems. -T adds the filesystem type. df -i shows inodes. Every file takes one inode, and when IUse% hits 100% you cannot create files even though df -h shows free bytes. Linux also reserves some space on ext4 for root, so usable space for normal users can be slightly less than the free total.

Step 2: Find what is using the space#

bash
sudo du -h --max-depth=1 /var | sort -h
sudo du -xh -d 1 / | sort -h | tail -n 10
sudo du -sh /var/log/* | sort -h | tail

du -sh prints one total per argument. --max-depth=1 (or -d 1) shows one level of subfolders. -x stays on one filesystem so it does not walk into other mounts. Sort with sort -h to order human sizes. Start at /, look at the biggest folder, then repeat inside it. Common culprits are /var/log, /var/lib/docker and /var/cache. Interactive ncdu (sudo apt install ncdu) does the same with a browsable view.

If df says the disk is full but du adds up to much less, a deleted file is probably still open:

bash
sudo lsof +L1

Restart the process that holds it and the space returns.

Step 3: Identify disks and mounts#

bash
lsblk
lsblk -f
sudo blkid
findmnt
findmnt /srv/data
mount | grep ext4

lsblk draws the tree of disks and partitions with size, type and mount point. lsblk -f adds filesystem type, label and UUID. blkid prints the UUID of each device and needs sudo. findmnt shows the mount tree, and findmnt PATH tells you which device and options back a path, which is the cleanest way to check where a folder really lives. mount with no arguments lists everything, but is noisy.

A new, empty disk shows in lsblk with no filesystem. Creating one with mkfs erases the disk, so confirm the device name twice first.

Step 4: Mount a disk by hand#

bash
sudo mkdir -p /srv/data
sudo mount /dev/sdb1 /srv/data
findmnt /srv/data
sudo umount /srv/data

Mount by hand first to confirm the device and filesystem work. This mount lasts until reboot. umount fails with target is busy while a process uses it, so check with lsof +f -- /srv/data. If the mount point already contained files, they are hidden while the disk is mounted.

Step 5: Make it permanent in fstab#

Warning: a wrong line in /etc/fstab can stop the server booting, and on a remote host that means console recovery. Back up the file, edit it carefully, and test with sudo mount -a and findmnt --verify before you ever reboot. Keep your SSH session open until the test passes.

Back up and find the UUID:

bash
sudo cp -a /etc/fstab /etc/fstab.bak
sudo blkid /dev/sdb1

Output looks like /dev/sdb1: UUID="3f6a1b22-8e1c-4c8f-9a52-0d7e1c0b5a11" TYPE="ext4". Edit with sudoedit /etc/fstab, as shown in read and edit files on Linux, and add:

output
UUID=3f6a1b22-8e1c-4c8f-9a52-0d7e1c0b5a11  /srv/data  ext4  defaults,nofail  0  2

The six fields are:

  1. Device: UUID=..., which stays stable, unlike /dev/sdb1.
  2. Mount point, which must exist.
  3. Filesystem type, such as ext4 or xfs.
  4. Options: defaults is the usual set, nofail lets boot continue if the device is missing, noatime reduces writes, ro mounts read-only.
  5. Dump flag, normally 0.
  6. fsck order: 1 for the root filesystem, 2 for others, 0 to skip checks.

Now test, with the session still open:

bash
sudo mount -a
findmnt --verify
findmnt /srv/data

mount -a mounts everything listed that is not already mounted, and prints any error right away. findmnt --verify checks syntax, and missing devices or mount points. Only reboot after both pass. If anything fails, restore the backup: sudo cp -a /etc/fstab.bak /etc/fstab.

Check it worked#

bash
df -h /srv/data
findmnt --verify
findmnt /srv/data

df should show the new disk size at the mount point, and findmnt should show the UUID’s device with your options. After a planned reboot, run findmnt /srv/data again to confirm it came back. Persistent application data on such a disk, such as Docker volumes, is covered in Docker volumes and persistent data.

Edge cases and gotchas#

  • A missing mount point is not fatal with nofail, but the folder looks empty. An app may then write into the underlying folder on the root disk and fill it. Check findmnt before starting apps.
  • Network mounts need _netdev. NFS and similar mounts must wait for the network, or boot may stall.
  • Cloned disks share UUIDs. Two filesystems with the same UUID confuse mounting. Change one with tune2fs -U random on ext4, while unmounted.
  • Resize before you fill. resize2fs grows an ext4 filesystem after you enlarge its partition. Take a snapshot first.
  • Swap lives in fstab too. A swap file line follows the same rules, see add swap space on Ubuntu.
  • Do not move system folders casually. /var or /home on a new disk needs a careful copy with rsync -aHAX, not just a mount.

Troubleshooting#

SymptomLikely causeFix
No space left on device with free bytes in df -hInodes exhausteddf -i, then delete the many small files
df -h shows 100% but du is smallDeleted file still opensudo lsof +L1, restart that service
mount: /srv/data: special device UUID=... does not exist.Wrong UUID or typoCompare with sudo blkid and fix the line
mount: /srv/data: mount point does not exist.Folder not createdsudo mkdir -p /srv/data
mount: wrong fs type, bad option, bad superblock on /dev/sdb1Wrong type or no filesystemCheck lsblk -f; do not run mkfs if data exists
umount: /srv/data: target is busy.A process uses itlsof +f -- /srv/data, stop the process
Boot ends in Welcome to emergency mode!Bad fstab line without nofailUse the console, restore /etc/fstab.bak, reboot
findmnt: /etc/fstab: parse error at line 5Wrong number of fieldsFix line 5 so it has six fields

Quick reference#

bash
df -h; df -i                          # space and inodes
sudo du -xh -d 1 /var | sort -h       # biggest folders
lsblk -f                              # disks, types, UUIDs
sudo blkid                            # UUIDs
findmnt /srv/data                     # what backs a path
sudo cp -a /etc/fstab /etc/fstab.bak  # backup first
# UUID=...  /srv/data  ext4  defaults,nofail  0  2
sudo mount -a && findmnt --verify     # test before reboot

For storage choices in a container setup, see self-host a static site with Docker.

Next#

With the base system covered, put it to use by serving a site in nginx from scratch.

Check what you learned4 questions
1A write fails with No space left on device, yet df -h shows free space. What should you check next?
2Why should /etc/fstab entries use UUID instead of /dev/sdb1?
3What should you run after editing /etc/fstab, before you reboot?
4What does the nofail option do in an fstab line?

Frequently asked questions

How do I check disk space on Linux?

Run df -h to see size, used and available space for each mounted filesystem in human-readable units. Then run sudo du -h --max-depth=1 /var to see which folders take the space.

Why is my disk full when du shows less than df?

A deleted file can still be held open by a running process, so df counts it but du cannot see it. Run sudo lsof +L1 to find it, then restart that process.

What are inodes and what does df -i show?

An inode stores one file's metadata. A filesystem has a fixed number of them, so millions of tiny files can exhaust inodes while df -h still shows free space. df -i shows inode use.

How do I mount a disk permanently on Linux?

Add a line to /etc/fstab using the disk's UUID from blkid, test it with sudo mount -a before rebooting, and keep a backup of the original file.

Sources

View MD Edit
Share
Helpful?
Progress is saved in this browser only.

Keep going: guides and tutorials

View MD Edit
Share
Helpful?
Run this on your own server

Levelrail deploys apps from git with health-checked releases and one-click rollback.

docker pull ghcr.io/glincker/levelrail:latest Install Levelrail From GLINR Studios