权限位、umask、ACL 掩码
目标
在一个目录中依次配置八进制模式、符号模式、umask、特殊位、ACL 与 mask,并亲眼确认它们之间的相互作用。
为什么重要
权限问题通常从“为什么不工作”开始,最后以 chmod 777 结束。这种习惯之所以无法通过审计,原因很简单:真正需要的是为一个人设置例外,却向所有人打开了大门。当需求无法用三个位置(owner、group、other)表达时,应判断是使用 ACL 而不是新建 group;多人向同一目录写入文件时,应判断使用 setgid 统一 group。这些是本领域的核心。mask 尤其重要。即使准确设置了 ACL,后来有人执行一次 chmod,mask 也会重新计算,使该 ACL 悄无声息地失效。能够读懂 getfacl 的 #effective: 标记,才能在 5 秒内识别这种情况。
步骤
- 创建目录
/root/lfcs-perm/,并创建 grouplfcsteam和用户lfcsone、lfcstwo。两个用户都必须将lfcsteam作为附加 group(primary group 是各自的个人 group)。 - 创建
/root/lfcs-perm/notes.txt,将权限设为 640,group 设为lfcsteam。 - 在
/root/lfcs-perm/run.sh中写入任意内容,使用符号模式将权限设为 750,group 设为lfcsteam。 - 在 umask 改为
0027的 shell 中,新建文件/root/lfcs-perm/umask-demo/newfile和目录/root/lfcs-perm/umask-demo/newdir。然后在/root/lfcs-perm/umask.txt中写三行:umask=0027、file=<새 파일의 권한>、dir=<새 디렉터리의 권한>。 - 创建
/root/lfcs-perm/probe,owner 为 root、权限为4755;再创建目录/root/lfcs-perm/tools/,并将权限设为1777。 - 先将
/root/lfcs-perm/shared/的 group 设为lfcsteam、权限设为2770,然后在其中创建handoff.txt。文件必须通过继承获得 grouplfcsteam。 - 为
handoff.txt设置 ACL:lfcsone为rw-,lfcstwo为r--;同时为shared/目录设置lfcsteamgroup 的 default ACLrwx。 - 将
handoff.txt的 ACL mask 降为r--,并在/root/lfcs-perm/mask.txt中写一行lfcsone_effective=<마스크 적용 후 lfcsone 의 실효 권한>。
参考
- 特殊位是四位八进制数的第一位:setuid 为 4,setgid 为 2,sticky 为 1。
- 第 6 步的顺序决定一切。在设置该位之前创建的文件不会继承 group。
- 只有 mask 实际削减权限时,
getfacl才会添加#effective:。 - 常见错误:设置 ACL 后再执行
chmod,group 位会被解释为 mask,导致 ACL 失效。
准备协作用 group 和用户
创建目录 /root/lfcs-perm/,并创建 group lfcsteam 和用户 lfcsone、lfcstwo。两个用户都必须将 lfcsteam 作为附加 group(primary group 是各自的个人 group)。
两个用户都必须将团队 group 作为附加 group。如果连 primary group 也设为团队 group,后续的继承实验将失去意义。
八进制模式与 group ownership
创建 /root/lfcs-perm/notes.txt,将权限设为 640,group 设为 lfcsteam。
权限和 group 由不同命令设置。640 表示 owner 可读写、group 可读、other 无权限。
使用符号模式设置权限
在 /root/lfcs-perm/run.sh 中写入任意内容,使用符号模式将权限设为 750,group 设为 lfcsteam。
符号模式由对象(u/g/o/a)、运算符(+/-/=)和权限(r/w/x)组合。等号会清除其余权限,只保留指定值。
umask 如何削减默认权限
在 umask 改为 0027 的 shell 中,新建文件 /root/lfcs-perm/umask-demo/newfile 和目录 /root/lfcs-perm/umask-demo/newdir。然后在 /root/lfcs-perm/umask.txt 中写三行:umask=0027、file=<새 파일의 권한>、dir=<새 디렉터리의 권한>。
文件基准权限是 666,目录基准权限是 777,再从中去掉 umask 位。不要事后用 chmod 调整,而应在改变 umask 的 shell 中重新创建。
setuid 与 sticky 位
创建 /root/lfcs-perm/probe,owner 为 root、权限为 4755;再创建目录 /root/lfcs-perm/tools/,并将权限设为 1777。
特殊位是四位八进制数的第一位。setuid 为 4,setgid 为 2,sticky 为 1。对于 setuid,关键在于 owner 是谁。
使用 setgid 的协作目录
先将 /root/lfcs-perm/shared/ 的 group 设为 lfcsteam、权限设为 2770,然后在其中创建 handoff.txt。文件必须通过继承获得 group lfcsteam。
顺序很重要。先在目录上设置该位并配置 group,然后在其中创建文件,才会发生继承。
命名用户 ACL 与 default ACL
为 handoff.txt 设置 ACL:lfcsone 为 rw-,lfcstwo 为 r--;同时为 shared/ 目录设置 lfcsteam group 的 default ACL rwx。
在文件上设置单独用户条目,在目录上设置将由今后新建文件继承的条目。后者需要单独的 flag。
mask 会削减有效权限
将 handoff.txt 的 ACL mask 降为 r--,并在 /root/lfcs-perm/mask.txt 中写一行 lfcsone_effective=<마스크 적용 후 lfcsone 의 실효 권한>。
mask 是除 owner 和 other 之外所有条目的权限上限。mask 有单独的条目表示法;降低后,getfacl 会同时显示有效权限。