Linux umount Command

Safely Unmount Filesystems and Storage Devices

Command Overview

umount (note: "unmount" but spelled "umount") detaches a mounted filesystem from the directory tree. Before removing storage media or performing maintenance, filesystems must be properly unmounted to ensure all cached data is written to disk and file handles are closed. Attempting to remove media without unmounting can result in data corruption.

Critical: Always unmount filesystems before physically disconnecting storage devices (USB drives, external hard drives, network shares). Failing to do so can cause:
  • Data loss or corruption
  • Filesystem inconsistencies
  • Lost write operations still in cache
  • Potential hardware damage

Basic Syntax

umount [OPTIONS] {DIRECTORY | DEVICE}

You can unmount by specifying either the mount point (directory) or the device name.

Example 1: Basic Unmount by Mount Point

sudo umount /mnt/backup
# No output means success
Explanation:

Unmounts the filesystem mounted at /mnt/backup. This is the most common and recommended way to unmount.

  • sudo: Root privileges usually required for unmounting
  • /mnt/backup: The mount point (directory)
  • Silent success: No output indicates successful unmount
  • Best practice: Unmount by mount point rather than device
Tip: Verify unmount was successful with mount | grep backup or df -h

Example 2: Unmount by Device Name

sudo umount /dev/sdb1
# Device successfully unmounted
Explanation:

Unmounts the filesystem by specifying the device name. Works even if you don't remember the mount point.

  • /dev/sdb1: Device identifier
  • Alternative method: When mount point is unknown
  • Multiple mounts: Device may be mounted multiple times
  • Use case: Quick unmount of USB devices

Example 3: Force Unmount (Use with Caution)

sudo umount -f /mnt/nfs-share
# Force unmounting /mnt/nfs-share
Explanation:

Forces unmount when normal unmount fails, typically used for unreachable network filesystems (NFS, CIFS).

  • -f flag: Force unmount even if busy
  • Network shares: Primary use case for force unmount
  • Data risk: May cause data loss if operations pending
  • Last resort: Try normal unmount first
Warning: Force unmount can cause data loss and should only be used when absolutely necessary, particularly for local filesystems. It's safer for network filesystems that are unreachable.

Example 4: Lazy Unmount (Detach When No Longer Busy)

sudo umount -l /mnt/busy-filesystem
# Filesystem marked for unmount, will detach when no longer busy
Explanation:

Lazy unmount immediately detaches the filesystem from the directory tree but waits to clean up until all references are closed.

  • -l flag: Lazy unmount (detach now, cleanup later)
  • Immediate detach: Mount point immediately available
  • Deferred cleanup: Actual unmount happens when possible
  • Busy filesystems: Useful when can't identify what's using it
  • Safe alternative: Safer than force unmount

Example 5: Unmount All Filesystems of Specific Type

sudo umount -a -t nfs
# Unmounting all NFS filesystems
Explanation:

Unmounts all mounted filesystems of a specific type. Useful for batch operations.

  • -a flag: Unmount all filesystems (with filters)
  • -t nfs: Limit to NFS filesystem type
  • Batch operation: Handles multiple mounts at once
  • Shutdown preparation: Clean up before system shutdown
  • Types: nfs, cifs, ext4, xfs, etc.
Note: The -a option respects /etc/fstab and won't unmount filesystems critical for system operation.

Example 6: Handle Busy Filesystem - Find and Close Processes

# Attempt unmount sudo umount /mnt/data # If busy, find what's using it sudo lsof +D /mnt/data # Or use fuser sudo fuser -m /mnt/data # Kill processes using it (if safe) sudo fuser -km /mnt/data # Now unmount sudo umount /mnt/data
# First attempt: umount: /mnt/data: target is busy. # lsof output: COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME bash 1234 user cwd DIR 8,17 4096 2 /mnt/data vim 5678 user cwd DIR 8,17 4096 2 /mnt/data/file.txt # After killing processes: # Successfully unmounted
Explanation:

