Bind Mounting allows us to make one directory accessible from more than one location. It’s like “intercepting” directory1 and making the system redirect requests to directory2.
Let’s take a look how to set this up and why we may want to use this technique.
Quick Refresher: Device Mounting
Perhaps you’re familiar with mounting devices to the filesystem in Linux. Say you had a new USB stick or other volume available and you want to make this accessible to your filesystem. Here’s how you’d do that:
mount /dev/sda1 /media
Whatever was located in /media is no longer visible – instead you’ll see the contents of the mounted device in its place. The content in /media is not gone though, it’s just “invisible” to the filesystem. To unmount the device we’ll use
and all previous content in /media is visible again.
Enter Bind Mounting
Bind Mounting behaves in much the same way, but it’s used for existing directories rather than devices. Consider two directories: /media and /pictures. If you’d like to see the content of /pictures inside /media you can bind mount those directories:
mount --bind /pictures /media
After the –bind switch, enter the directory to be mounted, followed by the mount point where you’d like to see it. Again the original contents of /media is invisible again and is replaced by what’s in /pictures. Notice that /pictures is still accessible as it was before – so you can read and write via two different access points – even though the content will only be written into one place (i.e. /pictures).
Why do I need this?
This technique can come in handy if you have a script you cannot change, and it’s insisting on writing data into a place you don’t like. Think of partitions that are too small. With bind mounting you can mount a directory from a larger partition to the directory on the smaller partition, and therefore magically create space where there clearly isn’t any.
How do we make this permanent?
The mount command does not survive a server restart and you’ll have to set it up when you reboot your system. You can add a bind mount automatically upon start by editing /etc/fstab and add your instruction in this way:
/pictures /media none bind