đź”§ mount Command

Attaching Filesystems to the Linux Directory Tree

Overview

The mount command attaches a filesystem to a specific location in the directory tree, making its contents accessible. This is fundamental to Linux filesystem management, as it connects storage devices (hard drives, USB drives, network shares, etc.) to mount points in the directory hierarchy. Understanding mount is essential for system administration, storage management, and troubleshooting filesystem issues.

Syntax: mount [OPTIONS] device mountpoint

Or: mount [OPTIONS] mountpoint (when specified in /etc/fstab)

The mount command makes a filesystem on a device available at a specified mount point. Without arguments, mount displays all currently mounted filesystems.

Example 1

Viewing All Mounted Filesystems

$ mount sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime) proc on /proc type proc (rw,nosuid,nodev,noexec,relatime) udev on /dev type devtmpfs (rw,nosuid,relatime,size=8154876k) devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620) tmpfs on /run type tmpfs (rw,nosuid,nodev,noexec,relatime,size=1634672k) /dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro) /dev/sda1 on /boot type ext4 (rw,relatime) /dev/sdb1 on /mnt/data type ext4 (rw,relatime) # More readable column format: $ mount | column -t # Filter by filesystem type: $ mount -t ext4 /dev/sda2 on / type ext4 (rw,relatime,errors=remount-ro) /dev/sda1 on /boot type ext4 (rw,relatime) /dev/sdb1 on /mnt/data type ext4 (rw,relatime) # Show only device name and mount point: $ mount | awk '{print $1, $3}' sysfs /sys proc /proc /dev/sda2 / /dev/sdb1 /mnt/data

What's Happening:

Running mount without arguments displays all currently mounted filesystems. Each line shows the device, mount point, filesystem type, and mount options in parentheses. This is essential for troubleshooting, understanding the system's filesystem layout, and verifying that devices are mounted correctly. The output includes pseudo-filesystems (like /proc, /sys) as well as physical storage.

Pro Tip: Use findmnt for a tree view: findmnt or df -h for a simpler overview with disk usage.
Example 2

Basic Mount: Attaching a USB Drive

# First, identify the device: $ lsblk NAME MAJ:MIN RM SIZE RO TYPE MOUNTPOINT sda 8:0 0 238.5G 0 disk ├─sda1 8:1 0 512M 0 part /boot └─sda2 8:2 0 238G 0 part / sdb 8:16 1 14.9G 0 disk └─sdb1 8:17 1 14.9G 0 part # Create mount point if needed: $ sudo mkdir -p /mnt/usb # Mount the USB drive: $ sudo mount /dev/sdb1 /mnt/usb # Verify it's mounted: $ mount | grep sdb1 /dev/sdb1 on /mnt/usb type vfat (rw,relatime) # Check contents: $ ls /mnt/usb documents photos videos # When done, unmount: $ sudo umount /mnt/usb

What's Happening:

This is the most common mount operation: attaching a removable drive. First, identify the device with lsblk. Create a mount point (directory) if it doesn't exist. Mount the device to that location. The filesystem type is usually auto-detected. After mounting, the device's contents are accessible at the mount point. Always unmount before physically removing the device to prevent data corruption.

Important: Always unmount USB drives before removing them physically! Use umount /mnt/usb or eject /dev/sdb.
Example 3

Mounting with Specific Filesystem Type

# Mount ext4 filesystem: $ sudo mount -t ext4 /dev/sdb1 /mnt/data # Mount NTFS filesystem (Windows): $ sudo mount -t ntfs-3g /dev/sdb1 /mnt/windows # Or with full read-write support: $ sudo mount -t ntfs-3g -o rw,uid=1000,gid=1000 /dev/sdb1 /mnt/windows # Mount FAT32: $ sudo mount -t vfat /dev/sdb1 /mnt/flash # Mount exFAT: $ sudo mount -t exfat /dev/sdb1 /mnt/external # Mount XFS: $ sudo mount -t xfs /dev/sdb1 /mnt/xfs_data # Mount Btrfs: $ sudo mount -t btrfs /dev/sdb1 /mnt/btrfs_vol # Check filesystem type first: $ sudo blkid /dev/sdb1 /dev/sdb1: UUID="..." TYPE="ext4" # Auto-detect (usually works): $ sudo mount /dev/sdb1 /mnt/data # Filesystem type auto-detected

