Skip to content

17. Filesystems

While we covered how to mount a disk previously, understanding what filesystem you are mounting is just as important. The filesystem dictates how data is physically laid out, indexed, cached, and recovered after an unexpected crash.


Choosing the right filesystem depends heavily on your workload (e.g., standard web servers vs. massive database clusters).

The default, battle-tested standard for Debian and Ubuntu distributions.

  • Pros: Incredibly stable, mature, and excellent backward compatibility.
  • Cons: Inode capacity is fixed at creation time; struggles with multi-petabyte scalability compared to newer filesystems.

A highly scalable, 64-bit journaling filesystem. It is the default for RHEL, CentOS, Rocky Linux, and AlmaLinux.

  • Pros: Excels at handling large files, high concurrent I/O, and dynamic inode allocation (perfect for databases).
  • Cons: You cannot shrink an XFS filesystem (you can only grow it).

A modern, advanced copy-on-write (CoW) filesystem designed for fault tolerance and easy administration.

  • Pros: Built-in volume management, software RAID, and instantaneous snapshots.
  • Cons: Higher CPU/memory overhead; historically had stability issues with some RAID 5/6 configurations.

A special memory-backed filesystem that lives entirely in RAM.

  • Pros: Blazing fast read/write speeds with zero disk latency.
  • Cons: Volatile (all data is completely wiped when the server reboots). Often used for /tmp, /run, or caching tiers.
  • FAT / exFAT: Universally supported across Linux, Windows, and macOS. Used heavily for USB flash media, but lacks native Linux file permissions and ownership.
  • NTFS: The native Windows filesystem. Linux can read and write to NTFS (via the ntfs-3g driver), but it does not support native Linux POSIX permissions.

Before mounting, a raw partition must be formatted with a filesystem structure. If a system crashes, the filesystem might need to be checked for integrity.

The mkfs (Make Filesystem) command acts as a frontend for distribution-specific filesystem builders:

Terminal window
# Format a partition as ext4
sudo mkfs.ext4 /dev/sdb1
# Format a partition as XFS
sudo mkfs.xfs /dev/sdc1

If a server loses power unexpectedly, the filesystem metadata can become corrupted. Linux normally runs fsck automatically during boot if it detects an unclean shutdown flag.

[!CAUTION] NEVER run fsck on a mounted filesystem! Checking or repairing a live filesystem will cause severe metadata corruption and permanent data loss. Always unmount the volume first.

Terminal window
# Unmount the partition first
sudo umount /dev/sdb1
# Check and repair an ext4 filesystem (-y automatically fixes errors)
sudo fsck.ext4 -y /dev/sdb1

As covered in the Storage chapter, /etc/fstab makes mounts survive reboots. However, there are advanced mount options you can pass to tune security and performance.

Device paths like /dev/sdb1 are not guaranteed to remain static across reboots or disk reconnections. Always query the block UUID using blkid and reference it in /etc/fstab:

# /etc/fstab entry format:
# <Device/UUID> <Mount Point> <Type> <Options> <Dump> <Pass>
UUID=a1b2c3d4-e5f6-7890-1234-56789abcdef0 /var/lib/mysql xfs defaults 0 2

The <Options> column in /etc/fstab controls filesystem behavior:

  • defaults: The standard set (implies rw, suid, dev, exec, auto, nouser, and async).
  • ro: Mounts the filesystem as Read-Only.
  • noexec: Prevents binaries and scripts from executing on this filesystem (strongly recommended for /tmp and /var/log to mitigate malware execution).
  • nosuid: Disables SUID and SGID privilege escalation bits on this volume.
  • nodev: Prevents the interpretation of character or block device files.
  • noatime: Disables updating the “Access Time” timestamp every time a file is read. This dramatically reduces disk write I/O and extends SSD longevity.
Terminal window
# Example of manually mounting a disk as read-only and no-execute
sudo mount -o ro,noexec,noatime /dev/sdb1 /mnt/secure_data

