Prevent deletion or moving of files

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?

"t" bit

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:


[[email protected] foo]$ ls -ld .
drwxrwxrwt 2 root root 4096 Sep 18 06:00 .
[[email protected] 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
[[email protected] foo]$ id
uid=502(sam) gid=502(sam) groups=502(sam)
[[email protected] 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:

[[email protected] ~]$ cd /foo
[[email protected] 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
[[email protected] foo]$ id
uid=500(apl) gid=500(apl) groups=10(wheel),500(apl)
[[email protected] 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
[[email protected] foo]$ 
[[email protected] foo]$ 
[[email protected] foo]$ rm peteapl
rm: cannot remove `peteapl': Operation not permitted
[[email protected] foo]$ rm petewheel
rm: cannot remove `petewheel': Operation not permitted
[[email protected] 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.

mount --bind

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!

ACL's

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.

[[email protected] foo]$ id
uid=501(pete) gid=501(pete) groups=501(pete)
[[email protected] foo]$ chattr +i pete
[[email protected] foo]$ rm -f pete
rm: cannot remove `pete': Operation not permitted
[[email protected] foo]$ mv pete /tmp/
mv: cannot move `pete' to `/tmp/pete': Operation not permitted
[[email protected] foo]$ 
 


Got something to add? Send me email.





(OLDER) <- More Stuff -> (NEWER)    (NEWEST)   

Printer Friendly Version

-> -> Prevent deletion or moving of files




Increase ad revenue 50-250% with Ezoic


More Articles by

Find me on Google+

© Anthony Lawrence



Kerio Connect Mailserver

Kerio Samepage

Kerio Control Firewall

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.

Contact us





As an experimental psychologist, I have been trained not to believe anything unless it can be demonstrated in the laboratory on rats or sophomores. (Steven Pinker)

Unlike info, pinfo does not display anything if it has nothing. I've been forever irritated by info coming up with its default page when it has nothing to tell me. (Tony Lawrence)








This post tagged: