How To Mount a Directory to Another Location – Linux/BSD
So, most people in the know have probably heard of symlinks. They are the way to make one point in a filesystem refer to another. However, symlinks are sometimes glitchy and don’t work within a chroot. That’s why you can mount a directory on Linux and BSD.
I figured out how to do this yesterday because I wanted my friend to help me with configuration files in a Minecraft Server. It was not as simple as giving him access to the MC server’s group and creating a symlink to the directory.
He couldn’t access it because it is within my home directory, and my servers have a systemwide umask that prevents people from cd’ing into home directories other than their own. So, I had to figure out something innovative to allow him to cd into and edit the server files. That was when it came to me… “What if I could mount a directory in a different place!?” So, here I am, making a tutorial on this subject so the world can also know how to do this.
How to Mount a Directory into a Different Location in Linux
This one works pretty simply. If you do this to share files with a different user, you will want to chown the folder and its children to <yourusername>:<group>. Then, you will want to add the user to the group. Check out https://www.howtogeek.com/50787/add-a-user-to-a-group-or-second-group-on-linux/ for how to add users to groups.
Now, all you need to do is mount it. Luckily, this is made easy in recent versions of the Linux kernel. Enter the following command:
sudo mount --bind /path/to/original/directory /path/to/mountpoint
Once you do that, you’re good to go. Happy sharing!
Mounting a Directory to a Different Location in FreeBSD
I am doing this tutorial specifically for FreeBSD because they removed the filesystem type that allows this to happen in OpenBSD. After all, there are obvious security issues if not done properly, but as long as you’ve been using the command line for long enough and know what you’re doing, it’s really not an issue. (Sorry, OpenBSD users.)
The command for this is pretty simple. Obviously, you’ll want to chown the directory and its children to the proper group and then add your friend to the group if you’re doing this for sharing purposes, but once you’re done with that, just run this command as root:
mount -t nullfs /path/to/original/directory /path/to/mountpoint