mount 命令详解与参数实测(Ubuntu 22.04)

mount 命令逐参数实测详解:无参数查看挂载表、-t 指定类型与 UUID 挂载、tmpfs 内存盘、--bind 绑定挂载、-o ro/remount 只读与在线切换、-a 配合 /etc/fstab 批量挂载,以及重复挂载、未知文件系统与坏超级块场景,附 ext4/xfs/btrfs/f2fs 对比,全部命令与回显在 Ubuntu 22.04(util-linux 2.37.2)实测通过。

Linux命令Linux系统UbuntuUbuntu 22.04命令行文件管理系统信息2026/10/15

mount 命令详解与参数实测(Ubuntu 22.04)

目标:全面掌握 mount 命令的核心用法——无参数查看挂载表、-t 指定文件系统类型与 UUID 挂载、tmpfs 内存盘、--bind 绑定挂载、-o 只读与 remount 在线切换、-a 配合 /etc/fstab 批量挂载,以及挂载点不存在、同设备重复挂载、未知文件系统、坏超级块四类场景的真实行为,全部命令与回显已在 Ubuntu 22.04(util-linux 2.37.2)实测通过。

一、环境信息

  • 操作系统:Ubuntu 22.04.5 LTS(x86_64)
  • 内核版本:6.8.0-138-generic(uname -r 实测)
  • mount 版本:util-linux 2.37.2(mount --version 实测)
  • 测试介质:/root/demo/img/ext4.img(dd 创建的 64 MiB 映像文件)→ losetup 关联到 /dev/loop17 → mkfs.ext4(e2fsprogs 1.46.5)格式化 → 挂载到 /mnt/ext4-demo
  • 说明:用映像文件 + 回环设备模拟块设备,mount 的行为与真实磁盘完全一致,且不会影响系统已有分区

二、命令概览

2.1 语法与实测环境准备

mount 的基本用法有四种形态:mount [-lhV] 查看挂载表;mount -a [options] 按配置批量挂载;mount [options] <source> <directory> 把设备挂到目录;mount [options] --bind <dir> <dir> 做子树绑定。本文用一个 64 MiB 的映像文件模拟块设备,先完成格式化准备:

💡 技术拓展:"让文件系统在某个目录可用"有几种不同挂载途径,横向对比——

方式原理/职责适用场景
mount 手动挂载命令行即时挂载,重启失效临时测试盘、一次性介质
/etc/fstab开机时按配置表自动挂载固定磁盘/分区的持久挂载
systemd .mount 单元以 unit 描述挂载点,依赖管理更强systemd 服务器上替代 fstab
autofs按需触发挂载、空闲自动卸载NFS 等慢速/间歇访问的远端目录

本文全部实测走 mount 手动挂载:行为最直接、便于逐参数观察;持久化方案是 fstab,属另一专题。

$ mkdir -p /root/demo/img

$ dd if=/dev/zero of=/root/demo/img/ext4.img bs=1M count=64
64+0 records in
64+0 records out
67108864 bytes (67 MB, 64 MiB) copied, 0.0463182 s, 1.4 GB/s

$ losetup -f --show /root/demo/img/ext4.img
/dev/loop17

$ mkfs.ext4 -F /dev/loop17
mke2fs 1.46.5 (30-Dec-2021)
Discarding device blocks:     0/16384           done
Creating filesystem with 16384 4k blocks and 16384 inodes

Allocating group tables: 0/1   done
Writing inode tables: 0/1   done
Creating journal (1024 blocks): done
Writing superblocks and filesystem accounting information: 0/1   done


$ blkid /dev/loop17
/dev/loop17: UUID="ceea7d13-3f61-4a27-8d31-1149ce7fcbfe" BLOCK_SIZE="4096" TYPE="ext4"

映像经 losetup 变成块设备 /dev/loop17,mkfs.ext4 写入 ext4 文件系统,blkid 确认 UUID 就绪。后文所有挂载实验都基于这块"盘"。

2.2 --help 全量回显

mount --help 输出全部参数清单(实测原样保留,可作为参数速查):

