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
Some related links you might find interesting:
http://lwn.net/Articles/159077/
http://linuxgazette.net/issue93/tag/2.html
Have you tried Searching this site?
Unix/Linux/Mac OS X support by phone, email or on-site: Support Rates
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. We appreciate comments and article submissions.
Many of the products and books I review are things I purchased for my own use. Some were given to me specifically for the purpose of reviewing them. I resell or can earn commissions from the sale of some of these items. Links within these pages may be affiliate links that pay me for referring you to them. That's mostly insignificant amounts of money; whenever it is not I have made my relationship plain. I also may own stock in companies mentioned here. If you have any question, please do feel free to contact me.
Specific links that take you to pages that allow you to purchase the item I reviewed are very likely to pay me a commission. Many of the books I review were given to me by the publishers specifically for the purpose of writing a review. These gifts and referral fees do not affect my opinions; I often give bad reviews anyway.
We use Google third-party advertising companies to serve ads when you visit our website. These companies may use information (not including your name, address, email address, or telephone number) about your visits to this and other websites in order to provide advertisements about goods and services of interest to you. If you would like more information about this practice and to know your choices about not having this information used by these companies, click here.
Click here to add your comments
Sun Apr 30 06:08:08 2006: Subject: 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: Subject: --bind in mac os x? anonymous
is there a way to do a --bind in Mac OS X (10.4.5) ????
please???
Tue May 2 20:10:50 2006: Subject: TonyLawrence
Not that I'm aware of.
Wed Nov 12 04:46:01 2008: Subject: cool Gonzalo
cool!
I had a problem with my ftp client and now it is solved. Thanks.
Sat Nov 22 09:19:56 2008: Subject: 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: Subject: So er... 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: Subject: TonyLawrence
Unmount it.
Fri Apr 3 16:33:08 2009: Subject: mount --bind 1 directory to 3 mount points 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
Don't miss responses! Subscribe to Comments by RSS or by Email
Click here to add your comments
If you want a picture to show with your comment, go get a Gravatar