Linux Permissions – Allow All Users to Create Files in Directory but Only Owner Can Delete

linuxpermissions

How can I use, preferably a single chmod command, which will allow any user to create a file in a directory but only the owner of their file (the user who created it) can delete their own file but no one else's in that directory.

I was thinking to use:

chmod 755 directory

As the user can create a file and delete it, but won't that allow the user to delete other people's files?

I only want the person who created the file to be able to delete their own file. So, anyone can make a file but only the person who created a file can delete that file (in the directory).

Best Answer

The sticky bit can do more or less what you want. From man 1 chmod:

The restricted deletion flag or sticky bit is a single bit, whose interpretation depends on the file type. For directories, it prevents unprivileged users from removing or renaming a file in the directory unless they own the file or the directory; this is called the restricted deletion flag for the directory, and is commonly found on world-writable directories like /tmp.

That is, the sticky bit's presence on a directory only allows contained files to be renamed or deleted if the user is either the file's owner or the containing directory's owner (or the user is root).

You can apply the sticky bit (which is represented by octal 1000, or t) like so:

# instead of your chmod 755
chmod 1777 directory

# or, to add the bit to an existing directory
chmod o+t directory
Related Question