Part 5 of 7 · 2 min read
Bind mounts
Think of a window between two rooms. You see the same view from both sides, and the view does not exist twice. The analogy stops in one place: a window shows, and a bind mount lets you also change what is behind it, if the permissions allow.
In plain words: A bind mount makes an existing directory (or file) appear at a second place in the tree. It does not copy anything. Both paths lead to the same inodes. On the test machine, mount --bind mnt bind made the contents of mnt visible under bind, and the file lost+found showed device 1792 and inode 11 at both paths. The kernel lists all mounts in /proc/self/mountinfo, with the mount point, the file system type and the options. The test machine's table had 27 lines. Containers (Level 11) use bind mounts to give a program a few chosen directories.
A worked example. The output of stat -c 'dev %d inode %i %n' for the two paths was:
dev 1792 inode 11 mnt/lost+found
dev 1792 inode 11 bind/lost+found
The pair (1792, 11) is equal, so it is one file reached through two paths. This is different from a hard link (page 1). A hard link adds a name inside one file system, and a bind mount adds a path at another place in the tree, without changing the file system itself.
The key lines. A mount stays only while the system runs. The file /etc/fstab lists the mounts that the system makes at boot. A mount in a shell is gone after a reboot unless it is also in that file.
Bind mount
A bind mount makes a directory or file of the existing tree visible at a second mount point, without copying it. Both paths refer to the same inodes, so a change through one path is seen through the other.
Example: mount --bind /data/reports /srv/www/reports shows the reports directory in the web server's tree.
Check yourself:
1. What do the two stat lines of the bind mount have in common?
2. How does a bind mount differ from a hard link?
3. Which file lists the mounts that are made at boot?