Linux directory permissions read write but not delete

LinuxFile Permissions

Linux Problem Overview


Is it possible to setup directory permissions such that a group is able to read and write files and subdirectories but not delete anything?

Linux Solutions


Solution 1 - Linux

It might be enough to set the sticky bit on the directories. Users will be able to delete any files they own, but not those of other users. This may be enough for your use case. On most systems, /tmp is setup this way (/tmp is set 1777)

chmod 1775 /controlled

However, If you want more control, you'll have to enable ACL on the filesystem in question.

In /etc/fstab, append acl to the flags:

/dev/root        /                       ext3    defaults,acl       1 1

You can then use setfacl/getfacl to control and view acl level permissions.

Example: (Create files, once written, they are read only, but CAN be deleted by owner, but not others.)

setfacl --set u::rwxs,g::rwx /controlled
setfacl -d --set u::r-x,g::r-x,o::- /controlled

You can set a default acl list on a directory that will be used by all files created there.

As others have noted, be careful to specify exactly what you want. You say "write" - but can users overwrite their own files? Can they change existing content, or just append? Once written, it's read only? Perhaps you can specify more detail in the comments.

Lastly, selinux and grsecurity provide even more control, but that's a whole other can of worms. It can be quite involved to setup.

Solution 2 - Linux

Well, it would be r-x for this directory.

And files in it would have rw-.

This is because a file can be written if its permissions allow Write, but it can only be deleted if its directory's permissions allow Write.

Solution 3 - Linux

Possible or not, make sure that overwriting with a 0-byte file isn't equivalent to deleting the file in your particular context.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionCodeLizardView Question on Stackoverflow
Solution 1 - Linuxjmanning2kView Answer on Stackoverflow
Solution 2 - LinuxalamarView Answer on Stackoverflow
Solution 3 - LinuxbacarView Answer on Stackoverflow