You need to let users create files in a common directory, but you don't want them to be able to delete other's files. Or you've put certain files, directories or symlinks into a user's home directory and don't want them to be able to mess with any of those. What can you do?
If you create /foo and do "chmod 1777 /foo", you'll have a world-writeable directory with the "text bit" set. Any user can create files here, but they can only delete files that they own (root can still rm anything). That's ownership as listed in the "owner" column of an "ls -l". Group ownership doesn't come into play here although it does change responses a bit.
Let's see what happens when Sam tries to remove Pete's files in a directory with the text bit set:
[sam@localhost foo]$ ls -ld . drwxrwxrwt 2 root root 4096 Sep 18 06:00 . [sam@localhost foo]$ ls -l total 12 -rw-rw-r-- 1 pete pete 29 Sep 18 05:52 pete -rw-rw-r-- 1 pete apl 29 Sep 18 06:00 peteapl -rw-rw-r-- 1 pete wheel 29 Sep 18 06:00 petewheel [sam@localhost foo]$ id uid=502(sam) gid=502(sam) groups=502(sam) [sam@localhost foo]$ rm * rm: remove write-protected regular file `pete'? y rm: cannot remove `pete': Operation not permitted rm: remove write-protected regular file `peteapl'? y rm: cannot remove `peteapl': Operation not permitted rm: remove write-protected regular file `petewheel'? y rm: cannot remove `petewheel': Operation not permitted
Now watch what happens when a user in the "wheel" group does the same thing:
[apl@localhost ~]$ cd /foo [apl@localhost foo]$ ls -l total 12 -rw-rw-r-- 1 pete pete 29 Sep 18 05:52 pete -rw-rw-r-- 1 pete apl 29 Sep 18 06:00 peteapl -rw-rw-r-- 1 pete wheel 29 Sep 18 06:00 petewheel [apl@localhost foo]$ id uid=500(apl) gid=500(apl) groups=10(wheel),500(apl) [apl@localhost foo]$ rm * rm: remove write-protected regular file `pete'? y rm: cannot remove `pete': Operation not permitted rm: cannot remove `peteapl': Operation not permitted rm: cannot remove `petewheel': Operation not permitted [apl@localhost foo]$ [apl@localhost foo]$ [apl@localhost foo]$ rm peteapl rm: cannot remove `peteapl': Operation not permitted [apl@localhost foo]$ rm petewheel rm: cannot remove `petewheel': Operation not permitted [apl@localhost foo]$ rm pete rm: remove write-protected regular file `pete'? y rm: cannot remove `pete': Operation not permitted
Having write permission makes rm proceed without caution, only to be brought up short by the restrictions of the "t" bit.
If the problem is removal of a directory and it is not terribly inconvenient for you to have that directory actually be on a separate filesystem, then "mount" can make the directory safe from removal. You can read more at mount --bind, but it's not very complicated. 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
Now THAT is removal protection!
Typically, ACL's let you avoid complicated groups by setting specific permissions for specific users. Other than setting a file as "immutable" (chattr +i filename on some Linuxes), you really can't prevent removal of a file. Of course setting it that way may also make it useless, as even the owner can't modify or remove it either without doing "chattr -i" first.. See ACL's for more on that.
[pete@localhost foo]$ id uid=501(pete) gid=501(pete) groups=501(pete) [pete@localhost foo]$ chattr +i pete [pete@localhost foo]$ rm -f pete rm: cannot remove `pete': Operation not permitted [pete@localhost foo]$ mv pete /tmp/ mv: cannot move `pete' to `/tmp/pete': Operation not permitted [pete@localhost foo]$
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
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