If the thing you want to link to is a mounted file system, you can use the "--bind" option of mount to create an unbreakable link.
Most Linux and Unix file systems don't allow hard links to directories (except for the . and .. entries that mkdir creates itself). The reasons are are pretty obvious: you could really confuse programs like ls (ls -R), find and of course fsck if you created links that recursed back to themselves. If there was a compelling reason to allow directory hard links, you'd need to rewrite any program that wants to walk a file system tree to be aware of the possible problems..
So instead we have symlinks. You've probably used them for things like shifting around disk space or to give more convenient access to a directory. For example, Mac OS X creates /tmp as a symbolic link to "private/tmp". We use symlinks to make other directories visible under Apache's htdocs directory (though the same thing can be accomplished with Apache's configuration files).
One problem with symbolic links is that really they are just files. A special kind of file, yes, but a symlink only points at a directory - it doesn't act like one. So, for example, if you put a symlink to /xyz in a users home directory, and the user has write permission to his home (as he ordinarily would), he can remove your symlink. Nothing you can do with ordinary permissions can prevent that. You can do a "chattr +i" on your symlink, but because it is a symlink, that passes through to the actual directory, making it unusable. If you use "+u" (undeletable), that again passes through, and the user still can delete your symlink.
This can be extremely annoying, especially when users accidentally delete a symlink they need to have. Of course your real directory is still safe, but you need to recreate the symlink. In the mean time, your user is confused or maybe even broken.
There is at least one way around this. If the thing you want to link to is a mounted file system, you can use the "--bind" option of mount to create an unbreakable link.
If the thing to link to it isn't a separate fs, you can almost always make it be one.
Here's how it works. Let's say we have /dev/foo mounted at /foo and I want a "link" to that under /home/fred. All I have to do is:
mount --bind /foo /home/fred/foo
Fred can have full write permissions on /foo if he needs it, but he will not be able to remove /home/fred/foo. Not even root can:
# rm -rf /home/fred/foo
rm: cannot remove directory '/home/fred/foo': Device or resource busy
Do NOT use this just for silly convenience because you want "/etc" to be "/etcetra". See, for example, Yet another warning about mount --bind and rm -rf.
Use it for the things that make sense: putting a directory into a users home directory that refers to some shared resource that you always want them to have access to without having to cd somewhere. That's a place that makes perfect sense. Mounting a system filesystem somewhere probably does not.
You might find this interesting:
Got something to add? Send me email.
More Articles by Anthony Lawrence © 2012-01-03 Anthony Lawrence
Don't blame me for the fact that competent programming, as I view it as an intellectual possibility, will be too difficult for "the average programmer" — you must not fall into the trap of rejecting a surgical technique because it is beyond the capabilities of the barber in his shop around the corner. (Edsger W. Dijkstra)
Sun Apr 30 06:08:08 2006: 1979 drag
I use mount to bind directories when using a Knoppix cdrom to recover from mucked up linux bootloaders or resetting root passwords and whatnot.
Assuming that /dev/hda1 is your OS's on your harddrive's main partition...
Boot up with knoppix.
mount /mnt/hda1
(should set that up automaticly, could be in /media though)
cd /mnt/hda1
mount -t proc proc ./proc
mount -t sysfs sys ./sys
mount -o bind /KNOPPIX/dev ./dev
(or is that /mnt/KNOPPIX/dev? I forget, been a while)
chroot ./ su -
Or something similar to that. Should end up having full access to the hardware from inside the chroot environment for setting up grub or whatnot.
I also use that in conjunction with debootstrap for installing Debian on machines for for whatever reason have issues with the installer cdrom (such as only have knoppix disk handy, or normal installer cdrom doesn't have drivers for the network or ide controller.
Tue May 2 17:53:13 2006: 1998 anonymous
is there a way to do a --bind in Mac OS X (10.4.5) ????
please???
Tue May 2 20:10:50 2006: 1999 TonyLawrence
Not that I'm aware of.
Wed Nov 12 04:46:01 2008: 4772 Gonzalo
cool!
I had a problem with my ftp client and now it is solved. Thanks.
Sat Nov 22 09:19:56 2008: 4793 seb
Hi,
there is something I can't do with "--bind".
In fact, I try to make some hardlinks between files in the newly binded directory and I always have the cross-device error (even if it's on the same device !).
Do you know if it's possible to do this ? Do I miss something ?
thx
Mon Feb 2 11:26:05 2009: 5278 Roger
"Fred can have full write permissions on /foo if he needs it, but he will not be able to remove /home/fred/foo. Not even root can:"
So how would one go about removing one of these then?
Mon Feb 2 12:03:28 2009: 5279 TonyLawrence
Unmount it.
Fri Apr 3 16:33:08 2009: 5970 NelsonDelgado
I have a ftp server with 3 local users chrooted to their home directories, and 1 annonymous directory for public downloads available to anyone (/home/ftp). I already bound /home/ftp to /home/user1/public-ftp so user1 can upload files to the public directory, but how can I bind the same public directory to all three users so they can upload there too... one directory to 3 mount points? Is it possible?
Thanks beforehand... Nelson
Thu Jan 21 07:57:38 2010: 7933 edubidu
Hi, does the bind option not work with a mount of an external server?
mount --bind hostname:/vol02 /home/
mount: special device diufnas01:/vol02 does not exist
Thu Jan 21 14:34:30 2010: 7939 TonyLawrence
You may have misunderstood - I'm not sure.
You use bind against something that is already mounted - see the examples above.
Fri Jan 22 10:23:04 2010: 7941 edubidu
Thank you. I see that the bind option does not work with the format server:/ but only with /folder/subfolder /mount
Wed Mar 31 22:45:00 2010: 8324 MichaelWhitis
OS X you CAN do the equivalent of a mount --bind using the MacFuse kernel extension
(link)
and loopback mount program (which is part of MacFuse, but you'll have to compile it)
then you can use loopback to mount anything thusly:
loopback /var/chrootenv/mount -omodules=threadid:subdir,subdir=/ -oallow_other,native_xattr,volname=LoopbackFS
Would mount / on /var/chrootenv/mount
if you REALLY want to get tricky you can use otool -L and install_name_tool to make a version with the libraries linked in the chroot location
Here's a small script:
chrootJailLocation="/tmp/chrootjail"
linklist="/usr/local/bin/loopback"
# relink libraries for chroot
linklist="/usr/local/bin/loopback"
for x in ${linklist}; do
cp $x ${chrootJailLocation}$x.linked
for y in `otool -LX $x | awk '{ print $1 }'`; do
#echo $x $y
/usr/bin/install_name_tool -change $y ${chrootJailLocation}$y ${chrootJailLocation}$x.linked;
done;
done
Wed Mar 31 22:53:09 2010: 8325 TonyLawrence
Thanks - I'm sure that will help someone!
Sun Oct 31 18:59:52 2010: 9081 joxl
Great article, very informative. Thanks for sharing!
Use caution when using mount to bind directories throughout your system though. It's an easy way to get unexpected results when using programs that recursively walk the filesystem.
> Not even root can:
> # rm -rf /home/fred/foo
Indeed, rm will not remove the mount point itself. However, rm will remove everything under it. Understanding the danger of the rm command is outside the scope of this article, but 'rm -rf' is really scary if you've been binding directories this way. Consider this example:
Hack around for a while... OK, done with that now...
Oh, oops, well that's good, I forgot about that mountpoint...
Whew!
Whoops!
Finally, (as far as doing this sort of thing on a Mac) it seems you can use bindfs (requires MacFUSE).
Fri Sep 30 21:29:09 2011: 9880 RonCam
[quote] Let's say we have /dev/foo mounted at /foo and I want a "link" to that under /home/fred. All I have to do is:
mount --bind /foo /home/fred/foo [/quote]
Why wouldn't that be --bind /dev/foo ? It doesn't need the full path?
By the way, I'm thinking of using this to relocate the /doc/ in /usr/share/doc off of the small SSD in my netbook.
Fri Sep 30 21:51:11 2011: 9881 TonyLawrence
Nope. You aren't linking to the device but to the place where it is mounted.
Tue Jan 3 11:17:05 2012: 10437 Daniel
We have a home net work with the following:
- my own system: dual boot Fedora/Windows 7
- my wife's system: Windows 7
- printer server and backup server, also file-transfer server between the two above: Windows XP.
I use Thunderbird on both the Fedora and the Win7 situation as I work both for longer periods at a time and need to be able to read mails whichever I use at the moment. A link in Fedora to the directories in Win7 in the past gave problems with running back-ups to the WinXP machine.
Mounting the Win7 partition as /media/Windows and thereafter mounting bind the Thunderbird-data-directories to the equivalent ones in Fedora worked perfectly.
So other directories needing to be accessible under both Win7 and Fedora I mounted bind also. So far no problems (but not done any rm -R either!)
------------------------
Printer Friendly Version
mount ––bind Copyright © April 2006 Tony Lawrence
Have you tried Searching this site?
This is a Unix/Linux resource website. It contains technical articles about Unix, Linux and general computing related subjects, opinion, news, help files, how-to's, tutorials and more.
Contact us
Printer Friendly Version