mount - Linux


Overview

The mount command in Linux is used to attach file systems to a specific directory in the file system hierarchy. This command facilitates access to files on different devices, allowing the system to treat them as if they are located within the directory structure. Most commonly used during system boots to set up initial file systems or when adding new devices like hard drives, USBs, or mounted network locations.

Syntax

The basic syntax for the mount command is:

mount [options] <device> <directory>
  • <device>: The name of the device or filesystem to mount.
  • <directory>: The directory where the filesystem is to be mounted.

You can also use mount without any parameters to display all currently mounted filesystems.

Options/Flags

  • -r or --read-only: Mount the filesystem read-only.
  • -w or --read-write: Mount the filesystem read-write (default if not specified).
  • -t <type>: Specify the filesystem type. Common types include ext4, ntfs, vfat, xfs.
  • -o <options>: Used to pass specific options with a comma-separated string. Common options include:
    • defaults: Mounts with default options.
    • ro: Read-only mount.
    • rw: Read-write mount.
    • uid: User ID to associate with file accesses.
    • gid: Group ID to associate with file accesses.
  • -a: Mount all filesystems defined in /etc/fstab.

Examples

Mounting a USB Drive:

mount -t vfat /dev/sdb1 /media/usb

Mounting an NFS Share:

mount -t nfs server:/path /mnt/nfs

Mounting a filesystem read-only:

mount -o ro /dev/sda1 /data

Unmounting a Device:

umount /media/usb

Common Issues

  • Device is busy: When trying to unmount a filesystem, make sure no files are being accessed. Use lsof +D <directory> to find open files.
  • Unknown filesystem type: Ensure you have the correct type and it’s supported on your system; check with less /proc/filesystems.
  • Permission errors: mount typically requires root privileges; using sudo can resolve this.

Integration

Combining mount with other commands can automate mounting tasks. For example, a script to check disk space and mount an additional device could look like this:

#!/bin/bash
if [ $(df /data | awk '{print $5}' | tail -n 1 | sed 's/%//') -gt 90 ]; then
   mount /dev/sdb1 /data/extra
fi
  • umount: Unmounts filesystems.
  • df: Reports file system disk space usage.
  • lsblk: Lists information about all available or the specified block devices.
  • fstab: Static information about the filesystems.

For more detailed information, you can refer to the official mount and umount man pages.