08 September, 2013

Linux Sticky Bit

Introduction:

Sticky Bit is mainly used on folders in order to avoid deletion of a folder and its content by other users though they having write permissions on the folder contents. If Sticky bit is enabled on a folder, the folder contents are deleted by only owner who created them and the root user. No one else can delete other users data in this folder(Where sticky bit is set). This is a security measure to avoid deletion of critical folders and their content(sub-folders and files), though other users have full permissions.

For example: Create a project where people will try to dump files for sharing, but they should not delete the files created by other users.

How can I setup Sticky Bit for a Folder?

Sticky Bit can be set in two ways

    Symbolic way (t, represents sticky bit)
    Numerical/octal way (1, Sticky Bit bit as value 1)

Use chmod command to set Sticky Bit on Folder: /opt/dump/

Symbolic way:

[root@linuxguide ]$ chmod o+t /opt/dump/
or
[root@linuxguide ]$ chmod +t /opt/dump/

Let me explain above command, We are setting Sticky Bit(+t) to folder /opt/dump by using chmod command.

Numerical way:

[root@linuxguide ]$ chmod 1757 /opt/dump/

Here in 1757, 1 indicates Sticky Bit set, 7 for full permissions for owner, 5 for read and execute permissions for group, and full permissions for others.

Checking if a folder is set with Sticky Bit or not?

Use ls –l to check if the x in others permissions field is replaced by t or T

For example: /opt/dump/ listing before and after Sticky Bit set

Before Sticky Bit set:


[root@linuxguide ]$ ls -l
total 8
-rwxr-xrwx 1 abc abcgroup 148 Oct 22 06:16 /opt/dump/

After Sticky Bit set:

[root@linuxguide ]$ ls -l
total 8
-rwxr-xrwt 1 abc abcgroup 148 Feb 22 04:26 /opt/dump/

Sticky Bit FAQs:

Now sticky bit is set, lets check if user “temp” can delete this folder which is created xyz user.

[root@linuxguide ]$ rm -rf /opt/dump

rm: cannot remove '/opt/dump': Operation not permitted

[root@linuxguide ]$ ls -l /opt
total 8
drwxrwxrwt 4 abc abcgroup 4096 2011-01-01 18:31 dump

if you observe other user is unable to delete the folder /opt/dump. And now content in this folder such as files and folders can be deleted by their respective owners who created them. No one can delete other users data in this folder though they have full permissions.

I am seeing “T” ie Capital s in the file permissions, what’s that?

After setting Sticky Bit to a file/folder, if you see ‘T’ in the file permission area that indicates the file/folder does not have executable permissions for all users on that particular file/folder.

Sticky bit without Executable permissions:


so if you want executable permissions, Apply executable permissions to the file.
chmod o+x /opt/dump/

ls -l command output:
-rwxr-xrwt 1 abc abcgroup 0 Mar 6 12:24 /opt/dump/

Sticky bit with Executable permissions:


you should see a smaller ‘t’ in the executable permission position.

How can I find all the Sticky Bit set files in Linux/Unix.

find / -perm +1000

The above find command will check all the files which is set with Sticky Bit bit(1000).

Can I set Sticky Bit for files?

Yes, but most of the time it’s not required.

How can I remove Sticky Bit bit on a file/folder?

chmod o-t /opt/dump/

2 comments: