| |

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.

Directory mount structure on Linux
The Linux/UNIX Filesystem

I figured out how to do this yesterday because I wanted my friend to help me with some 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.

The reason he couldn’t access it is that 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 then 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 know how to do this as well.

Diagram of Umasks
Diagram of Umasks

How to Mount a Directory into a Different Location in Linux

This one works pretty simply. If you are doing this to share files with a different user, you will want to chown the folder and its children to <yourusername>:<group>. And 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, in recent versions of the Linux kernel, this is made easy. Enter in the following command:

sudo mount --bind /path/to/original/directory /path/to/mountpoint

Once you do that, you’re good to go. Happy sharing!

See also  How to Install ZSH on Mac, Linux, and FreeBSD

Mounting a Directory to a Different Location in FreeBSD

I am doing this tutorial specifically for FreeBSD because they actually removed the filesystem type that allows this to happen in OpenBSD. After all, there are obvious security issues to this if not done properly, but as long as you’ve been using the command line for long enough and you 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

Conclusion

Well, I hope I helped the few people in this world that actually have this problem. If you’re interested, you should join our discord. We’re always looking for Linux and BSD users as those people tend to fit in well in our community. So, if you’re interested, come on over! https://discord.gg/rftS5NA

Similar Posts

Leave a Reply

Your email address will not be published. Required fields are marked *