Usage:
 mount [-lhV]
 mount -a [options]
 mount [options] [--source] <source> | [--target] <directory>
 mount [options] <source> <directory>
 mount <operation> <mountpoint> [<target>]

Mount a filesystem.

Options:
 -a, --all               mount all filesystems mentioned in fstab
 -c, --no-canonicalize   don't canonicalize paths
 -f, --fake              dry run; skip the mount(2) syscall
 -F, --fork              fork off for each device (use with -a)
 -T, --fstab <path>      alternative file to /etc/fstab
 -i, --internal-only     don't call the mount.<type> helpers
 -l, --show-labels       show also filesystem labels
 -n, --no-mtab           don't write to /etc/mtab
     --options-mode <mode>
                         what to do with options loaded from fstab
     --options-source <source>
                         mount options source
     --options-source-force
                         force use of options from fstab/mtab
 -o, --options <list>    comma-separated list of mount options
 -O, --test-opts <list>  limit the set of filesystems (use with -a)
 -r, --read-only         mount the filesystem read-only (same as -o ro)
 -t, --types <list>      limit the set of filesystem types
     --source <src>      explicitly specifies source (path, label, uuid)
     --target <target>   explicitly specifies mountpoint
     --target-prefix <path>
                         specifies path used for all mountpoints
 -v, --verbose           say what is being done
 -w, --rw, --read-write  mount the filesystem read-write (default)
 -N, --namespace <ns>    perform mount in another namespace

 -h, --help              display this help
 -V, --version           display version

Source:
 -L, --label <label>     synonym for LABEL=<label>
 -U, --uuid <uuid>       synonym for UUID=<uuid>
 LABEL=<label>           specifies device by filesystem label
 UUID=<uuid>             specifies device by filesystem UUID
 PARTLABEL=<label>       specifies device by partition label
 PARTUUID=<uuid>         specifies device by partition UUID
 ID=<id>                 specifies device by udev hardware ID
 <device>                specifies device by path
 <directory>             mountpoint for bind mounts (see --bind/rbind)
 <file>                  regular file for loopdev setup

Operations:
 -B, --bind              mount a subtree somewhere else (same as -o bind)
 -M, --move              move a subtree to some other place
 -R, --rbind             mount a subtree and all submounts somewhere else
 --make-shared           mark a subtree as shared
 --make-slave            mark a subtree as slave
 --make-private          mark a subtree as private
 --make-unbindable       mark a subtree as unbindable
 --make-rshared          recursively mark a whole subtree as shared
 --make-rslave           recursively mark a whole subtree as slave
 --make-rprivate         recursively mark a whole subtree as private
 --make-runbindable      recursively mark a whole subtree as unbindable

For more details see mount(8).

💡 说明:帮助把参数分成三组——Options(行为选项)、Source(设备来源,支持路径/LABEL/UUID/PARTUUID 等多种写法)、Operations(子树操作)。最常用的是 -t、-o、-a 与 --bind,下文逐一实测。

2.3 版本确认

$ mount --version
mount from util-linux 2.37.2 (libmount 2.37.2: selinux, smack, btrfs, verity, namespaces, assert, debug)

三、参数逐项实测

3.1 无参数:列出当前挂载表

不带任何参数执行 mount,打印当前命名空间内全部挂载条目(输出节选(实测):首行根文件系统条目与本文主题无关,已省略,其余条目原样保留):

$ mount
proc on /proc type proc (rw,nosuid,nodev,noexec,relatime)
tmpfs on /dev type tmpfs (rw,nosuid,size=65536k,mode=755,inode64)
devpts on /dev/pts type devpts (rw,nosuid,noexec,relatime,gid=5,mode=620,ptmxmode=666)
sysfs on /sys type sysfs (rw,nosuid,nodev,noexec,relatime)
cgroup on /sys/fs/cgroup type cgroup2 (rw,nosuid,nodev,noexec,relatime,nsdelegate,memory_recursiveprot)
mqueue on /dev/mqueue type mqueue (rw,nosuid,nodev,noexec,relatime)
shm on /dev/shm type tmpfs (rw,nosuid,nodev,noexec,relatime,size=65536k,inode64)
/dev/sda3 on /etc/hostname type ext4 (rw,relatime,errors=remount-ro)
/dev/sda3 on /etc/hosts type ext4 (rw,relatime,errors=remount-ro)
/dev/sda3 on /etc/resolv.conf type ext4 (rw,relatime,errors=remount-ro)

