The Linux mount command allows attaching external file systems and network shares into a system‘s directory tree. As a Linux administrator, fully grasping mounting is crucial for working with storage, configuring networks, deploying containers, and more.

In this advanced 3200+ word guide, you will gain an in-depth understanding of the Linux mount command. While covering the basics, we also dive into more complex mount scenarios like bind mounts, network file shares, disk images, and automated mounting. We also explore relevant storage topics like Linux virtual filesystem concepts, disk partitioning, and storage terminology.

After reading, you will have the deep Linux storage wisdom necessary to mount like a expert. Let‘s get started!

Storage Terminology

Before diving into mount, it helps to define some common storage terms:

File System: Provides an organized structure for storing/finding files and directories. Common Linux file systems are Ext4, XFS, Btrfs and more.

Mount: Attaches a file system to the main system directory tree allowing access to its contents.

fstab: Stores mount configurations for mounting filesystems on boot up.

Partition: Logically separates an individual accessible region on a disk device.

Disk Image: Single file containing full file system including boot information and contents. Common disk image types are ISO, VMDK, and Docker container images.

Mount Command Overview

The mount command in Linux mounts file systems, disks, and network shares into the main system directory tree. This allows interacting with the contents transparently as if residing on the local hard drive.

Some examples include:

  • Mounting additional drives like USB, DVDs, SD cards
  • Making available network shares from NFS, CIFS for remote access
  • Attaching disk images including ISOs, VMDKs, and container layers
  • Binding particular directories in complex scenarios

After mounting resources, they are assimilated into the main Linux file system.

Now let‘s explore mount and mounting in more depth.

Linux File System Concepts

It helps to understand two key concepts:

  1. The main Linux file system tree originating from /
  2. Everything else mounts into this unified structure

For example, on Linux systems the root file system / resides on a physical partition. But /home or /var may mount on separate disk partitions logically grafted into the root directory tree after booting.

Even system directories under /dev, /proc, /sys representing devices, processes and sysfs exist as virtual filesystems without a concrete disk location.

This is the power of the unified Linux file system namespace. Different physical and virtual storage resources integrate together on the same system.

The Linux mount command facilitates this by hooking filesystems into this main directory tree residing at /.

Mount Command Syntax

The basic syntax of mount is:

mount -t [file system type] [device] [directory]

Let‘s break this down:

  • -t: File system type (ext4, xfs, nfs4)
  • [device]: Block device, image file, network share URL
  • [directory]: Empty directory serving as mount point

For example:

# Mount /dev/sdb1 to /mnt/disks 
mount -t ext4 /dev/sdb1 /mnt/disks

This mounts the ext4 file system on partition /dev/sdb1 onto the local /mnt/disks empty directory.

Now let‘s explore additional options.

Mount Options

The mount command supports numerous options enhancing mounting functionality:

  • -a: Mount all mounts defined in /etc/fstab
  • -f: Mount forcibly without block device checks
  • -r: Mount read-only instead of read-write
  • -w: Explicit read-write (default behavior)
  • -L: Mount by filesystem label instead of device path
  • -U: Mount by UUID instead of device path
  • -O: Specify filesystem mount options (comma separated)
  • -B: Mount without updating /etc/mtab
  • -l: List devices already mounted
  • -h: Print help

For example, mounting by label instead of raw device path:

# Mount by label
mount -L mydrive /mnt/disks 

And mounting read-only:

# Readonly mount 
mount -r -L mydrive /mnt/disks

These are just some of the most common mount options. There are many more documented in the manuals (e.g. man mount).

Now let‘s walk through some examples of using the mount command in practice.

Mounting by UUID vs. Device Path

When mounting devices, you can directly reference the fixed device path like /dev/sdb1. However this exact path can change between reboots causing mounting errors.

A more reliable approach is mounting by universally unique identifier (UUID) or filesystem label using -L or -U.

First find the UUID:

