Linux Interview Question 3: Sticky Bit Shared Directory
Linux Interview Questions · Test 1 · question 3 of 10
The directory /srv/shared has mode drwxrwxrwx (0777) so that every developer can drop files into it. Developers now complain that colleagues keep deleting and renaming files they do not own.
Every user must still be able to create files in /srv/shared, but each file should be removable or renameable only by its owner (or root). What should you do?
Show answer and explanation
Answer:
Deleting or renaming a file is an operation on the directory that contains it, not on the file. It requires write and execute permission on the directory, which everyone has at 0777. The sticky bit (S_ISVTX, octal 1000) on a directory adds the rule that a file inside can be renamed or deleted only by the file's owner, the directory's owner, or a privileged process. Creating files is still allowed. This is exactly how /tmp is set up (drwxrwxrwt).
Option B is wrong because setgid on a directory only makes new files inherit the directory's group. It is useful for team directories but does nothing to stop deletions.
Option C is wrong because file write permission controls modifying a file's contents, not unlinking it. With a world-writable directory, anyone can still delete a read-only file.
Option D is wrong for the same reason: umask only sets the default permissions of newly created files, so files are still deletable through the directory. It also leaves every existing file unchanged.
Reference: inode(7) - Linux manual page