每行格式固定为 源 on 挂载点 type 文件系统 (选项)。这条命令也是排障起点:怀疑某个目录被谁占用、某文件系统是否带上了目标选项,先 mount | grep 关键字。

3.2 -t:指定文件系统类型挂载(设备名源 + UUID 源)

-t 显式声明文件系统类型,配合设备路径挂载:

$ mkdir -p /mnt/ext4-demo

$ mount -t ext4 /dev/loop17 /mnt/ext4-demo

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

$ echo 'hello mount' > /mnt/ext4-demo/hello.txt

$ cat /mnt/ext4-demo/hello.txt
hello mount

挂载成功后立刻可读写。设备来源除了路径,还支持 UUID= / LABEL= 写法(帮助中的 Source 组)——UUID 在设备名漂移时更可靠:

$ mkdir -p /mnt/by-uuid

$ mount UUID=ceea7d13-3f61-4a27-8d31-1149ce7fcbfe /mnt/by-uuid

$ findmnt /mnt/by-uuid
TARGET       SOURCE      FSTYPE OPTIONS
/mnt/by-uuid /dev/loop17 ext4   rw,relatime

$ umount /mnt/by-uuid

💡 说明:UUID 方式挂载后 findmnt 的 SOURCE 列仍解析为 /dev/loop17——内核按设备记账,UUID 只是用户态的定位方式。UUID 可用 blkid -o value -s UUID /dev/loop17 取得(见 blkid 篇实测)。

3.3 -t tmpfs:内存文件系统与容量上限

tmpfs 是纯内存文件系统,-o size= 限定容量上限,超写即报错:

$ mkdir -p /mnt/tmpfs-demo

$ mount -t tmpfs tmpfs /mnt/tmpfs-demo -o size=32m

$ df -h /mnt/tmpfs-demo
Filesystem      Size  Used Avail Use% Mounted on
tmpfs            32M     0   32M   0% /mnt/tmpfs-demo

$ dd if=/dev/zero of=/mnt/tmpfs-demo/big bs=1M count=40
dd: error writing '/mnt/tmpfs-demo/big': No space left on device
33+0 records in
32+0 records out
33554432 bytes (34 MB, 32 MiB) copied, 0.0139028 s, 2.4 GB/s

$ rm -f /mnt/tmpfs-demo/big

$ umount /mnt/tmpfs-demo

要写 40 MiB,写到 32 MiB 即 No space left on device——容量上限真实生效。tmpfs 内容随卸载与重启消失,适合临时缓存、构建工作目录。

3.4 --bind:绑定挂载

--bind 把已有目录(及其内容)映射到另一个路径,两个路径是同一份数据的两个入口:

$ mkdir -p /mnt/bind-demo

$ mount --bind /mnt/ext4-demo /mnt/bind-demo

$ findmnt /mnt/bind-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/bind-demo /dev/loop17 ext4   rw,relatime

$ echo via-bind > /mnt/bind-demo/via-bind.txt

$ cat /mnt/ext4-demo/via-bind.txt
via-bind

$ umount /mnt/bind-demo

从 bind 入口写入的 via-bind.txt,从原路径立即可见。典型用途:给进程暴露某个子目录、为构建环境注入目录视角。

3.5 -o ro:只读挂载

-o 接逗号分隔的选项列表,ro 表示只读。先卸载再用 ro 重新挂载:

$ umount /mnt/ext4-demo

$ mount -o ro /dev/loop17 /mnt/ext4-demo

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   ro,relatime

$ cat /mnt/ext4-demo/hello.txt
hello mount

$ touch /mnt/ext4-demo/blocked.txt
touch: cannot touch '/mnt/ext4-demo/blocked.txt': Read-only file system

