From the course: Linux Tips

Bind mounts - Linux Tutorial

From the course: Linux Tips

Bind mounts

- [Instructor] A bind mount allows us to mount a file system or a subset of a file system in two places at once. They can be used for various reasons when parts of the file system need to be made available in different places. When we use chroot to rescue a system for example, we mount certain system directories such that tools in one Linux installation can work with them as though they belong to it, rather than to another installation. Bind mounts can also be used to help control access to certain files, like if a program or user needs access to only one specific part of a file system, but not the rest of it. And we can create a read-only bind mount that can be used to avoid the problem of a program or user overwriting or removing data. We can also use a bind mount to replace one file with another in the view of the file system while preserving that original file behind the scenes. Let's take a look at creating a bind mount to see how it works. I'll create a directory here to stand in for another directory. And now using mount dash dash bind, I can mount the var log directory into this new directory that I just created. And there they are. These are the same files as in var log, not a copy or anything like that. So they have the same permissions and are updated at the same time, because they're the same. When I'm done with this I can unmount it like any other mount. With gu mount and the path. And now I'm back to having my empty placeholder directory. Let's also use bind to temporarily replace a file. I'll make a file here with some text in it. And I'll create another one, too. There's the original file. Let's mount the second file over top of the original one. And then if I take a look at that file I can see the contents of the other file show up here under the file name for the original. I can edit this file, and those changes will be reflected in the second file. Effectively, we have one file with two names. When I'm done with it, I can unmount the file. And if I take a look at the file, I can see that the original one is back. Bind mounts are pretty straightforward, and if you have a reason to use them, now you know how.

Contents