温馨提示×

linux mount绑挂如何做

小樊
71
2026-10-05 07:13:05
栏目: 智能运维

在 Linux 中,“绑挂”(bind mount)是指把一个已经挂载的目录或文件,再挂载到另一个位置,让两个路径看到相同的内容。常用于容器、chroot、共享目录等场景。

下面按常见用法说明。


一、基本 bind mount(目录)

1️⃣ 准备目录

mkdir /source
mkdir /target

2️⃣ 执行绑定挂载

mount --bind /source /target

此时:

  • /target 的内容会变成 /source
  • 对 /target 的修改会同步到 /source

二、查看 bind mount

mount | grep bind

或

findmnt

三、卸载 bind mount

umount /target

四、只读 bind mount

mount --bind /source /target
mount -o remount,ro /target

或一次性:

mount --bind -o ro /source /target

五、bind mount 单个文件

touch /etc/config.conf
cp /etc/config.conf /tmp/config.conf

mount --bind /etc/config.conf /tmp/config.conf

常用于:

  • 替换配置文件
  • 容器注入配置

六、开机自动 bind mount(/etc/fstab)

目录

/source /target none bind 0 0

文件

/source/file /target/file none bind 0 0

七、与 mount namespace 的关系(进阶)

bind mount 默认只在当前 mount namespace 生效,常见于:

  • Docker
  • unshare
  • systemd-nspawn

示例:

unshare --mount --fork bash
mount --bind /source /target

八、常见坑

⚠️ bind mount 不会递归挂载子挂载点

mount --bind /mnt /target
# /mnt/usb 不会自动出现在 /target/usb

如需递归:

mount --rbind /mnt /target

如果你有具体场景(如 Docker、chroot、NFS、容器、systemd),可以告诉我,我可以给更针对性的示例。

0 踩