Demonstrates the proper way to handle a "target is busy" error by identifying and closing processes using the filesystem.

  • lsof +D: List open files in directory tree
  • fuser -m: Show processes using the mount point
  • fuser -km: Kill processes using the filesystem
  • Careful approach: Identify before killing
  • Data safety: Close programs properly when possible

Example 7: Unmount Multiple USB Devices

# List mounted USB devices mount | grep /dev/sd # Unmount each one sudo umount /dev/sdb1 sudo umount /dev/sdc1 sudo umount /dev/sdd1 # Or unmount by mount points sudo umount /media/usb1 /media/usb2 /media/usb3
/dev/sdb1 on /media/usb1 type vfat (rw,nosuid,nodev) /dev/sdc1 on /media/usb2 type ext4 (rw,nosuid,nodev) /dev/sdd1 on /media/usb3 type ntfs (rw,nosuid,nodev) # All devices successfully unmounted
Explanation:

Shows how to safely unmount multiple USB drives before physical removal.

  • Identify first: Use mount or lsblk to find devices
  • Multiple methods: By device or mount point
  • Batch unmount: Can specify multiple targets
  • USB safety: Always unmount before removing
  • Verification: Check with df -h after unmounting

Example 8: Unmount with Verbose Output

sudo umount -v /mnt/external
umount: /mnt/external (/dev/sdb1) unmounted
Explanation:

Provides verbose output showing what was unmounted, useful for scripts and logging.

  • -v flag: Verbose mode
  • Confirmation: Shows device and mount point
  • Scripting: Better for automated scripts
  • Logging: Helpful for audit trails
  • Debugging: Verify correct filesystem unmounted

Example 9: Unmount Read-Only Remounted Filesystem

# First remount as read-only sudo mount -o remount,ro /mnt/data # Verify read-only mount | grep /mnt/data # Now safely unmount sudo umount /mnt/data
/dev/sdb1 on /mnt/data type ext4 (ro,relatime) # Successfully unmounted
Explanation:

Demonstrates safe unmounting by first remounting as read-only to flush all writes and prevent new ones.

  • remount,ro: Change to read-only without unmounting
  • Safety measure: Ensures all writes are flushed
  • Busy prevention: Stops new writes
  • Critical data: Extra safety for important filesystems
  • Best practice: Especially for database volumes

Example 10: Comprehensive Unmount Script with Error Handling

#!/bin/bash # Safe unmount script with error handling MOUNT_POINT="/mnt/backup" echo "Attempting to unmount $MOUNT_POINT" # Check if mounted if ! mountpoint -q "$MOUNT_POINT"; then echo "Error: $MOUNT_POINT is not a mount point" exit 1 fi # Attempt normal unmount if sudo umount "$MOUNT_POINT"; then echo "Successfully unmounted $MOUNT_POINT" exit 0 fi # If busy, identify what's using it echo "Filesystem is busy. Checking for processes..." sudo lsof +D "$MOUNT_POINT" # Ask user if they want to force read -p "Force unmount? (y/N) " -n 1 -r echo if [[ $REPLY =~ ^[Yy]$ ]]; then echo "Attempting lazy unmount..." if sudo umount -l "$MOUNT_POINT"; then echo "Lazy unmount initiated" exit 0 else echo "Lazy unmount failed" exit 1 fi else echo "Unmount cancelled by user" exit 1 fi
Attempting to unmount /mnt/backup Filesystem is busy. Checking for processes... COMMAND PID USER FD TYPE DEVICE SIZE/OFF NODE NAME rsync 2345 user cwd DIR 8,17 4096 2 /mnt/backup Force unmount? (y/N) y Attempting lazy unmount... Lazy unmount initiated
Explanation:

A comprehensive script that safely handles unmounting with proper error checking and user interaction.

  • mountpoint -q: Checks if target is actually mounted
  • Error handling: Gracefully handles each failure case
  • Process identification: Shows what's preventing unmount
  • User confirmation: Asks before force unmounting
  • Lazy unmount: Safer alternative to force
  • Exit codes: Proper success/failure indication

Additional Information

Essential Options Summary

Common Error Messages