What's Happening:

The -t flag explicitly specifies the filesystem type. This is necessary for filesystems that require special handling (like NTFS) or when auto-detection fails. Different filesystem types have different capabilities and mount options. For example, NTFS requires the ntfs-3g driver for full read-write support. Auto-detection usually works, but explicit type specification ensures correct handling and can prevent errors.

Note: Use blkid or file -s /dev/sdb1 to identify filesystem type before mounting.
Example 4

Mount Options: Read-Only, Permissions, and More

# Mount read-only: $ sudo mount -o ro /dev/sdb1 /mnt/readonly # Prevents any writes to the filesystem # Mount read-write (explicit): $ sudo mount -o rw /dev/sdb1 /mnt/data # Mount with specific user permissions (for FAT/NTFS): $ sudo mount -o uid=1000,gid=1000,umask=022 /dev/sdb1 /mnt/shared # uid=1000: owned by user with ID 1000 # gid=1000: group owned by group ID 1000 # umask=022: files created with 755 permissions # Mount with noexec (prevent execution): $ sudo mount -o noexec,nosuid,nodev /dev/sdb1 /mnt/untrusted # noexec: can't execute binaries # nosuid: ignores setuid/setgid bits # nodev: doesn't interpret device files # Mount with specific options for performance: $ sudo mount -o noatime,nodiratime /dev/sdb1 /mnt/fast # noatime: don't update access times (faster) # nodiratime: don't update directory access times # Multiple options combined: $ sudo mount -o rw,noatime,uid=1000,gid=1000 /dev/sdb1 /mnt/mydata # Check current mount options: $ mount | grep sdb1 /dev/sdb1 on /mnt/mydata type ext4 (rw,noatime,uid=1000,gid=1000)

What's Happening:

Mount options (-o) control how the filesystem behaves. Read-only (ro) prevents modifications—crucial for forensics or protecting data. The noexec/nosuid/nodev options enhance security by preventing executable code and device access. The noatime option improves performance by not updating access timestamps. Permission options (uid/gid/umask) are essential for multi-user access, especially on filesystems that don't support Unix permissions natively (FAT, NTFS).

Security Tip: Always mount untrusted USB drives with noexec,nosuid,nodev to prevent malicious code execution.
Example 5

Mounting by UUID or Label

# Get UUID and label: $ sudo blkid /dev/sdb1 /dev/sdb1: LABEL="BACKUP" UUID="a1b2c3d4-1234-5678-9abc-def012345678" TYPE="ext4" # Mount by UUID (preferred for /etc/fstab): $ sudo mount UUID=a1b2c3d4-1234-5678-9abc-def012345678 /mnt/backup # Mount by label: $ sudo mount LABEL=BACKUP /mnt/backup # Why use UUID instead of device name: # /dev/sdb1 might become /dev/sdc1 if you add another disk # UUID never changes, ensuring correct device is mounted # Find UUID for all devices: $ sudo blkid /dev/sda1: UUID="..." TYPE="ext4" /dev/sda2: UUID="..." TYPE="ext4" /dev/sdb1: LABEL="BACKUP" UUID="..." TYPE="ext4" # Mount multiple devices by UUID: $ sudo mount UUID=first-uuid /mnt/disk1 $ sudo mount UUID=second-uuid /mnt/disk2 # In /etc/fstab (persistent mounting): UUID=a1b2c3d4-1234-5678-9abc-def012345678 /mnt/backup ext4 defaults 0 2

What's Happening:

UUIDs (Universally Unique Identifiers) and labels provide stable identifiers for devices, unlike device names (/dev/sdX) which can change between boots. This is critical for /etc/fstab entries and automated mounting. Labels are human-readable but must be unique. UUIDs are guaranteed unique but harder to remember. Using UUID/label ensures your system mounts the correct device even if device enumeration order changes.

Best Practice: Always use UUID in /etc/fstab for reliability. Use labels for removable media when you need human-readable identification.
Example 6

Network Filesystems: NFS and SMB/CIFS

