Linux setuid and setgid
The Linux Files can be setuid or setgid. When a user executes a setuid file, the program runs with the effective user ID of the file’s owner, rather than that of the user. Similarly, when a user executes a setgid file, the program runs with the effective group ID of the file’s group owner, rather than that of the user.
Directories can also be setgid. When a user creates a file in a non-setgid directory, the group ownership of the file is set to the user’s group ID. However, when a user creates a file in a setgid directory, the group ownership of the file is set to the group owner of the directory.
To enable setuid you can use Linux chmod command as shown below.
chmod u+s file_name
Example:
[root@RHEL2 setuidtest]# ls -l
total 0
-rwxr--r-- 1 root root 0 Jul 6 07:34 setuidtest
[root@RHEL2 setuidtest]# chmod u+s setuidtest
[root@RHEL2 setuidtest]# ls -l
total 0
-rwsr--r-- 1 root root 0 Jul 6 07:34 setuidtest
[root@RHEL2 setuidtest]#
To disable setuid you can use Linux chmod command as shown below.
Example:
chmod u-s file_name
[root@RHEL2 setuidtest]# ls -l
total 0
-rwsr--r-- 1 root root 0 Jul 6 07:34 setuidtest
[root@RHEL2 setuidtest]# chmod u-s setuidtest
[root@RHEL2 setuidtest]# ls -l
total 0
-rwxr--r-- 1 root root 0 Jul 6 07:34 setuidtest
[root@RHEL2 setuidtest]#
To enable setgid you can use Linux chmod command as shown below.
chmod g+s file_name
Example:
[root@RHEL2 setgidtest]# ls -l
total 0
-rwxrwxr-- 1 root root 0 Jul 6 08:13 setgidtest
[root@RHEL2 setgidtest]# chmod g+s setgidtest
[root@RHEL2 setgidtest]# ls -l
total 0
-rwxrwsr-- 1 root root 0 Jul 6 08:13 setgidtest
[root@RHEL2 setgidtest]#
To enable setgid you can use Linux chmod command as shown below.
chmod g-s file_name
Example:
[root@RHEL2 setgidtest]# ls -l
total 0
-rwxrwsr-- 1 root root 0 Jul 6 08:13 setgidtest
[root@RHEL2 setgidtest]# chmod g-s setgidtest
[root@RHEL2 setgidtest]# ls -l
total 0
-rwxrwxr-- 1 root root 0 Jul 6 08:13 setgidtest
[root@RHEL2 setgidtest]#
Note: Also Octal values 4 and 2 can be used for setuid and setgid respectively.