Set-group-id on directory
虽然用linux多年,但是还不知道,原来set-group-id还可以在directory上设置,它的作用是:任何用户在该directory下创建的文件,其group都是该directory所属于的组,任何用户在该directory下创建的目录,其group都是该directory所属的组,而且继承set-group-id设置。
举例
普通用户jason(它的primary group也是jason),在/home/jason目录下创建了目录dir。然后为设置dir设置set-group-id。
jason@jason:~
cd/home/jasonjason@jason:
chmod g+s dir
此时使用ls -l时,显示dir目录时,它的group executable bit显示为s,而不在是x;当然如果dir目录已经关闭了group executable bit,那么显示为S。
为了允许其它的用户创建和访问dir目录,需要设置dir的other权限为rwx。
jason@jason:~$ chmod o=rwx dir
然后使用用户jixu(它的primary group为jixu,它不属于jason组)登陆,进入到dir目录,创建文件jixu_file,此时该文件的group为jason;创建目录jixu_dir,此时该目录的group为jason,且该目录也开启了set-group-id设置(ls -l显示时,该folder的group executable bit显示为s)。
在mount磁盘分区的时候,如果设置了grpid,那么效果与对mount分区的跟目录设置set-group-id相同。
原因
方便各个用户之间对文件的共享和操作。
Corner case
如果把set-group-id应用到可执行文件,那么表示执行该文件时,该进程的effective group id将被设置为该文件的group id。
当把set-user-id应用到可执行文件时,那么表示执行该文件时,该进程的effective user id将被设置为该文件的user id。
那么把set-user-id应用到目录呢?在linux上,尽管目录的user executable bit会显示为s,但是没有实质的作用。根据GNU-gcc的描述,在有些系统上,它与set-group-id在目录上的作用类似:即任何用户在该目录下创建的文件都会与该目录所属的user相同,方便文件共享。
On most systems, if a directory’s set-group-ID bit is set, newly created subfiles inherit the same group as the directory, and newly created subdirectories inherit the set-group-ID bit of the parent directory. On a few systems, a directory’s set-user-ID bit has a similar effect on the ownership of new subfiles and the set-user-ID bits of new subdirectories. These mechanisms let users share files more easily, by lessening the need to use chmod or chown to share new files.
[1]: Linux programming interface 15.3.1