avatar🌌
TangentTangent Blog

技术博客 | Go & Kubernetes & Linux

技术博客 | Go & Kubernetes & Linux

Linux mount Command Explained: From Basic Mounting to Container Storage

In Linux systems, mount is an extremely fundamental command. Unlike Windows, which automatically assigns drive letters, Linux organizes all files and directories into a single inverted tree structure that starts at the root directory /. To access a new storage device, you must manually mount it somewhere in this tree.

1. What mount Does ​

Simply put, mount is used to "attach" a filesystem.

When you connect a new hard disk, USB drive, or network storage to a Linux system, the system does not automatically assign a drive letter. You need to connect that storage device to an empty directory in the directory tree. This process is called mounting, and that directory is called a mount point.

With mount, you tell the system: "Please present the filesystem on this device under this specific directory."

2. mount Parameters ​

Full syntax of mount:

bash
mount [-t filesystem_type] [-o mount_options] <device_name> <mount_point>

Core Parameters ​

ParameterMeaningNotes
-tFilesystem typeSpecifies the filesystem type on the device, such as ext4, xfs, vfat, ntfs, nfs, iso9660, etc. Linux can usually detect it automatically, so this can often be omitted
-oMount optionsMultiple options can be separated by commas: ro (read-only), rw (read-write), remount (remount), loop (mount a regular file as a block device)
-aMount allReads /etc/fstab and mounts all devices that are not yet mounted. This is executed automatically at boot

Common Examples ​

bash
mkdir /mnt/usb
mount /dev/sdb1 /mnt/usb

mount -t ext4 /dev/sdb1 /mnt/data

mount -o ro /dev/sdb1 /mnt/readonly

mount -o loop /path/to/image.iso /mnt/iso

mount --bind /home/data /var/www/html

3. Filesystem Types Explained ​

The filesystem type specified with -t is essentially telling the kernel which "data organization rule" the device uses. Common types can be grouped into four categories:

1. Native Linux filesystems ​

FilesystemCharacteristicsTypical use
ext4Most classic and widely used; stable and mature. Default on Ubuntu/DebianGeneral-purpose servers and personal computers
xfsHigh-performance journaling filesystem; excels with large files and high concurrency. Default on CentOS/RHELDatabase servers, large-scale storage
btrfsCopy-on-write (CoW), with native snapshots, transparent compression, and multi-device managementScenarios requiring frequent backup and rollback

2. Cross-platform and removable-device filesystems ​

FilesystemCharacteristicsNotes
vfat (FAT32)Best compatibility; almost all devices can read/writeDoes not support single files larger than 4GB
exfatUpgraded FAT32, removes the 4GB limitationPreferred for large USB drives and cross-platform removable disks
ntfsDefault format on WindowsLinux is read-only by default; ntfs-3g is needed for read/write

3. Network filesystems ​

FilesystemCharacteristicsTypical use
nfsStandard protocol for sharing directories between Linux systemsFile sharing between Linux/Unix servers
cifsBased on SMB protocolMounting Windows shared folders on Linux

4. Special and in-memory filesystems ​

FilesystemCharacteristicsTypical use
tmpfsMemory-based, very fast, data disappears after reboot/tmp temporary directory, high-speed cache
overlayUnion filesystem that stacks multiple directories like layersCore storage technology in Docker containers
iso9660CD/DVD filesystemMounting .iso image files

4. Viewing the Filesystem a File Belongs To ​

One concept needs clarification: an individual file itself does not have a "filesystem". Filesystems apply to disk partitions or mount points. To see the filesystem type of the partition containing a file, you can use the following methods:

df -T: most intuitive ​

bash
$ df -T /var/log/syslog
Filesystem     Type  1K-blocks    Used Available Use% Mounted on
/dev/sda2      ext4   50212344 1234567  46000000   5% /

The Type column is the filesystem type, and Mounted on is the mount point.

findmnt: most professional ​

bash
$ findmnt --target /var/log/syslog
TARGET SOURCE    FSTYPE OPTIONS
/      /dev/sda2 ext4   rw,relatime,errors=remount-ro

lsblk -f: global overview ​

bash
$ lsblk -f

Shows all disks, partitions, filesystem types, and mount points in a tree structure.

5. OS Concepts Behind mount ​

1. Everything is a file ​

In Linux, hardware devices are also abstracted as files and placed under /dev:

  • Block devices: disks like /dev/sda, USB drives like /dev/sdb, etc., read/written in blocks. The essence of mount is mapping a block device file to a directory.
  • Shadowing effect: when mounting onto a non-empty directory, existing files are "hidden" (not deleted) and become visible again after umount.

2. Virtual File System (VFS) ​

The Linux kernel implements the VFS (Virtual File System) abstraction layer, providing a unified system call interface (open(), read(), write()) to upper-layer applications. Whether the underlying filesystem is ext4, NTFS, or NFS, VFS calls the corresponding filesystem driver, so users do not need to care about the differences.

┌─────────────────────────────┐
│   User programs (open/read) │
├─────────────────────────────┤
│       VFS abstraction       │
├──────┬──────┬───────┬───────┤
│ ext4 │ xfs  │ ntfs  │  nfs  │  ← Filesystem drivers
├──────┴──────┴───────┴───────┤
│     Block devices / network │
└─────────────────────────────┘

3. /etc/fstab: automatic mounts at boot ​

At startup, the system uses the /etc/fstab (File System Table) configuration file to determine mount policy, and automatically executes mount -a:

UUID=xxxx-xxxx-xxxx                    /             ext4   defaults    1      1
UUID=yyyy-yyyy-yyyy                    /home         ext4   defaults    0      2

4. Kernel state tracking ​

When you run mount, it actually sends a system call to the kernel. The kernel records mount state in memory, which you can inspect as follows:

bash
cat /proc/mounts

mount

6. Advanced: Root Switching and Container Isolation ​

Turning a directory into a new root is one of the core operations in container technology.

chroot: classic root switching ​

bash
sudo chroot /home/user/my_new_root /bin/bash
  • In the new bash process, / actually points to /home/user/my_new_root on the host
  • Prerequisite: the target directory must include a basic Linux filesystem structure (/bin/bash and its dependent dynamic libraries under /lib)
  • Security limitation: it only changes path resolution and does not provide true isolation; escape risks exist

pivot_root + Mount Namespace: real isolation ​

This is the mechanism used by modern container runtimes (such as runc):

  1. Mount Namespace: creates an isolated mount tree; mount operations inside it do not affect the host
  2. pivot_root: replaces the root mount with a specified directory; the old root is "pushed" to a temporary location and then unmounted
bash
sudo unshare --mount --pid --fork --root=/home/user/my_new_root /bin/bash

mount -t proc proc /proc

For detailed usage of the unshare command, see Linux unshare Command Explained.

Key detail: pseudo filesystems are not created automatically ​

After entering a new namespace, pseudo filesystems such as /proc and /sys from the host will not appear automatically, nor will they be created automatically. Why:

  • --root locks the process’s "world boundary" inside the new directory; the host’s /proc is outside that boundary
  • The kernel only provides an isolated "mount ledger" and does not proactively populate anything
  • If only Mount Namespace is isolated but PID Namespace is not, a manually mounted /proc will still show all host processes

To achieve full container-level isolation (where /proc only shows the process itself), you need to isolate both Mount and PID namespaces and include the --fork option.

Linux unshare Command Explained: From Namespaces to DIY Containers
Go JSON Library Performance Comparison: Std JSON V1/V2 vs Sonic
由 Valaxy v1.2.2 驱动|主题-Yunv1.2.2