我知道setgid是如何工作的,但我不知道它为什么被devise出来,是否有任何例子来说明它解决了什么问题?
虽然setgid文件/二进制文件可能不是很有用,但我一定会发现setgid位在目录上非常有用。 假设你是不同工作组的一部分,每个工作组都有自己的unix(许可)组。 当然,你会希望把setgid位放在项目文件夹中,确保在创build新文件时应用正确的组所有权,从而允许该项目组中的同事访问这些文件?
主要用途是保存文件树的组所有者:
[lockie@bubbles tmp]$ mkdir dir1 && touch dir1/file && mkdir dir1/dir [lockie@bubbles tmp]$ mkdir dir2 && chgrp staff dir2 && chmod 2755 dir2 && touch dir2/file && mkdir dir2/dir [lockie@bubbles tmp]$ ls -al dir1 total 32 drwxrwxr-x 3 lockie lockie 4096 Dec 13 19:32 . drwxrwxrwt 125 root root 20480 Dec 13 19:32 .. drwxrwxr-x 2 lockie lockie 4096 Dec 13 19:32 dir -rw-rw-r-- 1 lockie lockie 0 Dec 13 19:32 file [lockie@bubbles tmp]$ ls -al dir2 total 32 drwxr-sr-x 3 lockie staff 4096 Dec 13 19:32 . drwxrwxrwt 125 root root 20480 Dec 13 19:32 .. drwxrwsr-x 2 lockie staff 4096 Dec 13 19:32 dir < note new dir is g+s, owned by "staff" group, so the setgid behaviour acts recursively -rw-rw-r-- 1 lockie staff 0 Dec 13 19:32 file < note new file is owned by "staff" group [lockie@bubbles tmp]$
在不同的用户将在一个目录下创build/编辑文件/目录的环境中,这样做通常很有用:当所有文件/目录共享同一个组时,所有用户都可以编辑/更改文件/目录(权限允许)如“xyz拥有文件abc,所以我不能编辑它”。
以这种方式使用setgid的替代方法是grpid文件系统挂载选项。
从人山上:
grpid or bsdgroups / nogrpid or sysvgroups These options define what group id a newly created file gets. When grpid is set, it takes the group id of the directory in which it is created; otherwise (the default) it takes the fsgid of the current process, unless the directory has the setgid bit set, in which case it takes the gid from the parent directory, and also gets the setgid bit set if it is a directory itself.
启用时,在grpid挂载的文件系统上创build的文件/目录也会inheritance父目录的组:
[lockie@bubbles ~]$ mount | grep /home /dev/mapper/VolGroup00-home on /home type ext3 (rw,grpid) [lockie@bubbles ~]$ mkdir dir3 && touch dir3/file && mkdir dir3/dir [lockie@bubbles ~]$ ls -al dir3 total 12 drwxrwxr-x 3 lockie users 4096 Dec 13 19:37 . drwxrwxr-x 12 lockie users 4096 Dec 13 19:37 .. drwxrwxr-x 2 lockie users 4096 Dec 13 19:37 dir < inherited "users" group from parent dir -rw-rw-r-- 1 lockie users 0 Dec 13 19:37 file < inherited "users" group from parent dir [lockie@bubbles ~]$
我发现使用grpid选项适当地减less了人为错误的机会(因为文件系统做的工作,无论目录权限)。
要详细了解setgid,setuid和sticky位,请查看下面的链接
高级Linux文件权限