36

When I am doing:

mount --bind dirone dirtwo

After OS reboot the binding is lost.

I am checking binded dirs in /proc/mounts

How can I make these binds permanent without clogging up /etc/fstab ?

Here's one entry from /proc/mounts

/dev/disk/by-uuid/4f5082d7-aba2-4bc7-8d64-3bbb3d773aab /home/username/dir ext4 rw,relatime,data=ordered 0 0
Eric Leschinski
  • 2,221
  • 1
  • 20
  • 24
  • 2
    You have to either run that command during boot (rc.local) or put an entry in /etc/fstab. What makes you think this is clogging up /etc/fstab ? , this is what fstab is for, fstab is much "cleaner" then rc.local – Panther Nov 16 '14 at 18:30

4 Answers4

45

What do you mean "clogging up /etc/fstab"? The best place to put this in is /etc/fstab; that's what it was made for!

All you have to do is add one line after the first mount:

# <device>                                 <dir>                 <type>  <options>                 <dump>  <pass>
UUID=288a84bf-876c-4c4b-a4ba-d6b4cc6fc0d2  /mnt/device            ext4    defaults,noatime,nofail   0       2
/mnt/device                                /srv/binded_device     none    bind                      0       0
40

The easiest way is to mount --bind what you need like

mount --bind /home/sda1/Windows/Users/Me/Dropbox ~/Dropbox

Then open mtab

sudo nano /etc/mtab

Copy your line like

/home/sda1/Windows/Users/Me/Dropbox /home/me/Dropbox none rw,bind 0 0

and paste it in fstab so it would mount on reboot

sudo nano /etc/fstab

If you folder is on mounted disk make sure your binding line comes after disk mount

dgpro
  • 564
  • 4
  • 6
  • 6
    this should be the accepted solution. – Daksh M. Aug 31 '18 at 14:42
  • Dont edit /etc/mtab directly, its a symbolic link, so you will get errors if you edit it directly. More importantly, edit /etc/fstab. /etc/mtab reflects the changes in /etc/fstab. You are basically putting the same settings in a different file so why not use /etc/fstab? – Dave Oct 28 '22 at 18:18
  • @Dave, you misread. @dgpro is not talking about editing /etc/mtab, he's talking about copying the line that will appear in that file after using the mount command to /etc/fstab. – Forage Dec 22 '23 at 16:51
4

Another solution (which is helpful when you're using LVM and the accepted answer will not work and some may consider more useful since it uses a bit more logic. Another use case scenario where this is the preferred option is if you're using encrypted partitions that require a password so you'd want to make sure that it doesn't attempt to mount there before the volume is mounted.) would be doing something similar to this:

Append the following to your crontab
# crontab -l | tail -1 ; cat /usr/sbin/custom-compiler-mount

@reboot /usr/sbin/custom-compiler-mount

Essentially you would use crond to execute a script on reboot

#!/bin/bash
( until [[ $( (mount |& grep vg0-homevol 2>&1 9<&1 > /dev/null 1<&9) ) ]] ; 
do 
sleep 1 
done & wait;mount -o rbind /home/linuxgeek/experimental/s3/gcc/ /gcc & ) & >/dev/null 
0

If this is a "per user" mount and not a system-wide mount, rather than using mount --bind or creating an /etc/fstab entry, why not use a symlink?

ln --symbolic target_dir access_point_of_target_dir

Add this line to the bottom of your user's .profile file to have it available automagically on login, without creating a system-wide fstab entry, and without needing root permissions like you will when putting mount --bind in a script.

It's not a perfect solution, but I have found that in most cases a symbolic link performs the same as having access to the physical directory.

Ian Moote
  • 121
  • This doesn't work for snaps, which is the default for many tools on an Ubuntu system. This is my use case, but symlinks make snaps fail. Using a bind mount makes it work. – ZaxLofful Oct 12 '23 at 05:35
  • Some tools like IDE get confused: in some of their internals, they call realpath, but not everywhere. In result you never know whether it's gonna remember a linked path or real. With bind mounts they don't know. – Dzmitry Lazerka Oct 31 '23 at 02:25