Saturday, March 7, 2015

What is SGID and how to set SGID in Linux?

This is next to SUID in our ongoing Linux file and folder permissions series. We already discussed about CHMOD, UMASK, CHOWN, CHGRP, SUID, StickyBit and SUDO concepts in our previous posts. In this post we will see

What is SGID?

Why we require SGID?

Where we are going to implement SGID?

How to implement SGID in Linux?

What is SGID?

SGID (Set Group ID up on execution) is a special type of file permissions given to a file/folder. Normally in Linux/Unix when a program runs, it inherits access permissions from the logged in user. SGID is defined as giving temporary permissions to a user to run a program/file with the permissions of the file group permissions to become member of that group to execute the file. In simple words users will get file Group’s permissions when executing a Folder/file/program/command.

SGID is similar to SUID. The difference between both is that SUID assumes owner of the file permissions and SGID assumes group’s permissions when executing a file instead of logged in user inherit permissions.

Learn SGID with examples:

Example: Linux Group quota implementation

When implementing Linux Group quota for group of people SGID plays an important role in checking the quota timer. SGID bit set on folder is used to change their inherit permissions to group’s permissions to make it as single user who is dumping data. So that group members whoever dumps the data the data will be written with group permissions and in turn quota will be reduced centrally for all the users. For clear understanding of this you have to implement group quota from the above link. Without implementation of SGID the quota will not be effective.

How can I setup SGID for a file?

SGID can be set in two ways

1) Symbolic way (s)

2) Numerical/octal way (2, SGID bit as value 2)

Use chmod command to set SGID on file: file1.txt

Symbolic way:

chmod g+s file1.txt

Let me explain above command we are setting SGID(+s) to group who owns this file.

Numerical way:

chmod 2750 file1.txt

Here in 2750, 2 indicates SGID bitset, 7 for full permissions for owner, 5 for read and execute permissions for group, and no permissions for others.

How can I check if a file is set with SGID bit or not?

Use ls –l to check if the x in group permissions field is replaced by s or S

For example: file1.txt listing before and after SGID set

Before SGID set:

ls -l

total 8

-rwxr--r-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt

After SGID set:

Linuxnix-free-e-book
ls -l

total 8

-rwxr-sr-- 1 xyz xyzgroup 148 Dec 22 03:46 file1.txt

Some FAQ’s related to SGID:

Where is SUID used?

1) When implementing Linux group disk quota.

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

After setting SUID or SGID to a file/folder if you see ‘S’ in the file permission area that indicates that the file/folder does not have executable permissions for that user or group on that particular file/folder.

chmod g+s file1.txt

output:
-rwxrwSr-x 1 surendra surendra 0 Dec 27 11:24 file1.txt

so if you want executable permissions too, apply executable permissions to the file.

chmod g+x file1.txt

output:
-rwxrwsr-x 1 surendra surendra 0 Dec 5 11:24 file1.txt

you should see a smaller 's' in the executable permission position.

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

find / -perm +2000

The above find command will check all the files which is set with SGID bit(2000).

Can I set SGID for folders?

Yes, you can if it’s required (you should remember one thing, that Linux treats everything as a file)

How can I remove SGID bit on a file/folder?

chmod g-s file1.txt

Reference:

http://www.linuxnix.com/2011/12/sgid-set-sgid-linuxunix.html
http://stackoverflow.com/questions/5953169/avoid-having-subversion-modify-linux-file-permissions
http://stackoverflow.com/questions/1830131/weird-subversion-permissions-issue?rq=1
http://stackoverflow.com/questions/5836124/linux-permission-inheritance?rq=1
http://computernetworkingnotes.com/managing-file-system-security/sticky-bit.html
http://unix.stackexchange.com/questions/79395/how-does-the-sticky-bit-work
http://unix.stackexchange.com/questions/64126/why-does-chmod-1777-and-chmod-3777-both-set-the-sticky-bit
http://www.linuxnix.com/2011/12/sgid-set-sgid-linuxunix.html
http://www.bashguru.com/2010/03/unixlinux-advanced-file-permissions.html
http://stackoverflow.com/questions/14529325/how-to-remove-setgid-linux-unix
http://flylib.com/books/en/1.17.1.133/1/
http://www.linuxquestions.org/questions/linux-general-1/what-is-sticky-bit-mode-suid-sgid-258719/

http://en.wikipedia.org/wiki/Chmod
http://en.wikipedia.org/wiki/Sticky_bit
http://en.wikipedia.org/wiki/Setuid

http://computernetworkingnotes.com/managing-file-system-security/sticky-bit.html
http://ubuntuforums.org/showthread.php?t=1053885

No comments: