Linux - 目录管理之群组与权限继承

54 篇文章 21 订阅

Linux - 目录管理之群组与权限继承


提示:写完文章后,目录可以自动生成,如何生成可参考右边的帮助文档


前言


提示:以下是本篇文章正文内容,下面案例可供参考

一、需求

项目目录下,创建的文件或目录,需要继承为父目录群组权限

二、解决

继承父目录的群组

执行chmod g+s <FOLDER>chmod +2000 <FOLDER>命令,随后在<FOLDER>目录下新建的

  • 文件,都继承<FOLDER>的群组;
  • 文件夹,都继承<FOLDER>的群组,并继承set-group-ID bit

效果如下

[thesre@centos8 test_set-group-ID]$ ls -ld /home/thesre/test_set-group-ID
drwxrwxr-x 2 thesre stapusr 4096 Jun  2 21:09 /home/thesre/test_set-group-ID
[thesre@centos8 test_set-group-ID]$ mkdir subdir01; touch subfile01 # 创建测试文件夹与文件
[thesre@centos8 test_set-group-ID]$ ls -ld subdir01 subfile01 # 查看,未继承父目录群组,是主群组thesre
drwxrwxr-x 2 thesre thesre 4096 Jun  2 21:10 subdir01
-rw-rw-r-- 1 thesre thesre    0 Jun  2 21:10 subfile01
[thesre@centos8 test_set-group-ID]$
[thesre@centos8 test_set-group-ID]$ chmod +2000 /home/thesre/test_set-group-ID # 加上set-group-ID bit
[thesre@centos8 test_set-group-ID]$ ls -ld /home/thesre/test_set-group-ID # 检查已设置成功
drwxrwsr-x 3 thesre stapusr 4096 Jun  2 21:10 /home/thesre/test_set-group-ID
[thesre@centos8 test_set-group-ID]$ mkdir subdir02; touch subfile02 # 创建测试文件夹与文件
[thesre@centos8 test_set-group-ID]$ ls -ld subdir02 subfile02 # 查看,已继承父目录群组stapusr
drwxrwsr-x 2 thesre stapusr 4096 Jun  2 21:11 subdir02
-rw-rw-r-- 1 thesre stapusr    0 Jun  2 21:11 subfile02
[thesre@centos8 test_set-group-ID]$
[thesre@centos8 test_set-group-ID]$ ls -al 
total 16
drwxrwsr-x 4 thesre stapusr 4096 Jun  2 21:11 .
drwx------ 7 thesre thesre  4096 Jun  2 21:08 ..
drwxrwxr-x 2 thesre thesre  4096 Jun  2 21:10 subdir01
drwxrwsr-x 2 thesre stapusr 4096 Jun  2 21:11 subdir02
-rw-rw-r-- 1 thesre thesre     0 Jun  2 21:10 subfile01
-rw-rw-r-- 1 thesre stapusr    0 Jun  2 21:11 subfile02

继承父目录的权限

通过配置ACL,来使得子文件(夹)继承父目录权限

[thesre@centos8 ~]$ mkdir test_facl
[thesre@centos8 ~]$ cd !$
cd test_facl
[thesre@centos8 test_facl]$ ls 
[thesre@centos8 test_facl]$ ls -al
total 8
drwxrwxr-x 2 thesre thesre 4096 Jun  2 21:38 .
drwx------ 8 thesre thesre 4096 Jun  2 21:38 ..
[thesre@centos8 test_facl]$ setfacl -b . # 清理可能存在的ACL配置
[thesre@centos8 test_facl]$ getfacl . # 查看ACL
# file: .
# owner: thesre
# group: thesre
user::rwx
group::rwx
other::r-x

[thesre@centos8 test_facl]$ setfacl -R -d -m o::- . # 设置ACL的other默认值
[thesre@centos8 test_facl]$ setfacl -R -d -m g::rwx . # 设置ACL的group默认值
[thesre@centos8 test_facl]$ getfacl . # 查看ACL
# file: .
# owner: thesre
# group: thesre
user::rwx
group::rwx
other::r-x
default:user::rwx
default:group::rwx
default:other::---

[thesre@centos8 test_facl]$ mkdir subdir01 # 创建文件夹
[thesre@centos8 test_facl]$ touch subfile01 # 创建文件
[thesre@centos8 test_facl]$ ls -ld subdir01 subfile01 # 查看权限,可以看到已生效:文件与文件夹权限均为ACL定义的。
drwxrwx---+ 2 thesre thesre 4096 Jun  2 21:40 subdir01
-rw-rw----  1 thesre thesre    0 Jun  2 21:41 subfile01
[thesre@centos8 test_facl]$ getfacl subdir01 # 查看子文件夹的ACL继承情况
# file: subdir01
# owner: thesre
# group: thesre
user::rwx
group::rwx
other::---
default:user::rwx
default:group::rwx
default:other::---

[thesre@centos8 test_facl]$ getfacl subfile01 # 查看文件的ACL继承情况
# file: subfile01
# owner: thesre
# group: thesre
user::rw-
group::rw-
other::---


总结

提示:这里对文章进行总结。

参考资料

http://www.gnu.org/software/coreutils/manual/html_node/Directory-Setuid-and-Setgid.html //GNU网站介绍
https://www.stigviewer.com/stig/ibm_aix_7.x/2020-02-24/finding/V-91593 //mount选项打开或关闭设置set-group-ID bit的选项nosuid(此项关于安全)。
https://man7.org/linux/man-pages/man5/acl.5.html //acl扩展控制列表的详细介绍
https://unix.stackexchange.com/a/1315
https://unix.stackexchange.com/a/331167
https://serverfault.com/a/708163 //这篇thread提到ACL或ZFS文件系统,都可以实现权限的继承
https://linux.die.net/man/8/mount //mount选线打开或关闭ACL的支持noacl

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

王万林 Ben

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值