umount Command
Safely unmount filesystems and devices from the Linux filesystem hierarchy with proper cleanup and error handling.
Syntax
umount [OPTIONS] directory|device
umount [OPTIONS] -a
The umount command detaches mounted filesystems from the filesystem hierarchy, ensuring data integrity and proper cleanup.
Common Options
| Option | Description |
|---|---|
-a |
Unmount all filesystems in /proc/mounts |
-f |
Force unmount (for unreachable NFS mounts) |
-l |
Lazy unmount (detach now, cleanup when not busy) |
-n |
Don't write to /etc/mtab |
-r |
Remount read-only if unmount fails |
-v |
Verbose mode |
-t fstype |
Unmount only filesystems of specified type |
-O option |
Unmount filesystems with specified mount option |
--fake |
Dry run (don't actually unmount) |
Basic Usage
Unmounting by mount point
# Unmount by mount point
umount /mnt/usb
umount /media/cdrom
umount /home
# Unmount with verbose output
umount -v /mnt/backup
# Check what's mounted first
mount | grep /mnt/usb
umount /mnt/usb
Unmount filesystems using their mount points
Unmounting by device
# Unmount by device name
umount /dev/sdb1
umount /dev/sdc1
umount /dev/loop0
# Unmount USB devices
umount /dev/sdb1
# Unmount CD/DVD
umount /dev/sr0
# List mounted devices first
lsblk
umount /dev/sdb1
Unmount filesystems using device names
Multiple unmounts
# Unmount multiple mount points
umount /mnt/usb1 /mnt/usb2 /mnt/backup
# Unmount all removable media
umount /media/*
# Unmount all filesystems of specific type
umount -a -t vfat
umount -a -t ntfs
# Unmount all except essential filesystems
umount -a -t noext4,noext3,noxfs
Unmount multiple filesystems at once
Handling Busy Filesystems
Identifying what's keeping filesystem busy
# Try to unmount
umount /mnt/data
# Error: umount: /mnt/data: target is busy
# Find processes using the filesystem
lsof /mnt/data
fuser -v /mnt/data
# Find processes with current directory in filesystem
fuser -c /mnt/data
# Show detailed process information
lsof +D /mnt/data
# Kill processes using the filesystem
fuser -k /mnt/data
# Then try unmounting again
umount /mnt/data
Identify and resolve busy filesystem issues
Force and lazy unmounting
# Lazy unmount (detach immediately, cleanup later)
umount -l /mnt/data
# Force unmount (mainly for NFS)
umount -f /mnt/nfs
# Combine lazy and force
umount -fl /mnt/problematic
# Remount read-only if unmount fails
umount -r /mnt/data
# Kill processes and unmount
fuser -km /mnt/data
umount /mnt/data
Force unmounting when normal unmount fails
Safe unmounting procedure
#!/bin/bash
# Safe unmount script
MOUNT_POINT="/mnt/external"
# Check if mounted
if ! mountpoint -q "$MOUNT_POINT"; then
echo "Not mounted: $MOUNT_POINT"
exit 0
fi
# Sync data
sync
# Try normal unmount
if umount "$MOUNT_POINT" 2>/dev/null; then
echo "Successfully unmounted: $MOUNT_POINT"
exit 0
fi
# Find what's using it
echo "Filesystem busy. Processes using $MOUNT_POINT:"
lsof +D "$MOUNT_POINT" 2>/dev/null || fuser -v "$MOUNT_POINT" 2>/dev/null
# Ask user for action
read -p "Kill processes and unmount? (y/N): " -n 1 -r
echo
if [[ $REPLY =~ ^[Yy]$ ]]; then
fuser -k "$MOUNT_POINT"
sleep 2
umount "$MOUNT_POINT"
fi
Safe unmounting procedure with error handling
Filesystem Types and Special Cases
Network filesystems
# Unmount NFS shares
umount /mnt/nfs-share
# Force unmount unresponsive NFS
umount -f /mnt/nfs-share
# Unmount CIFS/SMB shares
umount /mnt/smb-share
# Unmount SSHFS
umount /mnt/sshfs
fusermount -u /mnt/sshfs # Alternative for FUSE
# Unmount all NFS mounts
umount -a -t nfs,nfs4
Handle network filesystem unmounting
Special filesystems
# Unmount loop devices
umount /mnt/iso
losetup -d /dev/loop0 # Detach loop device
# Unmount encrypted filesystems
umount /mnt/encrypted
cryptsetup luksClose encrypted-volume
# Unmount bind mounts
umount /mnt/bind-mount
# Unmount tmpfs
umount /mnt/tmpfs
# Unmount FUSE filesystems
fusermount -u /mnt/fuse-mount
Handle special filesystem types
Removable media
# Unmount USB drives
umount /media/usb
umount /dev/sdb1
# Unmount CD/DVD
umount /media/cdrom
eject /dev/sr0 # Eject after unmount
# Unmount SD cards
umount /media/sdcard
# Safe removal of USB device
sync # Flush buffers
umount /dev/sdb1
eject /dev/sdb # Safe to remove
# Unmount all removable media
for mount in /media/*; do
[ -d "$mount" ] && umount "$mount" 2>/dev/null
done
Safely unmount removable media devices
System Administration
Maintenance operations
# Unmount for filesystem check
umount /dev/sdb1
fsck /dev/sdb1
mount /dev/sdb1 /mnt/data
# Unmount for backup
umount /home
dd if=/dev/sda2 of=/backup/home.img
mount /home
# Unmount before system shutdown
umount -a -r # Remount all read-only
# Emergency unmount all
umount -a -f -l
Administrative tasks requiring unmounting
Scripted unmounting
#!/bin/bash
# Backup script with unmounting
BACKUP_MOUNT="/mnt/backup"
BACKUP_DEVICE="/dev/sdc1"
# Mount backup drive
mount "$BACKUP_DEVICE" "$BACKUP_MOUNT"
# Perform backup
rsync -av /home/ "$BACKUP_MOUNT/home-backup/"
# Sync and unmount
sync
umount "$BACKUP_MOUNT"
echo "Backup completed and drive unmounted"
# Cleanup script for development
cleanup_mounts() {
local mounts=("/mnt/test1" "/mnt/test2" "/mnt/temp")
for mount in "${mounts[@]}"; do
if mountpoint -q "$mount"; then
echo "Unmounting $mount"
umount "$mount" || umount -l "$mount"
fi
done
}
Automated unmounting in scripts
Troubleshooting
Common Unmount Errors
- Target is busy - Files or processes are using the filesystem
- Not mounted - Filesystem is not currently mounted
- Permission denied - Need root privileges to unmount
- Invalid argument - Mount point or device doesn't exist
- Resource temporarily unavailable - Network filesystem issues
Diagnostic commands
# Check what's mounted
mount | grep /mnt/data
mountpoint /mnt/data
# Check filesystem usage
lsof +D /mnt/data
fuser -v /mnt/data
# Check mount options
mount | grep /mnt/data
cat /proc/mounts | grep /mnt/data
# Check for errors
dmesg | tail -20
journalctl -f
# Verify device status
lsblk
blkid /dev/sdb1
# Check filesystem health
fsck -n /dev/sdb1 # Read-only check
Diagnose unmounting issues
Recovery procedures
# Force unmount stuck NFS
umount -f -l /mnt/nfs
# Recover from failed unmount
mount -o remount,ro /mnt/data # Remount read-only
sync
umount /mnt/data
# Clear stale file handles
echo 3 > /proc/sys/vm/drop_caches
umount /mnt/data
# Emergency remount root read-only
mount -o remount,ro /
# Fix corrupted mount table
grep -v /mnt/problematic /etc/mtab > /tmp/mtab
mv /tmp/mtab /etc/mtab
Recovery procedures for unmount failures
Best Practices
Safe Unmounting Practices
- Always sync data before unmounting:
sync - Check if filesystem is busy before unmounting
- Use lazy unmount (-l) for stuck filesystems
- Verify unmount success before removing devices
- Keep backups before unmounting critical filesystems
- Use proper shutdown procedures for network filesystems
- Monitor system logs for unmount errors
Important Warnings
- Data loss risk - Never remove devices without proper unmounting
- System stability - Don't force unmount system filesystems
- Network timeouts - NFS unmounts may hang on network issues
- Process termination - fuser -k kills processes, use carefully
- Lazy unmount - May hide underlying issues, use as last resort