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/usbIf 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 unmountConfigure 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 2Apply the above without rebooting your Linux box:
sudo mount -a # Mount all filesystems in fstabCommon options:
defaults→ rw, suid, dev, exec, auto, nouser, asyncnoexec→ don’t allow binaries to runnosuid→ ignore set-user-ID bitsnodev→ 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 0The 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/usbUse 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 blkidExample 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 2You can also mount it via label:
LABEL=acoolusb /media/usb vfat defaults 0 0Awareness 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.targetTo enable this systemd mount unit you can run the below two commands:
sudo systemctl daemon-reload
sudo systemctl enable --now data.mountThis 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
blkidThe partition exists, UUID matches.
Run integrity check:
sudo fsck -y /dev/sdb1Fixed some errors.
Manually test mount:
sudo mount /dev/sdb1 /mnt/testWorks.
Fix /etc/fstab entry:
UUID=0452-CBCD /data ext4 defaults 0 2Test without reboot:
sudo mount -aIf still failing under systemd:
systemctl status data.mount
journalctl -xe
