Controlling mounting and unmounting filesystems in Linux is one of the things I hardly do when working in Linux server specifically. So, I decided to do some research while having a nice cup of specialty coffee from Peru.

Here are a few examples of mounting and unmounting of filesystems in Linux:

Manually mount and unmount filesystems

When you want to access a partition, USB drive, or ISO image temporarily:

# Create a mount point
sudo mkdir /mnt/usb
# Mount a device (e.g., USB drive)
sudo mount /dev/sdb1 /mnt/usb
# Verify mounted filesystems
mount | grep sdb1
df -h
# Unmount when done
sudo umount /mnt/usb

If the filesystem is busy (e.g., terminal is inside /mnt/usb), umount will fail. If so, run:

sudo umount -l /mnt/usb   # Lazy unmount
sudo umount -f /mnt/usb   # Force unmount

Configure filesystem mounting on bootup (/etc/fstab)

Permanent mounts are configured in /etc/fstab. Example entry:

# <device>     <mountpoint>   <fstype>  <options>         <dump> <pass>
/dev/sda1      /              ext4      defaults          0      1
/dev/sdb1      /data          ext4      defaults          0      2

Apply the above without rebooting your Linux box:

sudo mount -a   # Mount all filesystems in fstab

Common options:

  • defaults → rw, suid, dev, exec, auto, nouser, async
  • noexec → don’t allow binaries to run
  • nosuid → ignore set-user-ID bits
  • nodev → don’t interpret device files

Configure user mountable removable filesystems

Allow normal users to mount USB sticks/CDs by adding user or users option in /etc/fstab this way:

/dev/sdb1   /media/usb   vfat   rw,user,noauto   0   0

The options above are:

  • user → only the user who mounted can unmount.
  • users → any user can unmount.
  • noauto → won’t mount automatically at boot, only when requested.

The a user can mount without sudo:

mount /media/usb
umount /media/usb

Use of labels and UUIDs for identifying and mounting file systems

Using /dev/sdX is risky because device names can change. Use UUIDs or labels if possible. For example:

Find UUID for a device run the command:

sudo blkid

Example output:

/dev/sda1: UUID="1234-ABCD" TYPE="ext4" PARTLABEL="root"

Mount via UUID in /etc/fstab. You will need to add the following line in the file:

UUID=1234-ABCD /data ext4 defaults 0 2

You can also mount it via label:

LABEL=acoolusb   /media/usb   vfat   defaults   0   0

Awareness of systemd mount units

Modern Linux uses systemd which can manage mounts via mount units.
A file like /etc/systemd/system/data.mount can represent a mount point.

Example could be a file in /etc/systemd/system/data.mount:

[Unit]
Description=Mount data partition
[Mount]
What=/dev/sdb1
Where=/data
Type=ext4
Options=defaults
[Install]
WantedBy=multi-user.target

To enable this systemd mount unit you can run the below two commands:

sudo systemctl daemon-reload
sudo systemctl enable --now data.mount

This integrates mounts into systemctl workflow.

Some useful Mounting and unmounting filesystems troubleshooting

You try to reboot and /data fails to mount.

You check with:

lsblk
blkid

The partition exists, UUID matches.

Run integrity check:

sudo fsck -y /dev/sdb1

Fixed some errors.

Manually test mount:

sudo mount /dev/sdb1 /mnt/test

Works.

Fix /etc/fstab entry:

UUID=0452-CBCD   /data   ext4   defaults   0  2

Test without reboot:

sudo mount -a

If still failing under systemd:

systemctl status data.mount
journalctl -xe

Leave a comment

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