Error Message Meaning Solution
target is busy Files are open or programs using filesystem Use lsof or fuser to identify and close processes
not mounted Specified device/directory not mounted Check with mount or df -h
must be superuser Insufficient permissions Use sudo umount
device is busy System is accessing the device Wait or use lazy unmount (-l)
umount: /path: not found Mount point doesn't exist Check path spelling and existence

Safe Unmount Workflow

  1. Close applications: Close all programs using the filesystem
  2. Navigate away: Change directory if you're in the mount point
  3. Sync data: Run sync to flush buffers
  4. Attempt unmount: Use sudo umount /mount/point
  5. If busy: Identify processes with lsof +D or fuser -m
  6. Close processes: Close them properly or kill if necessary
  7. Retry unmount: Try again after closing processes
  8. Verify: Check with mount | grep or df -h

Finding What's Using a Filesystem

# Method 1: lsof (list open files) sudo lsof +D /mnt/data # Method 2: fuser (file user) sudo fuser -mv /mnt/data # Method 3: Check current directory of all processes sudo lsof | grep /mnt/data # Method 4: See what users are accessing sudo fuser -m /mnt/data

Filesystem Types and Special Considerations

Filesystem Type Unmount Considerations Special Notes
NFS May hang if server unreachable Use -f to force unmount network shares
CIFS/SMB Can be slow to unmount Check network connection first
USB/External Must unmount before physical removal Wait for activity light to stop
Loop devices May need to detach loop after unmount Use losetup -d after unmount
Encrypted (LUKS) Need to close encryption after unmount Follow with cryptsetup luksClose

Related Commands

Best Practices

Unmounting in Different Scenarios

USB Drive:

# Sync data sync # Unmount sudo umount /media/usb # Or use udisksctl (doesn't need sudo) udisksctl unmount -b /dev/sdb1 udisksctl power-off -b /dev/sdb

Network Share (NFS):

# Normal unmount sudo umount /mnt/nfs-share # If server is down, force unmount sudo umount -f /mnt/nfs-share # Or lazy unmount sudo umount -l /mnt/nfs-share

Encrypted Volume:

# Unmount the filesystem sudo umount /mnt/encrypted # Close the LUKS container sudo cryptsetup luksClose encrypted_volume

Loop Device:

# Unmount the loop-mounted filesystem sudo umount /mnt/iso # Detach the loop device sudo losetup -d /dev/loop0

Emergency Procedures

When System Won't Shut Down Due to Busy Filesystem:
  1. Try lazy unmount: sudo umount -l /mount/point
  2. If that fails, reboot with: sudo systemctl reboot -f
  3. As last resort: sudo systemctl reboot -ff (immediate reboot)
  4. After reboot, check filesystem: sudo fsck /dev/sdX

Checking Mount Status

# List all mounts mount # Check specific mount mount | grep /mnt/data # Using df df -h /mnt/data # Using findmnt findmnt /mnt/data # Check if directory is a mount point mountpoint /mnt/data

Common Use Cases

Pro Tip: Create a simple alias for safe unmounting:
# Add to ~/.bashrc alias safe-umount='sync && sudo umount' # Usage: safe-umount /mnt/backup

This ensures data is flushed before unmounting.

Automatic Unmount on System Shutdown

Systemd automatically unmounts filesystems during shutdown in the correct order. However, for network filesystems, you may want to ensure they unmount before network goes down:

# In /etc/fstab, add _netdev option for network filesystems: server:/export /mnt/nfs nfs defaults,_netdev 0 0

The _netdev option tells systemd that this is a network device and should be unmounted before network shutdown.

Troubleshooting Checklist

  1. ✓ Are you in the mount point directory? (cd away from it)
  2. ✓ Are any programs accessing files? (check with lsof/fuser)
  3. ✓ Are you using sudo? (unmounting requires root)
  4. ✓ Is the device actually mounted? (check with mount or df)
  5. ✓ For network shares, is the server reachable? (try ping)
  6. ✓ Have you synced data? (run sync command)
  7. ✓ Is another user accessing the filesystem? (check fuser output)
  8. ✓ Are there any systemd services using it? (systemctl list-units)