# Mount NFS share: $ sudo mount -t nfs server.example.com:/shared /mnt/nfs # Or with explicit version: $ sudo mount -t nfs -o nfsvers=4 server:/export /mnt/nfs # Mount with NFS options: $ sudo mount -t nfs -o rw,soft,intr,timeo=30 server:/data /mnt/nfs_data # soft: return error on timeout # intr: allow interruption # timeo: timeout value # Mount SMB/CIFS share (Windows share): $ sudo mount -t cifs //server/share /mnt/windows -o username=user,password=pass # Better: use credentials file: $ cat /home/craig/.smbcredentials username=myuser password=mypassword domain=WORKGROUP $ sudo mount -t cifs //server/share /mnt/windows \ -o credentials=/home/craig/.smbcredentials,uid=1000,gid=1000 # Mount with guest access: $ sudo mount -t cifs //server/public /mnt/public -o guest # Check network mounts: $ mount -t nfs,nfs4,cifs server:/export on /mnt/nfs type nfs4 (rw,relatime) //server/share on /mnt/windows type cifs (rw,relatime) # Unmount network share: $ sudo umount /mnt/nfs

What's Happening:

Network filesystems allow mounting remote shares as if they were local. NFS (Network File System) is the Unix/Linux standard for network storage. SMB/CIFS is used for Windows shares. These require network connectivity and proper credentials. Options control behavior on network failures (soft vs. hard mounts) and timeout values. Using a credentials file is more secure than putting passwords on the command line.

Security: Never put passwords on the command line! Use credentials files with proper permissions (chmod 600).
Example 7

Special Mounts: tmpfs, Loop Devices, and ISO Images

# Mount tmpfs (RAM-based filesystem): $ sudo mount -t tmpfs -o size=1G tmpfs /mnt/ramdisk # Creates 1GB RAM disk for temporary fast storage # Mount ISO image: $ sudo mount -o loop image.iso /mnt/iso # Access ISO contents without burning to disc # Mount disk image: $ sudo mount -o loop disk.img /mnt/image # Mount specific partition from image: $ sudo mount -o loop,offset=1048576 disk.img /mnt/partition # offset calculated from fdisk or parted # Create and mount encrypted loop device: $ dd if=/dev/zero of=encrypted.img bs=1M count=100 $ sudo cryptsetup luksFormat encrypted.img $ sudo cryptsetup open encrypted.img myencrypted $ sudo mount /dev/mapper/myencrypted /mnt/encrypted # Mount squashfs (compressed read-only): $ sudo mount -t squashfs -o loop filesystem.squashfs /mnt/squash # Bind mount (mount directory elsewhere): $ sudo mount --bind /original/path /new/path # Same directory accessible from two locations # Check tmpfs mounts: $ df -h | grep tmpfs tmpfs 1.0G 0 1.0G 0% /mnt/ramdisk

What's Happening:

Special mount types serve specific purposes. Tmpfs creates a RAM-based filesystem—extremely fast but volatile (data lost on reboot). Loop devices allow mounting files as if they were block devices, essential for ISOs and disk images. Bind mounts create alternate access points to existing directories. Squashfs is compressed and read-only, used in live CDs. These techniques are fundamental for system administration, testing, and application deployment.

Use Cases: tmpfs for /tmp or build directories, loop mounts for ISOs and containers, bind mounts for chroot environments and Docker.
Example 8

Persistent Mounting with /etc/fstab

# View current fstab: $ cat /etc/fstab # <filesystem> <mountpoint> <type> <options> <dump> <pass> UUID=abcd-1234... / ext4 defaults 0 1 UUID=efgh-5678... /boot ext4 defaults 0 2 UUID=ijkl-9012... /home ext4 defaults 0 2 UUID=mnop-3456... none swap sw 0 0 # Add new entry for automatic mounting: $ sudo nano /etc/fstab # Add data disk: UUID=a1b2c3d4-... /mnt/data ext4 defaults,noatime 0 2 # Add USB drive (optional mount): UUID=qrst-uvwx... /media/usb vfat noauto,user,rw,uid=1000 0 0 # noauto: don't mount at boot # user: allow regular users to mount # Add NFS share: server:/export /mnt/nfs nfs defaults,_netdev 0 0 # _netdev: mount after network is available # Add tmpfs: tmpfs /tmp tmpfs defaults,noatime,mode=1777 0 0 # Test fstab entries without rebooting: $ sudo mount -a # Mounts all filesystems in fstab that aren't mounted # Test specific entry: $ sudo mount /mnt/data # Uses settings from fstab # Verify after editing: $ sudo findmnt --verify # Checks fstab for errors # Backup fstab before editing: $ sudo cp /etc/fstab /etc/fstab.backup