An inode (index node) is a fundamental data structure in Linux filesystems (ext4, XFS) that represents a filesystem object (file, directory, socket, named pipe).

  • File ownership: User ID (UID) and Group ID (GID)
  • Permissions: Read, Write, Execute mode bits and ACL references
  • Timestamps: Access time (atime), Modification time (mtime), Change time (ctime), Creation/Birth time (crtime)
  • File size: Size in bytes
  • Disk block pointers: Direct and indirect pointers mapping physical disk sectors holding file content
  • Link count: Number of hard links pointing to this inode

[!IMPORTANT] What an Inode Does NOT Store: An inode does not store the filename or the actual file content. Filenames are stored inside directory blocks as key-value pairs (Directory Entry: filename → inode number).


Terminal window
# Check inode capacity and usage across all mounted filesystems
df -i
# View the inode number of a specific file
ls -i filename.txt
# View inode information of a directory itself
ls -idl /var/log/
# View detailed inode metadata (size, blocks, links, timestamps)
stat /etc/passwd

Example df -i output:

Filesystem Inodes IUsed IFree IUse% Mounted on
/dev/sda1 1310720 452100 858620 35% /
/dev/sdb1 655360 655360 0 100% /var/spool/postfix

Inode Exhaustion: The “No Space Left on Device” Myth

Section titled “Inode Exhaustion: The “No Space Left on Device” Myth”

Each file or directory consumes exactly one inode. If a filesystem creates millions of tiny files (such as PHP session caches, mail queues, or unrotated application logs), it will run out of available inodes even when multiple gigabytes of disk space remain free.

  • Error message: No space left on device when trying to save files or write logs, even though df -h shows plenty of free capacity.
  • Applications crash unexpectedly, database transactions roll back, or cron jobs fail to run.
  • df -i reports 100% IUse%.

Identifying Culprit Directories Consuming Inodes

Section titled “Identifying Culprit Directories Consuming Inodes”

Run a recursive find pipeline to count files per top-level directory without crossing filesystem boundaries:

Terminal window
# Find directories containing the largest number of files
sudo find /var -xdev -type f | cut -d/ -f2-3 | sort | uniq -c | sort -nr | head -n 10
  • Remove expired session files and stale temporary data in /tmp and /var/tmp.
  • Flush unneeded mail queues: postsuper -d ALL (Postfix).
  • Purge rotated log files older than retention policy: sudo find /var/log -type f -name "*.gz" -mtime +30 -delete.
  • Remove orphaned packages and cached package archives (apt clean or dnf clean all).

On ext2/3/4 filesystems, the total number of inodes is fixed at format time based on the inode density (bytes-per-inode ratio):

Terminal window
# Create ext4 with higher inode density (1 inode per 8192 bytes - ideal for mail/web servers)
sudo mkfs.ext4 -i 8192 /dev/sdb1
# Explicitly set the total number of inodes using -N
sudo mkfs.ext4 -N 2000000 /dev/sdb1
  • Lower bytes-per-inode (-i): Creates more inodes (for systems hosting millions of tiny files).
  • Higher bytes-per-inode (-i): Creates fewer inodes (saving metadata overhead for large video/database storage).
  • ext4: The inode count cannot be increased on an existing ext4 filesystem. You must back up data, reformat with mkfs.ext4 -i, and restore.
  • XFS: Allocates inodes dynamically on demand. Growing an XFS volume automatically expands inode capacity:
    Terminal window
    sudo xfs_growfs /mount/point

Command Operational Purpose
df -i Display total, used, and free inodes per filesystem
ls -i filename Display the inode number mapped to a file
ls -idl directory/ Display the inode number and metadata of a directory
stat filename Inspect comprehensive inode metadata (timestamps, links, block counts)
mkfs.ext4 -i <bytes> Tune inode density (bytes per inode) during ext4 filesystem creation
xfs_growfs /mount/point Expand XFS filesystem size and dynamic inode headroom