Mounting and fstab: Make Filesystems Available

A filesystem on a disk is useless until it’s mounted — attached to a directory in your filesystem tree where you can read and write it. mount does this manually. /etc/fstab makes it automatic at every boot.

The mount command

mount                            # show all currently mounted filesystems
mount -t ext4                    # filter by type
findmnt                          # nicer output
findmnt /                        # info about / mount

Mount a filesystem

# By device path
sudo mount /dev/sdb1 /mnt/data

# By UUID (more reliable)
sudo mount UUID=abc-123-def /mnt/data

# By label
sudo mount -L mydata /mnt/data

# With options
sudo mount -o ro /dev/sdb1 /mnt/data         # read-only
sudo mount -o noatime /dev/sdb1 /mnt/data    # don't update access times
sudo mount -t nfs server:/share /mnt/share   # NFS mount

Unmount

sudo umount /mnt/data            # by mount point (preferred)
sudo umount /dev/sdb1            # by device

# "device is busy" — find what's using it
sudo lsof /mnt/data
sudo fuser -v /mnt/data
sudo umount -l /mnt/data         # lazy unmount (when ready)

/etc/fstab: the mount config

Each line in fstab tells the system how to mount one filesystem at boot.

#                             
UUID=abc-123-def     /             ext4      defaults             0      1
UUID=xyz-456-aaa     /home         ext4      defaults,noatime     0      2
UUID=swp-789         none          swap      sw                   0      0
LABEL=data           /data         xfs       defaults             0      2
//server/share       /mnt/cifs     cifs      credentials=/etc/smb 0      0
tmpfs                /tmp          tmpfs     size=2G,nodev        0      0

Each column explained

  1. device — UUID=, LABEL=, /dev/path, or remote path. UUIDs are most reliable.
  2. mountpoint — where to attach it. Must already exist.
  3. fstype — ext4, xfs, btrfs, vfat, nfs, cifs, swap, tmpfs, etc.
  4. options — comma-separated. defaults = rw,suid,dev,exec,auto,nouser,async.
  5. dump — backup flag for the (rarely used) dump tool. Use 0.
  6. pass — fsck order at boot. 1 for root, 2 for other ext-style filesystems, 0 for swap/network/tmpfs.

Common options

defaults    rw,suid,dev,exec,auto,nouser,async
ro          read-only
rw          read-write (default)
noatime     don't update access timestamps (faster)
nodev       no device files
nosuid      no setuid bits honored (security)
noexec      cannot execute binaries
user        any user can mount/unmount
nofail      don't fail boot if device missing (useful for removable)
x-systemd.automount    mount on first access (lazy mount)
discard     send TRIM to SSDs on delete

Add a new entry safely

# 1. Get the UUID
blkid /dev/sdc1
# /dev/sdc1: UUID="abc-123-def" TYPE="ext4"

# 2. Make the mount point
sudo mkdir -p /data

# 3. Add to fstab
echo "UUID=abc-123-def  /data  ext4  defaults,noatime  0  2" | sudo tee -a /etc/fstab

# 4. Test WITHOUT reboot
sudo mount -a                    # mount everything in fstab; reports errors
df -h /data
findmnt /data

Always run mount -a after editing fstab, BEFORE rebooting. If there’s a syntax error, you find out now instead of when your system fails to boot.

Special filesystems

Swap

# Create a swap file (no need for a partition)
sudo dd if=/dev/zero of=/swapfile bs=1M count=2048
sudo chmod 600 /swapfile
sudo mkswap /swapfile
sudo swapon /swapfile

# Make permanent
echo '/swapfile none swap sw 0 0' | sudo tee -a /etc/fstab

# Check swap
free -h
swapon --show

tmpfs (RAM-backed)

# Mount /tmp in RAM
sudo mount -t tmpfs -o size=2G tmpfs /tmp

# In fstab
tmpfs /tmp tmpfs size=2G,nodev,nosuid 0 0

Bind mount (one path, two locations)

sudo mount --bind /var/data /home/alice/data

# In fstab
/var/data /home/alice/data none bind 0 0

NFS (network share)

sudo apt install nfs-common
sudo mount -t nfs fileserver:/share /mnt/share

# In fstab
fileserver:/share /mnt/share nfs defaults 0 0

Recover from a broken fstab

If your system won’t boot because of a fstab typo:

  1. Boot to recovery / single-user mode (use kernel parameter single or systemd.unit=rescue.target).
  2. Remount root as read-write: mount -o remount,rw /
  3. Edit fstab: vi /etc/fstab
  4. Reboot.

Add nofail to fstab entries for non-essential disks so a missing disk doesn’t block boot.

Common mistakes

  • Using device paths (/dev/sdb1) in fstab — they can change order. Always UUID.
  • Wrong pass (column 6) on root — should be 1; everything else 2 or 0.
  • Forgetting mount -a after editing fstab.
  • Not adding nofail for cloud disks that might not always be attached.

What to learn next

LVM — Linux’s logical volume manager — lets you resize, snapshot, and span filesystems across disks. Up next.

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *