Linux Mounting and Unmounting Drives - Complete Filesystem Guide

Filesystem mounting is a fundamental operation in Linux that makes storage devices accessible to the system. This comprehensive guide covers everything from basic mount operations to advanced automount configurations. Master the art of managing filesystems, understanding mount options, and troubleshooting common mounting issues.

Mounting Tools Comparison

Tool Category Primary Tools Purpose Skill Level
Basic Mounting mount, umount Manual filesystem mounting Beginner
Automatic Mounting /etc/fstab, systemd Persistent filesystem configuration Intermediate
Network Filesystems mount.nfs, mount.cifs Remote filesystem mounting Intermediate
Advanced Mounting autofs, systemd.mount On-demand automounting Advanced
Monitoring findmnt, mount, df Mount point monitoring Intermediate
Troubleshooting dmesg, journalctl, strace Mount issue diagnosis Advanced
Quick Reference:
• List mounts: mount or findmnt
• Mount filesystem: mount /dev/sdb1 /mnt/data
• Unmount filesystem: umount /mnt/data
• Check disk space: df -h
• Persistent mounts: edit /etc/fstab
• Test fstab: mount -a
• Force unmount: umount -f (use with caution)
• Lazy unmount: umount -l
• Always verify mount points exist before mounting
• Use mount --bind for directory trees

Mounting Process Workflow

Filesystem Mounting Process

Device Detection
Filesystem Check
Mount Point Creation
Mount Operation
Verification
fstab Configuration

Essential Mounting Tools

Tool Command Examples Purpose Key Output
mount mount /dev/sdb1 /mnt
mount -o ro /dev/sdb1 /mnt
Mount filesystems Mount success/failure
umount umount /mnt
umount -l /mnt
Unmount filesystems Unmount success/failure
findmnt findmnt
findmnt /mnt
List mounted filesystems Mount hierarchy, options
df df -h
df -i
Disk space usage Used/available space
blkid blkid
blkid /dev/sdb1
Block device information UUID, filesystem type
lsblk lsblk
lsblk -f
Block device hierarchy Device tree, mount points

Basic Mount Operations

/dev/sdb1
/mnt/data
mount - Filesystem Mounting

Mount filesystems and make them accessible to the system.

mount [options] device mountpoint

Basic Mount Operations:

# Mount filesystem by device
sudo mount /dev/sdb1 /mnt/data

# Mount filesystem by UUID
sudo mount UUID=1234-5678-9ABC /mnt/data

# Mount filesystem by label
sudo mount LABEL="DataDisk" /mnt/data

# Mount with specific filesystem type
sudo mount -t ext4 /dev/sdb1 /mnt/data
sudo mount -t xfs /dev/sdb1 /mnt/data
sudo mount -t ntfs-3g /dev/sdb1 /mnt/data

# Mount read-only
sudo mount -o ro /dev/sdb1 /mnt/data

# Mount with multiple options
sudo mount -o rw,noatime,nodiratime /dev/sdb1 /mnt/data

# Mount and show verbose output
sudo mount -v /dev/sdb1 /mnt/data

# Mount all filesystems in fstab
sudo mount -a
umount - Filesystem Unmounting

Safely unmount filesystems and detach them from the system.

umount [options] device|mountpoint

Unmount Operations:

# Unmount by mount point
sudo umount /mnt/data

# Unmount by device
sudo umount /dev/sdb1

# Force unmount (use with caution)
sudo umount -f /mnt/data

# Lazy unmount (detach immediately, cleanup later)
sudo umount -l /mnt/data

# Unmount with verbose output
sudo umount -v /mnt/data

# Unmount all filesystems of specific type
sudo umount -a -t nfs

# Check why unmount is failing
sudo lsof /mnt/data
sudo fuser -v /mnt/data

# Unmount busy filesystem after checking processes
sudo fuser -km /mnt/data  # Kill processes using mount
sudo umount /mnt/data
/etc/fstab - Persistent Mounts

Configure filesystems to mount automatically at boot.

/etc/fstab configuration file

fstab Configuration:

# /etc/fstab structure:
# device mountpoint fstype options dump fsck

# Mount by device (not recommended)
/dev/sdb1   /mnt/data   ext4    defaults    0   2

# Mount by UUID (recommended)
UUID=1234-5678-9ABC   /mnt/data   ext4    defaults    0   2

# Mount by label
LABEL=DataDisk   /mnt/data   ext4    defaults,noatime    0   2

# NFS mount
nfs-server:/export/data   /mnt/nfs   nfs    rw,hard,intr    0   0

# CIFS/SMB mount
//server/share   /mnt/smb   cifs    credentials=/root/smb.cred,uid=1000    0   0

# tmpfs mount
tmpfs   /mnt/tmpfs   tmpfs   size=1G,mode=1777    0   0

# Bind mount (directory to directory)
/mnt/data   /srv/data   none    bind    0   0

# Test fstab without mounting
sudo mount -a --fake
sudo findmnt --verify

Mount Options Reference

Option Description Use Case Example
rw/ro Read-write or read-only mount Data protection, backup mount -o ro /dev/sdb1 /mnt
noatime Don't update access times Performance optimization mount -o noatime /dev/sdb1 /mnt
nodiratime Don't update directory access times Performance optimization mount -o nodiratime /dev/sdb1 /mnt
relatime Update access times relative to modify times Balance performance and compatibility mount -o relatime /dev/sdb1 /mnt
sync/async Synchronous or asynchronous I/O Data integrity vs performance mount -o sync /dev/sdb1 /mnt
noexec Prevent execution of binaries Security, data partitions mount -o noexec /dev/sdb1 /mnt
nosuid Ignore setuid bits Security mount -o nosuid /dev/sdb1 /mnt
nodev Don't interpret device files Security mount -o nodev /dev/sdb1 /mnt
defaults Default options: rw,suid,dev,exec,auto,nouser,async General purpose mount -o defaults /dev/sdb1 /mnt

Advanced Mount Operations

Bind Mounts

Mount a directory to another location in the filesystem.

mount --bind olddir newdir

Bind Mount Operations:

# Create bind mount
sudo mount --bind /var/www /srv/www

# Make bind mount persistent in fstab
/var/www   /srv/www   none    bind    0   0

# Recursive bind mount (subdirectories)
sudo mount --rbind /old/dir /new/dir

# Make recursive bind persistent
/old/dir   /new/dir   none    rbind    0   0

# Read-only bind mount
sudo mount --bind -o ro /source /destination

# Unmount bind mount
sudo umount /srv/www

# Show bind mounts
findmnt -R

# Move mount to another location
sudo mount --move /old/mount /new/mount
Network Filesystems

Mount remote filesystems over the network.

mount -t nfs|cifs|sshfs remote local

Network Mount Operations:

# NFS mount
sudo mount -t nfs nfs-server:/export/data /mnt/nfs

# NFS with options
sudo mount -t nfs -o rw,hard,intr,timeo=300 nfs-server:/export /mnt/nfs

# CIFS/SMB mount
sudo mount -t cifs //server/share /mnt/smb -o username=user,password=pass

# CIFS with credentials file
sudo mount -t cifs //server/share /mnt/smb -o credentials=/root/smb.cred

# SSHFS mount (FUSE)
sshfs user@server:/remote/path /mnt/sshfs

# AutoFS for on-demand network mounting
# Configure in /etc/auto.master and /etc/auto.nfs

# Unmount network filesystems
sudo umount /mnt/nfs
sudo umount /mnt/smb
fusermount -u /mnt/sshfs  # For FUSE filesystems
Remount Operations

Change mount options without unmounting.

mount -o remount,options device mountpoint

Remount Operations:

# Remount with new options
sudo mount -o remount,rw /mnt/data

# Remount read-only
sudo mount -o remount,ro /mnt/data

# Remount with additional options
sudo mount -o remount,noatime,nodiratime /mnt/data

# Remount with smaller options set
sudo mount -o remount,defaults /mnt/data

# Remount all filesystems
sudo mount -o remount /

# Change NFS mount options
sudo mount -o remount,soft,intr nfs-server:/export /mnt/nfs

# Emergency remount read-only
sudo mount -o remount,ro /

# Remount with specific filesystem options
sudo mount -o remount,usrquota,grpquota /mnt/data

Filesystem Monitoring & Information

Tool Category Tools Primary Use Installation
Mount Monitoring findmnt, mount, df View mounted filesystems Built-in
Device Information lsblk, blkid, udevadm Block device details Built-in
Space Monitoring df, du, ncdu Disk usage analysis sudo apt install ncdu
Inode Monitoring df -i, stat Inode usage tracking Built-in
Real-time Monitoring inotify-tools, watch Filesystem event monitoring sudo apt install inotify-tools

Monitoring & Information Tools

Mount Point Information

Get detailed information about mounted filesystems.

findmnt, mount, /proc/mounts

Mount Information:

# List all mounted filesystems
findmnt

# Show in tree format
findmnt -t ext4

# Show specific mount point
findmnt /mnt/data

# Show mount information in JSON
findmnt -J

# Show by filesystem type
findmnt -t ext4,xfs,nfs

# Legacy mount listing
mount
cat /proc/mounts

# Show mount options for specific filesystem
findmnt -o TARGET,SOURCE,FSTYPE,OPTIONS

# Monitor mount changes
watch -n 1 findmnt

# Show bind mounts
findmnt -R
Space & Inode Monitoring

Monitor disk space and inode usage.

df, du, ncdu

Space Monitoring:

# Show disk space in human-readable format
df -h

# Show inode usage
df -i

# Show specific filesystem
df -h /mnt/data

# Show filesystem type
df -T

# Show directory sizes
du -sh /mnt/data/*
du -h --max-depth=1 /mnt/data

# Interactive disk usage analysis
ncdu /mnt/data

# Monitor disk space in real-time
watch -n 5 'df -h'

# Show largest files
find /mnt/data -type f -exec du -h {} + | sort -rh | head -20

# Check for filesystem errors
sudo fsck -n /dev/sdb1
Advanced Monitoring

Real-time filesystem monitoring and event tracking.

inotify-tools, watch, lsof

Advanced Monitoring:

# Monitor filesystem events
inotifywait -m -r /mnt/data

# Monitor specific events
inotifywait -m -e create,delete,modify /mnt/data

# Watch mount points for changes
watch -n 1 'mount | grep /mnt/data'

# Check what's using a mount point
lsof /mnt/data
fuser -v /mnt/data

# Monitor I/O statistics
iostat -x 1

# Check filesystem health
sudo smartctl -a /dev/sdb

# Monitor kernel messages for filesystem events
dmesg -T | grep -i mount
journalctl -f | grep -i mount

# Check for stuck processes
ps aux | grep -i defunct

Practical Mounting Scenarios

Real-World Mounting Examples

# 1. Complete Mount Diagnostics Script
#!/bin/bash
echo "=== Mount Diagnostics ==="
echo "1. Current Mounts:"
findmnt
echo -e "\n2. Disk Space:"
df -h
echo -e "\n3. fstab Configuration:"
cat /etc/fstab
echo -e "\n4. Block Devices:"
lsblk -f
echo -e "\n5. Recent Mount Events:"
dmesg | grep -i mount | tail -10

# 2. Automated Mount Setup Script
#!/bin/bash
DEVICE="/dev/sdb1"
MOUNT_POINT="/mnt/data"
FSTYPE="ext4"
OPTIONS="defaults,noatime"

echo "Setting up mount for $DEVICE..."
sudo mkfs.$FSTYPE $DEVICE
sudo mkdir -p $MOUNT_POINT
sudo mount $DEVICE $MOUNT_POINT
echo "$DEVICE $MOUNT_POINT $FSTYPE $OPTIONS 0 2" | sudo tee -a /etc/fstab
echo "Mount setup complete. Testing..."
sudo mount -a
df -h $MOUNT_POINT

# 3. Mount Health Check Script
#!/bin/bash
echo "Mount Health Check:"
while read -r line; do
    if [[ $line == \#* ]] || [[ -z $line ]]; then
        continue
    fi
    mountpoint=$(echo $line | awk '{print $2}')
    if mountpoint -q "$mountpoint"; then
        echo "✓ $mountpoint is mounted"
        # Check if writable
        if touch "$mountpoint/.mount_test" 2>/dev/null; then
            echo "  - Writable"
            rm -f "$mountpoint/.mount_test"
        else
            echo "  - Read-only"
        fi
    else
        echo "✗ $mountpoint is NOT mounted"
    fi
done < /etc/fstab

# 4. Network Mount Manager
#!/bin/bash
NFS_SERVER="nfs.example.com"
NFS_EXPORT="/export/data"
LOCAL_MOUNT="/mnt/nfs"

echo "Managing NFS mount..."
if ping -c 1 $NFS_SERVER &> /dev/null; then
    echo "NFS server is reachable"
    if mountpoint -q $LOCAL_MOUNT; then
        echo "NFS is already mounted"
    else
        echo "Mounting NFS..."
        sudo mount -t nfs $NFS_SERVER:$NFS_EXPORT $LOCAL_MOUNT
    fi
else
    echo "NFS server is unreachable"
    if mountpoint -q $LOCAL_MOUNT; then
        echo "Unmounting stale NFS mount..."
        sudo umount -l $LOCAL_MOUNT
    fi
fi

# 5. USB Auto-mount Script
#!/bin/bash
USB_DEVICE=$(lsblk -r -n -o NAME,MOUNTPOINT | grep -v / | head -1 | cut -d' ' -f1)
if [ -n "$USB_DEVICE" ]; then
    MOUNT_POINT="/mnt/usb_$USB_DEVICE"
    sudo mkdir -p $MOUNT_POINT
    sudo mount /dev/$USB_DEVICE $MOUNT_POINT 2>/dev/null && echo "USB mounted at $MOUNT_POINT" || echo "Failed to mount USB"
else
    echo "No USB device found"
fi

# 6. Mount Option Optimizer
#!/bin/bash
MOUNT_POINT="/mnt/data"
CURRENT_OPTS=$(findmnt -o OPTIONS -n $MOUNT_POINT)
echo "Current options: $CURRENT_OPTS"

# Check if noatime is set
if [[ $CURRENT_OPTS != *noatime* ]]; then
    echo "Optimizing: Adding noatime option..."
    sudo mount -o remount,noatime $MOUNT_POINT
fi

# Check if async is set (for performance)
if [[ $CURRENT_OPTS != *async* ]]; then
    echo "Optimizing: Using async writes..."
    sudo mount -o remount,async $MOUNT_POINT
fi

# 7. Bind Mount Manager
#!/bin/bash
SOURCE_DIR="/var/www"
BIND_DIR="/srv/www"

echo "Managing bind mount..."
if [ ! -d $BIND_DIR ]; then
    sudo mkdir -p $BIND_DIR
fi

if ! findmnt -R | grep -q $BIND_DIR; then
    echo "Creating bind mount..."
    sudo mount --bind $SOURCE_DIR $BIND_DIR
    echo "$SOURCE_DIR $BIND_DIR none bind 0 0" | sudo tee -a /etc/fstab
else
    echo "Bind mount already exists"
fi

# 8. Mount Point Cleanup Script
#!/bin/bash
echo "Cleaning up stale mount points..."
for mount in /mnt/*; do
    if mountpoint -q "$mount"; then
        echo "Active: $mount"
    else
        echo "Stale: $mount"
        # Uncomment to remove stale mount points
        # sudo rmdir "$mount"
    fi
done

# 9. Filesystem Type Checker
#!/bin/bash
echo "Filesystem Type Report:"
for device in /dev/sd*; do
    if [ -b $device ]; then
        fstype=$(lsblk -r -n -o FSTYPE $device 2>/dev/null)
        mountpoint=$(lsblk -r -n -o MOUNTPOINT $device 2>/dev/null)
        echo "$device: $fstype (mounted at: $mountpoint)"
    fi
done

# 10. Emergency Read-only Remount
#!/bin/bash
echo "Emergency remount to read-only..."
CRITICAL_MOUNTS="/ /var /home"

for mount in $CRITICAL_MOUNTS; do
    if mountpoint -q "$mount"; then
        echo "Remounting $mount as read-only..."
        sudo mount -o remount,ro "$mount"
    fi
done

echo "System is now in read-only mode for safety"

# 11. Mount Statistics Collector
#!/bin/bash
LOG_FILE="/var/log/mount-stats.log"
echo "$(date): Mount Statistics" >> $LOG_FILE
echo "=== Mount Points ===" >> $LOG_FILE
findmnt >> $LOG_FILE
echo -e "\n=== Disk Usage ===" >> $LOG_FILE
df -h >> $LOG_FILE
echo -e "\n=== Inode Usage ===" >> $LOG_FILE
df -i >> $LOG_FILE
echo "Statistics saved to $LOG_FILE"

# 12. AutoFS Configuration Generator
#!/bin/bash
AUTOFS_MASTER="/etc/auto.master"
AUTOFS_MAP="/etc/auto.nfs"

echo "Configuring AutoFS..."
echo "/mnt/autofs /etc/auto.nfs --timeout=300" | sudo tee $AUTOFS_MASTER
echo "data -fstype=nfs,rw,hard,intr nfs-server:/export/data" | sudo tee $AUTOFS_MAP
sudo systemctl restart autofs
echo "AutoFS configured. Access mounts at /mnt/autofs/data"

# 13. Mount Point Permission Fixer
#!/bin/bash
MOUNT_POINT="/mnt/data"
DESIRED_UID=1000
DESIRED_GID=1000

echo "Fixing permissions for $MOUNT_POINT..."
sudo chown $DESIRED_UID:$DESIRED_GID $MOUNT_POINT
sudo chmod 755 $MOUNT_POINT
echo "Permissions updated:"
ls -ld $MOUNT_POINT

# 14. LVM Mount Manager
#!/bin/bash
VG_NAME="datavg"
LV_NAME="datalv"
MOUNT_POINT="/mnt/lvm"

echo "Managing LVM mount..."
if [ -e "/dev/mapper/${VG_NAME}-${LV_NAME}" ]; then
    if ! mountpoint -q $MOUNT_POINT; then
        echo "Mounting LVM volume..."
        sudo mount "/dev/mapper/${VG_NAME}-${LV_NAME}" $MOUNT_POINT
    else
        echo "LVM volume already mounted"
    fi
else
    echo "LVM volume not found"
fi

# 15. FUSE Mount Handler
#!/bin/bash
SSHFS_MOUNT="/mnt/sshfs"
REMOTE_HOST="user@example.com"
REMOTE_PATH="/home/user"

echo "Managing SSHFS mount..."
if ping -c 1 example.com &> /dev/null; then
    if ! mountpoint -q $SSHFS_MOUNT; then
        echo "Mounting SSHFS..."
        sshfs $REMOTE_HOST:$REMOTE_PATH $SSHFS_MOUNT
    else
        echo "SSHFS already mounted"
    fi
else
    echo "Remote host unreachable"
    if mountpoint -q $SSHFS_MOUNT; then
        echo "Unmounting SSHFS..."
        fusermount -u $SSHFS_MOUNT
    fi
fi

Common Mounting Issues & Solutions

Mount Failures

  • Device Busy: Use lsof or fuser to find processes, use umount -l for lazy unmount
  • Wrong Filesystem Type: Specify correct type with -t option, check with blkid
  • Invalid Mount Point: Ensure directory exists, check permissions
  • Network Unreachable: Verify network connectivity, check server availability

fstab Issues

  • Boot Hangs: Use nofail option for non-critical filesystems
  • Wrong UUID: Verify UUID with blkid, update fstab
  • Syntax Errors: Use mount -a --fake to test fstab
  • Missing Dependencies: Use nofail,x-systemd.device-timeout for network filesystems

Performance Issues

  • Slow I/O: Use noatime,nodiratime options, check for disk issues
  • Network Latency: Adjust NFS/CIFS timeout options, use hard/soft mounts appropriately
  • Memory Issues: Monitor cache usage, adjust dirty page ratios
  • Permission Problems: Check uid/gid mapping for network filesystems

Unmount Problems

  • Device Busy: Find and terminate processes using lsof or fuser -k
  • Stale NFS Handles: Use umount -f (force) or umount -l (lazy)
  • Zombie Processes: Check for defunct processes, reboot if necessary
  • Filesystem Corruption: Run fsck after forced unmount

Advanced Mount Techniques

AutoFS Configuration

On-demand automounting for network resources.

autofs, systemd.automount

AutoFS Setup:

# Install autofs
sudo apt install autofs

# Configure /etc/auto.master
/mnt/autofs /etc/auto.nfs --timeout=300

# Configure /etc/auto.nfs
data -fstype=nfs,rw,hard nfs-server:/export/data
home -fstype=nfs,rw,soft user-server:/export/home

# Configure systemd automount
# /etc/fstab entry:
/dev/sdb1 /mnt/data ext4 defaults,x-systemd.automount 0 2

# Reload and restart
sudo systemctl daemon-reload
sudo systemctl restart autofs

Overlay Filesystems

Layered filesystems for containers and special use cases.

mount -t overlay

Overlay Mounts:

# Create overlay mount
sudo mount -t overlay overlay \
  -o lowerdir=/lower,upperdir=/upper,workdir=/work \
  /merged

# Docker-style overlay
sudo mount -t overlay overlay \
  -o lowerdir=/base1:/base2,upperdir=/diff,workdir=/work \
  /merged

# Read-only overlay
sudo mount -t overlay overlay \
  -o lowerdir=/lower1:/lower2,workdir=/work \
  /merged

# Persistent in fstab
overlay /merged overlay lowerdir=/lower,upperdir=/upper,workdir=/work 0 0

Debugging & Troubleshooting

Advanced debugging techniques for mount issues.

strace, dmesg, systemd-analyze

Debugging Techniques:

# Trace mount system calls
strace mount /dev/sdb1 /mnt/data

# Check kernel messages
dmesg | grep -i mount
journalctl -f | grep -i mount

# Analyze boot time mounts
systemd-analyze critical-chain
systemctl list-dependencies local-fs.target

# Check filesystem consistency
sudo fsck -f /dev/sdb1

# Monitor mount operations
inotifywait -m /mnt/data

# Check for kernel module issues
lsmod | grep nfs
modinfo nfs

# Verify systemd mount units
systemctl status mnt-data.mount
Important Considerations:
• Always backup data before filesystem operations
• Use nofail in fstab for non-critical filesystems to prevent boot hangs
• Test fstab changes with mount -a --fake before rebooting
• Be cautious with force unmount - can cause data loss
• Monitor disk space to prevent filesystem corruption
• Use UUIDs instead of device names in fstab for persistence
• Understand the difference between hard and soft NFS mounts
• Keep filesystem checks (fsck) scheduled and monitored
• Document complex mount configurations for troubleshooting
Pro Tips:
• Use findmnt instead of mount for better output formatting
• Prefer UUIDs over device names in fstab for persistent identification
• Use noatime option for better performance on frequently read filesystems
• Configure AutoFS for network filesystems to handle connection issues gracefully
• Use mount --bind for creating flexible directory structures
• Monitor /proc/mounts for real-time mount information
• Use systemd.mount units for complex mount configurations
• Test mount configurations in virtual machines before production use
• Keep a backup of working fstab configurations

Key Takeaways

Effective filesystem mounting requires understanding both basic operations and advanced configurations. By mastering mount options, fstab syntax, and troubleshooting techniques, you can ensure reliable filesystem access in any Linux environment. Remember that mounting bridges the gap between physical storage and accessible data, making it a critical skill for system administration.

Next Step: Explore advanced filesystem topics like distributed filesystems (Ceph, GlusterFS), storage performance tuning, container storage management, and automated storage provisioning with tools like Ansible.