ls -l /dev/disk/by-uuid/

Then provide this during mounting:

# Mount by UUID
mount -U 7733-A442 /mnt/data

This mounts the filesystem with matching UUID to /mnt/data no matter if the actual /dev/sdb1 path changes in the future.

Mounting Network Shares and Remote File Systems

In addition to physical devices, we can also mount remote filesystems like NFS and Samba/CIFS Windows shares.

For example, first create the destination:

mkdir /mnt/mynas

Then mount an NFSv4 share by hostname:

mount -t nfs4 nas01:/shared /mnt/mynas

We could also mount a CIFS Windows share:

mount -t cifs //windowsserver/share /mnt/windows -o username=user

There are many advanced options for tuning and access control when mounting remote network file systems. Consult distro documentation for further guidance.

Bind Mounts

Bind mounts provide advanced functionality for creating custom views combining multiple mount points.

For example, imagine we have two separate disk partitions already mounted on /mnt/disk1 and /mnt/disk2.

We can create a bind mount merging both partitions together at a new mount point like so:

mount --bind /mnt/disk1 /mnt/mergedisks
mount --bind /mnt/disk2 /mnt/mergedisks

Now /mnt/mergedisks contains the combined contents from both disk1 and disk2 partition mounts.

Bind mounts are incredibly powerful for creating custom nested mount structures.

Mounting Disk Images

In addition to physical storage devices, we can mount disk images like ISO files.

First, confirm loop device support is enabled on your Linux distribution. Then we create a mount point and mount the ISO specifying the loop device -o loop:

# Mount ISO 
mkdir /mnt/iso
mount -o loop myimage.iso /mnt/iso 

We can now interact with the ISO content at /mnt/iso.

This also works for other disk image formats like VMDK.

Shared Subdirectories

Linux allows directly mounting sub-directories instead of full mount roots.

For example, imagine we have a NAS with the following shares:

NAS01
|- apps
|- docs
   |- invoices
   |- statements
|- media
   |- movies
   |- photos

Instead of mounting the entire NAS, we can mount just the docs subfolder:

# Only mount docs share itself
mount nas01:/docs /mnt/nasdocs

Now /mnt/nasdocs only contains the invoices and statements folders.

This allows mounting just slices of remote shares including sub-directories.

Automounting Drives

Manually mounting drives on insertion starts to become cumbersome at scale.

Autofs provides automatic mounting capabilities for attached media like USB and CD-ROM drives. When inserted, they automatically mount to /media without any intervention.

For example:

/media
|- /sda1  (mounted USB drive)
|- /cdrom (mounted CD-ROM)

Consult distro documentation to enable automounting if not already active by default.

Persistent Mounts via /etc/fstab

Instead of manually specifying mounts, we can define persistent mounts in /etc/fstab.

This file lists mounts to process during system boot. For example:

# /etc/fstab

UUID=8d02ee17-130e /mnt/data ext4 defaults 0 0
//server/share /mnt/cifs cifs credentials=/etc/samba/creds 0 0 

Now /mnt/data and the CIFS share mount automatically on every reboot via /etc/fstab.

Unmounting and Detaching Mounts

We covered several ways to mount resources, so how do you unmount them?

Use the umount command referencing either the original device or mount point:

# Unmount by device  
umount /dev/sdb1

# Unmount by mount point
umount /mnt/disks

Storage devices should always be safely unmounted before removal to prevent data loss or filesystem damage.

Understanding Mounts

In Linux, mount grafts file systems into the main directory tree located at /. Mount points create access portals into external filesystem resources.

Learning to proficiently mount storage media, remote shares, disk images, and custom bind structures is essential for any Linux administrator. Automounting and defining mounts in /etc/fstab improves ease of use.

For additional practice, set up a Linux VM and start experimenting with mount techniques covered in this guide. Let us know what topics you would like to see us tackle next!

Similar Posts