Special permissions


==> 1. How to set User ID (setuid)?

SUID is a special permission assigned to a file. These permissions allow the file to be executed with the privileges of the owner. For example, if a file was owned by the root user and has the setuid bit set, no matter who executed the file, it would always run with the root user privileges.

How to set setuid permission to a file?

For this, you must be the owner of the file or root user. Run the following command to set the setuid permission/bit.

# chmod u+s file1

View the permissions using the ls -l command:

# ls -l file1

-rwSrw-r-- 1 user1 user1 0 2018-02-04 21:41 file1

The S (capital letter) means, there is no execute permission. To assign the execute permission, run the following command,

# chmod u+x file1

# ls -l file1

-rwsrw-r-- 1 user1 user1 0 2018-02-04 12:20 file1

The lower case s means, there is an execute permission.

Alternatively, you can set the setuid bit using the numeric method by prepending a 4 to the mode. For example, to set the setuid bit, read, write, and execute permissions for the owner of the file "file1", execute the below command:

# chmod 4700 file1



==> 2. How to set the Group ID (setgid) for files?


 # chmod g+s test (Run this command to set the setgid bit on file "test")

Note: Both the setuid and setgid bits are set using the s symbol. The setgid is represented the same as the setuid bit, except in the group section of the permissions.

Run the following command as root to set the setgid bit, and read, write, and execute permissions for the owner of the file "test:

# chmod 2700 test
The setgid is represented the same as the setuid bit, except in the group section of the permissions:

ls -l file1

-rwx--S--- 1 user1 user1 0 2017-10-30 21:40 file1

Simply, Use the chmod u+s command to set the setuid bit. Use the chmod g+s command to set the setgid bit.



==> Set group id permission for Directories


When the setgid bit is set on a directory, all files created within that directory will inherit the group ownership of that directory.

For example, the folder "folder1" is owned by the user "user1", and the group "group1":

# ls -ld folder1
 
drwxrwxr-x 2 user1 group1 4096 2017-10-30 22:25 folder1

Files created in the "folder1" will inherit the "group1" group membership:

# touch folder1/file1

 
# ls -l folder1/file1

-rw-rw-r-- 1 user1 group1 0 2017-10-30 22:29 folder1/file1


==> How to set the SGID bit for directories?

 

To set the setgid bit on a directory, use the chmod g+s command:

# chmod g+s folder1

View the permissions using the ls -ld command, noting the s in the group permissions:

# ls -ld folder1
 
drwxrwsr-x 2 user1 group1 4096 2017-10-30 22:32 folder1

Alternatively, prepend a 2 to the directories mode:

# chmod 2770 folder1


==> What is sticky bit on a directory?


When the sticky bit is set on a directory, only the root user, the owner of the directory, and the owner of a file can remove files from the said directory.


How to set a sticky bit?


An example of the sticky bit is the /tmp directory. Use the ls -ld /tmp command to view the permissions:

# ls -ld /tmp
 
drwxrwxrwt  24 root root  4096 2017-10-30 22:00 tmp

 
The t at the end symbolizes that the sticky bit is set. A file created in the /tmp directory can only be removed by its owner or the root user. For example, run the following command to set the sticky bit on the folder1 folder:

# chmod a+t folder1

Alternatively, prepend a 1 to the mode of a directory to set the sticky bit:

# chmod 1777 folder1

 
The permissions should be read, write, and execute for the owner, group, and everyone else, on directories that have the sticky bit set. This allows anyone to cd into the directory and creates files.
 

How to find files with SUID/SGID bit set?


1. To find all the files with SUID bit set, use the below command :

# find / -perm +4000

2. To find all the files with SGID bit set, use the below command :

# find / -perm +2000

You can also combine both the commands to find both SGID and SUID bit set files.

# find / -type f \\( -perm -4000 -o -perm -2000 \\) -exec ls -l {} \\;

0 comments: