1. Home
  2. Linux
  3. Bind mounts and symlinks on linux

How To Use Bind-mounts And Symlinks On Linux

There are a lot of mundane things on Linux that a lot of users don’t think much of that are incredibly useful. When new users learn how to use these mundane, boring little tricks, they become better Linux users. Bind-mounts and Symlinks on Linux are a prime example of this. In this article, we’ll discuss just exactly what is a symlink, how to create one and what they’re for. We’ll also go over how to create bind mounts, why they’re so useful, and even show users how to beef up their bind mounts with bindfs!

SPOILER ALERT: Scroll down and watch the video tutorial at the end of this article.

What Is A Symlink?

A symlink, or “symbolic link” is a reference to another file or folder somewhere else on the file system. For example, most developers, instead of duplicating libraries on a user’s system, will instead have their code create symbolic links to library folders and program files. This allows the program to easily have all the required tools and library files it needs to run, without creating extra cruft.

Symlinks are also used when referencing files between different partitions and drives. For example: your entire music library is stored on hard drive B, and the entire library’s size is 500 GB. Due to how large it is, it’s not possible to directly move the music library to Hard Drive A (aka the system drive).

To easily solve this problem, users can make a symbolic link and reference each file from drive B to drive A. That way the music player can find and access all music files easily.

Using Symlinks

Symbolic links can be used in two ways: to link an entire directory, or a single file. Here’s how to create them.

Symlink a file

To symlink a file, first open a terminal. Next, cd to the directory with the file to be linked.

cd ~/path/to/file

Create the symbolic link. Keep in mind where the link should go.

ln -s original-file /path/where/link/goes

Symlink a folder

ln -s /path/to/original/folder /path/where/linked/folder/goes

Delete a symlink

cd /path/where/symlink/is
rm symlinkname
unlink symlinkname

What Is A Bind-mount?

A bin-mount is a way to show contents of one folder in once place on the file system in a different directory. Bind mounting sounds a lot link symbolically linking a folder, but it is very different. The bind mount is very useful. System administrators often use this method to quickly add more storage to a folder on the fly when the main system’s file system is ‘read only’. Bind-mounts can also be used to quickly bind a network share to a local directory, without the need to set up a mount point in the fstab file, and etc.

Using Bind-mounts With The Built-in Linux Kernel Command

The Linux kernel has a built in bind command. It is because of this, users can easily mount the contents of directories across the file system with ease. Here’s how to do it.

Think of a directory that has files you’d like to mount in another location. When you’ve got the location in mind, think of the place you’d like to mount it to.

In this example, we’ll bind a music directory from one hard drive to the other.

sudo mount --bind /mnt/DataDrive/Music /home/user/Music

Unmount the bind mount with:

umount /home/derrik/Music

BindFS

Before making a bind-mount, you’ll need to install bindfs. It’s a tool that makes bind mounting possible, and also adds extra functionality (permission settings, better mirroring and etc). Here’s how to get it on your Linux distribution:

Ubuntu

sudo apt install bindfs

Debian

sudo apt-get install bindfs

Arch Linux

Bindfs isn’t in the official Arch software sources, unfortunately. If users want to use bindfs, they’ll need to install this AUR package instead.

Fedora

sudo dnf install bindfs

OpenSUSE

sudo zypper install bindfs

Other Linuxes

To get bindfs on the Linux distribution of your choice, consider going to the official bindfs website. Alternatively, open the package manager on your Linux PC, search for “bindfs” and install it!

Using Bindfs For Bind-mounts

Using bindfs has it’s advantages. The built in kernel bind command works in a snap, but it’s very basic and doesn’t allow for some advance features such as restricting access to binds and etc. Here’s how to make use of bindfs.

Make a read only bind mount

bindfs --perms=a-w /path/to/the/original/directory/ /path/to/bind/to/

Make a read/write bind mount

bindfs /path/to/the/original/directory /path/to/bind/to

Unmount a bindfs mount

sudo umount /path/to/bind/

Conclusion

Mundane operations like symbolic links and bind mounts are probably the least explored subject on Linux due to the fact that they’re pretty uninteresting, and usually go unused by general users. The fact is: when you learn that symbolic links let you quickly and easily access files anywhere, or that bind mounts can make it incredibly easy to bolt on additional storage wherever you want, these subjects become much more interesting.

Leave a Reply

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

This site uses Akismet to reduce spam. Learn how your comment data is processed.