读正常,写被内核以 Read-only file system 拒绝。-o ro 与 -r 等价。

3.6 -o remount:在线切换读写

remount 不重新指定设备,只改已有挂载的选项,是最常用的在线变更手段:

$ mount -o remount,rw /mnt/ext4-demo

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

$ touch /mnt/ext4-demo/rw-back.txt

$ ls /mnt/ext4-demo
hello.txt
lost+found
rw-back.txt
via-bind.txt

$ mount -o remount,ro /mnt/ext4-demo

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   ro,relatime

$ touch /mnt/ext4-demo/again.txt
touch: cannot touch '/mnt/ext4-demo/again.txt': Read-only file system

$ mount -o remount,rw /mnt/ext4-demo

$ findmnt /mnt/ext4-demo
TARGET         SOURCE      FSTYPE OPTIONS
/mnt/ext4-demo /dev/loop17 ext4   rw,relatime

remount,rw 后立即恢复写入(rw-back.txt 创建成功),remount,ro 又立即只读。remount 只需要挂载点路径,不需要设备名。

3.7 -a 与 /etc/fstab:批量挂载

-a 按 /etc/fstab 挂载其中"尚未挂载"的条目。先往 fstab 追加一条 tmpfs 记录:

$ cp /etc/fstab /root/fstab.bak

$ printf 'tmpfs /mnt/fstab-demo tmpfs size=16m,mode=0755 0 0\n' >> /etc/fstab

$ cat /etc/fstab
# UNCONFIGURED FSTAB FOR BASE SYSTEM
tmpfs /mnt/fstab-demo tmpfs size=16m,mode=0755 0 0

$ mkdir -p /mnt/fstab-demo

$ mount -a

$ findmnt /mnt/fstab-demo
TARGET          SOURCE FSTYPE OPTIONS
/mnt/fstab-demo tmpfs  tmpfs  rw,relatime,size=16384k,mode=755,inode64

$ umount /mnt/fstab-demo

$ cp /root/fstab.bak /etc/fstab

fstab 每行六个字段:设备、挂载点、类型、选项、dump 频率、fsck 顺序。mount -a 把未挂载的条目一次性挂上——fstab 里写错的行会在这一步集中报错,因此改完 fstab 先跑一次 mount -a 验证再重启。

3.8 错误场景:挂载点不存在 / 同设备重复挂载 / 未知文件系统 / 坏超级块

挂载点目录不存在时直接失败:

$ mount /dev/loop17 /mnt/ghost
mount: /mnt/ghost: mount point does not exist.

同一设备再挂到第二个目录——实测成功且共享同一文件系统实例:

$ mkdir -p /mnt/again-demo

$ mount -t ext4 /dev/loop17 /mnt/again-demo

$ findmnt /mnt/again-demo
TARGET          SOURCE      FSTYPE OPTIONS
/mnt/again-demo /dev/loop17 ext4   rw,relatime

$ cat /mnt/again-demo/hello.txt
hello mount

💡 说明:二次挂载没有报错,findmnt 确认挂载成功,且从新视图能读到此前写入的 hello.txt——两个挂载点共享同一文件系统实例。这虽是内核允许的行为,生产上出现通常意味着误操作,挂载前建议先用 findmnt 检查(演示后已将 again-demo 卸载)。

对无法识别的文件系统类型,报错直白:

$ umount /mnt/ext4-demo

$ mount -t bogusfs /dev/loop17 /mnt/again-demo
mount: /mnt/again-demo: unknown filesystem type 'bogusfs'.

用随机数据制造坏超级块(未格式化的设备):

$ losetup -d /dev/loop17

$ dd if=/dev/urandom of=/root/demo/img/bad.img bs=1M count=8
8+0 records in
8+0 records out
8388608 bytes (8.4 MB, 8.0 MiB) copied, 0.0439316 s, 191 MB/s

$ losetup -f --show /root/demo/img/bad.img
/dev/loop17

$ mkdir -p /mnt/bad-demo