What's Happening:

/etc/fstab defines filesystems to be mounted at boot time. Each line specifies a device (by UUID preferred), mount point, filesystem type, mount options, dump frequency, and fsck pass number. The defaults option applies standard settings (rw, suid, dev, exec, auto, nouser, async). The noauto option prevents automatic mounting, useful for removable media. The mount -a command tests fstab entries without rebooting.

Critical: Errors in /etc/fstab can prevent boot! Always:
  • Backup fstab before editing
  • Test with mount -a before rebooting
  • Use findmnt --verify to check syntax
  • Have recovery media available
Example 9

Remounting and Changing Mount Options

# Remount filesystem read-write: $ sudo mount -o remount,rw /dev/sdb1 /mnt/data # Changes mount options without unmounting # Remount root filesystem read-write (recovery): $ sudo mount -o remount,rw / # Critical for system recovery when root is ro # Remount with different options: $ sudo mount -o remount,noatime /home # Add noatime option to already-mounted /home # Remount read-only (for maintenance): $ sudo mount -o remount,ro /mnt/data # Safer for fsck or backup # Common remount scenarios: # System in single-user mode (root is ro): $ sudo mount -o remount,rw / $ # Now you can make changes # Before running fsck: $ sudo mount -o remount,ro /dev/sdb1 $ sudo fsck /dev/sdb1 $ sudo mount -o remount,rw /dev/sdb1 # Change from rw to ro on running system: $ sync # Flush buffers first $ sudo mount -o remount,ro /mnt/backup # Check if remount succeeded: $ mount | grep sdb1 /dev/sdb1 on /mnt/data type ext4 (rw,noatime) # Can't remount if files are open: $ sudo fuser -m /mnt/data # Check who's using it $ sudo lsof +D /mnt/data # List open files

What's Happening:

Remounting allows changing mount options without unmounting, which would be disruptive or impossible if files are in use. This is essential for system administration: remounting root read-write in single-user mode, changing options for performance tuning, or preparing filesystems for maintenance. The sync command flushes buffers before remounting read-only to ensure data integrity.

Important: Can't remount if files are open. Use fuser -m or lsof to identify processes accessing the filesystem.
Example 10

Troubleshooting Mount Issues

# Check why mount failed: $ sudo mount /dev/sdb1 /mnt/data mount: /mnt/data: wrong fs type, bad option, bad superblock... # Get more details: $ sudo dmesg | tail [error messages about filesystem] # Check filesystem type: $ sudo blkid /dev/sdb1 /dev/sdb1: TYPE="ext4" # Try specifying type explicitly: $ sudo mount -t ext4 /dev/sdb1 /mnt/data # Check if device is in use: $ sudo lsof /dev/sdb1 $ sudo fuser -v /dev/sdb1 # Device is busy (can't unmount): $ sudo umount /mnt/data umount: /mnt/data: target is busy # Find what's using it: $ sudo lsof +D /mnt/data $ sudo fuser -vm /mnt/data # Shows process IDs using the mount # Force unmount (last resort): $ sudo umount -l /mnt/data # Lazy unmount $ sudo umount -f /mnt/data # Force unmount (NFS) # Mount point doesn't exist: $ sudo mount /dev/sdb1 /mnt/nonexistent mount: /mnt/nonexistent: mount point does not exist $ sudo mkdir -p /mnt/nonexistent $ sudo mount /dev/sdb1 /mnt/nonexistent # Permission denied: $ mount /dev/sdb1 /mnt/data mount: only root can do that $ sudo mount /dev/sdb1 /mnt/data # Filesystem needs checking: $ sudo mount /dev/sdb1 /mnt/data mount: wrong fs type, bad superblock... $ sudo fsck /dev/sdb1 $ sudo mount /dev/sdb1 /mnt/data # Check if already mounted: $ mount | grep sdb1 /dev/sdb1 on /mnt/oldlocation type ext4 (rw) # Already mounted elsewhere! # Check mount options compatibility: $ sudo mount -o ro,rw /dev/sdb1 /mnt/data mount: mutually exclusive options specified # Verbose output for debugging: $ sudo mount -v /dev/sdb1 /mnt/data mount: /dev/sdb1 mounted on /mnt/data

What's Happening:

Mount failures have many causes: wrong filesystem type, corrupted filesystem, device in use, missing mount point, permission issues, or conflicting options. The error messages, combined with dmesg, blkid, lsof, and fuser, help diagnose issues. Check if the device is already mounted elsewhere. Verify the filesystem isn't corrupted (run fsck). Ensure the mount point exists and you have proper permissions. Understanding these troubleshooting steps is essential for system administration.

Troubleshooting Checklist:
  • âś“ Does mount point directory exist?
  • âś“ Is device already mounted?
  • âś“ Do you have root/sudo privileges?
  • âś“ Is filesystem type correct?
  • âś“ Is device being used by another process?
  • âś“ Is filesystem corrupted? (run fsck)
  • âś“ Check dmesg for kernel messages

Common Mount Options

Option Description Use Case
defaults rw, suid, dev, exec, auto, nouser, async Standard mount options for most filesystems
ro Read-only Protecting data, forensics, CD-ROMs
rw Read-write Normal operation
noexec No execution of binaries Security (temp dirs, untrusted media)
nosuid Ignore suid/sgid bits Security on untrusted filesystems
nodev No device files Security on user-writable filesystems
noatime Don't update access times Performance improvement
nodiratime Don't update directory access times Performance (less impact than noatime)
sync Synchronous I/O Slower but safer (USB drives)
async Asynchronous I/O Faster but riskier (default)
auto Mount with -a Automatic mounting at boot
noauto Don't mount with -a Manual mount only (removable media)
user Allow normal users to mount CD-ROMs, floppy disks
users Any user can mount/unmount Shared removable media
remount Change mount options Modify without unmounting
_netdev Network device Wait for network before mounting
uid=N Owner user ID FAT/NTFS permissions
gid=N Owner group ID FAT/NTFS permissions
umask=NNN Permission mask Default permissions on FAT/NTFS

Best Practices & Tips

đź’ˇ Mounting Best Practices

  • Use UUID in fstab: Device names can change, UUIDs don't
  • Test fstab changes: Use mount -a before rebooting
  • Secure untrusted media: Use noexec, nosuid, nodev
  • Improve performance: Use noatime on busy filesystems
  • Always unmount properly: Prevents data corruption
  • Document mount points: Comment fstab entries clearly
  • Use appropriate options: Match options to use case

⚠️ Common Mistakes to Avoid

  • Editing fstab without backup: Can prevent boot
  • Not unmounting USB drives: Causes data corruption
  • Using device names in fstab: Use UUID instead
  • Mounting to non-empty directory: Hides existing contents
  • Forgetting network dependency: Use _netdev for network mounts
  • Force unmounting carelessly: Can cause data loss
  • Not checking mount success: Always verify

📝 Quick Mounting Guide by Device Type

  • USB Flash Drive: mount -o noexec,nosuid /dev/sdX /mnt/usb
  • External HDD: mount -o noatime /dev/sdX /mnt/external
  • NFS Share: mount -t nfs server:/path /mnt/nfs
  • Windows Share: mount -t cifs //server/share /mnt/win -o credentials=file
  • ISO Image: mount -o loop image.iso /mnt/iso
  • RAM Disk: mount -t tmpfs -o size=1G tmpfs /mnt/ram

Quick Reference

Command Description
mount Show all mounted filesystems
mount device mountpoint Mount device to mountpoint
mount -t type device mountpoint Mount with specific filesystem type
mount -o options device mountpoint Mount with specific options
mount -a Mount all filesystems in /etc/fstab
mount -o remount,options mountpoint Change mount options without unmounting
umount mountpoint Unmount filesystem
umount -l mountpoint Lazy unmount (when busy)
findmnt Tree view of mounted filesystems
lsblk -f List block devices with filesystem info
blkid Show UUID and filesystem type
df -h Show disk usage of mounted filesystems