$ mount /dev/loop17 /mnt/bad-demo
mount: /mnt/bad-demo: wrong fs type, bad option, bad superblock on /dev/loop17, missing codepage or helper program, or other error.

$ losetup -d /dev/loop17

四类场景四种表现:mount point does not exist(先建目录)、静默成功(重复挂载,需自查)、unknown filesystem type(类型名拼错或内核不支持)、bad superblock(设备未格式化或损坏)。

四、实战示例

实战一:临时块设备的挂载与回收闭环

映像文件 → 回环设备 → 格式化 → 挂载 → 用完卸载并释放,一条链路(各步回显见 2.1 与 3.8):

dd if=/dev/zero of=/root/demo/img/ext4.img bs=1M count=64   # 造映像
losetup -f --show /root/demo/img/ext4.img                    # 关联回环设备,返回 /dev/loop17
mkfs.ext4 -F /dev/loop17                                     # 格式化
mkdir -p /mnt/ext4-demo && mount -t ext4 /dev/loop17 /mnt/ext4-demo
# ……使用文件系统……
umount /mnt/ext4-demo                                        # 卸载
losetup -d /dev/loop17                                       # 释放回环设备

最后两步缺一不可:umount 只卸文件系统,losetup -d 才把回环设备还回去(详见 umount 篇)。

实战二:用 UUID 固定挂载关系

设备名(/dev/sdb1 等)在插拔顺序变化后会漂移,UUID 不会。取 UUID 后直接挂载(回显见 3.2):

blkid -o value -s UUID /dev/loop17        # 取 UUID
mount UUID=ceea7d13-3f61-4a27-8d31-1149ce7fcbfe /mnt/by-uuid

写进 /etc/fstab 时同样以 UUID= 开头,重启后挂载关系稳定。

实战三:给临时任务一块内存盘

构建、测试等场景需要高速临时目录,用 tmpfs 并限死容量防止吃光内存(回显见 3.3):

mkdir -p /mnt/tmpfs-demo
mount -t tmpfs tmpfs /mnt/tmpfs-demo -o size=32m
df -h /mnt/tmpfs-demo        # 确认容量生效

五、注意事项与常见问题

Q1:报 "bad superblock" 类错误怎么排查? 按顺序检查:设备是否已格式化(blkid /dev/loop17 无输出即没有可识别的文件系统)、类型是否拼错、超级块是否损坏。dmesg | tail 通常能看到内核侧的具体原因;确认损坏后用对应 fsck 工具修复或重新 mkfs。本篇 3.8 用随机数据映像复现了该报错。

Q2:同一设备挂到第二个挂载点会失败吗? 实测不会(见 3.8):第二次挂载静默成功,两个挂载点共享同一文件系统实例,任一视图写入对方立即可见。这通常不是想要的行为,挂载前先 findmnt /mnt/目标 确认目标目录空闲。

Q3:挂载点目录里原有文件去哪了? 挂载后原内容被"遮蔽"——隐藏但未删除,卸载后自动恢复。因此挂载应使用专用空目录;如果卸载后发现"文件丢了",先检查是否仍有别的挂载叠在该目录上。

Q4:不写 -t 和写 -t 有什么区别? 不写 -t 时 mount 依赖 libblkid 探测文件系统类型;写 -t 则跳过探测按指定类型直接挂。对正常设备两者等价;对内容无法识别的设备(如坏超级块),不写 -t 的报错正是探测失败的表现(3.8 实测)。

常用文件系统选型参考

文件系统关键特性典型场景格式化命令
ext4成熟稳定、日志、延迟分配,Linux 默认通用场景首选mkfs.ext4
xfs高并发、大文件吞吐强、在线扩容大文件存储、数据库mkfs.xfs
btrfs写时复制、快照、子卷、校验和需要快照/回滚的场景mkfs.btrfs
f2fs面向闪存的日志结构设计SSD/eMMC/SD 卡mkfs.f2fs

一句话选型:无特殊需求用 ext4;需要快照与子卷选 btrfs;大文件高吞吐选 xfs;纯闪存设备可评估 f2fs。

六、参考链接