linux系统中的特殊权限之SGID(Set GID)


***linux系统中的特殊权限之SUID(Set UID)***
***linux系统中的特殊权限之SGID(Set GID)***
***linux系统中的特殊权限之SBIT(Sticky BIT)***

前言

之前,简单的讲解了SUID,有需要的可以使用上面的链接跳转,查看关于SUID的相关知识。
下面,来为大家介绍SGID这一特殊权限。

SGID(Set GID)

SGID的作用

SGID最常见的作用对象是目录,即对目录添加SGID权限。
当一个目录的属组拥有w权限(写权限),并且拥有SGID权限时,所有属于此目录的属组的用户,在以属组的身份在这个目录中新建文件(包括目录)时,新文件的属组不是用户的基本组,而是和此目录的属组相同。
光看概念的话可能不好理解,下面还是以实例来讲解。

实例讲解

默认情况(不使用SGID)

假设tom和jerry属于同一工作组,他们使用的文件,都放在同一个目录下,可以这样设置;

  • #创建一个新的组,并将tom和jerry添加到组中
[root@centos7u6 tmp]# groupadd  workgroup
[root@centos7u6 tmp]# usermod -a -G workgroup tom
[root@centos7u6 tmp]# usermod -a -G workgroup jerry
[root@centos7u6 tmp]# id tom
uid=1004(tom) gid=1004(tom) groups=1004(tom),1006(workgroup)
[root@centos7u6 tmp]# id jerry
uid=1005(jerry) gid=1005(jerry) groups=1005(jerry),1006(workgroup)
  • #创建一个共享目录,并将目录的属组改为workgroup
[root@centos7u6 tmp]# mkdir /tmp/test_SGID
[root@centos7u6 tmp]# ls -ld test_SGID/
drwxr-xr-x 2 root root 6 Mar 30 11:09 test_SGID/
[root@centos7u6 tmp]# chown :workgroup /tmp/test_SGID/
[root@centos7u6 tmp]# ls -ld test_SGID/
drwxr-xr-x 2 root workgroup 6 Mar 30 11:09 test_SGID/
  • #需要给/tmp/test_SGID目录的属组赋予写权限,否则,tom和jerry无法在该目录中新建文件
[tom@centos7u6 test_SGID]$ touch /tmp/test_SGID/tom_test
touch: cannot touch ‘/tmp/test_SGID/tom_test’: Permission denied
  • #分别使用tom和jerry用户在/tmp/test_SGID目录中创建文件
[tom@centos7u6 ~]$ echo "tom" > /tmp/test_SGID/tom.test
[tom@centos7u6 ~]$ cat /tmp/test_SGID/tom.test
tom

[jerry@centos7u6 test_SGID]$ echo "jerry" > /tmp/test_SGID/jerry.test
[jerry@centos7u6 test_SGID]$ cat /tmp/test_SGID/jerry.test 
jerry
  • #查看被创建的文件的属组,分别为tom和jerry的基本组
[root@centos7u6 test_SGID]# ls  -l 
total 8
-rw-rw-r-- 1 jerry jerry 6 Mar 30 13:16 jerry.test
-rw-rw-r-- 1 tom   tom   4 Mar 30 13:14 tom.test

以上是默认不使用SGID的情况,但是这种情况下,用户tom只能查看jerry.test,但是无法修改jerry.test的内容。

[root@centos7u6 test_SGID]# su tom
[tom@centos7u6 test_SGID]$ cat jerry.test 
jerry
[tom@centos7u6 test_SGID]$ echo "tom2" >> jerry.test 
bash: jerry.test: Permission denied

现在,当tom和jerry需要可以互相修改对方的文件时,SGID便派上用场了。

使用SGID权限

  • #现在赋予/tmp/test_SGID目录特殊权限SGID
    注意,拥有SGID权限后,文件属组的x权限(执行权限)位,变成了小s
drwxrwxr-x 2 root workgroup 40 Mar 30 13:27 /tmp/test_SGID/
[root@centos7u6 test_SGID]# chmod g+s /tmp/test_SGID/
[root@centos7u6 test_SGID]# ls -ld /tmp/test_SGID/
drwxrwsr-x 2 root workgroup 40 Mar 30 13:27 /tmp/test_SGID/
  • #现在我们再分别用tom和jerry创建文件,并查看文件的属性
    可以看到新创建的文件的属主不变,但是属组不再是用户的基本组,而是变成了/tmp/tset_SGID目录的属组
[tom@centos7u6 test_SGID]$ echo "tom2" > /tmp/test_SGID/tom2.test

[jerry@centos7u6 test_SGID]$ echo "jerry2" > /tmp/test_SGID/jerry2.test
[root@centos7u6 test_SGID]# ls -l /tmp/test_SGID/
total 16
-rw-rw-r-- 1 jerry workgroup 5 Mar 30 13:40 jerry2.test
-rw-rw-r-- 1 jerry jerry     6 Mar 30 13:27 jerry.test
-rw-rw-r-- 1 tom   workgroup 5 Mar 30 13:40 tom2.test
-rw-rw-r-- 1 tom   tom       4 Mar 30 13:14 tom.test
  • 现在,tom和jerry可以互相修改对方创建的文件的内容了
[tom@centos7u6 test_SGID]$ echo "tom is coming..." >> jerry2.test 
[tom@centos7u6 test_SGID]$ cat jerry2.test 
[tom@centos7u6 test_SGID]$ cat jerry2.test 
jerry2
tom is coming...
chmod
[jerry@centos7u6 test_SGID]$ echo "jerry is coming" >> /tmp/test_SGID/tom2.test 
[jerry@centos7u6 test_SGID]$ cat tom2.test 
tom2
jerry is coming

通过对比,我们应该能更清晰的看到SGID的作用了。

SGID的用法

同之前的SUID类似:

  • 第一种方法:
    chmod g+|-s FILENAME
    • 设置权限:chmod g+s /tmp/test
    • 取消:chmod g-s /tmp/test
  • 第二种方法:
    使用8进制赋权
    • 设置权限:chmod 2775 /tmp/test
    • 取消:chmod 775 /tmp/test
SUIDSGIDSBIT八进制权限
0000
0011
0102
0113
1004
1015
1106
1117

判断是否有SGID权限

SGID的权限展示位,在文件属组的x权限(执行权限)位上。

  • 如果有SGID
    • 属组原本有执行权限,则属组的执行权限位,显示为小s
    • 属组原本没有执行权限,则属组的执行权限位,显示为大写S
  • 如果没有SUID
    • 属组的执行权限位,仍然按其基本权限,显示为x或者-
[root@centos7u6 test_SGID]# ls -ld /tmp/test_SGID/
drwxrwsr-x 2 root workgroup 40 Mar 30 13:27 /tmp/test_SGID/

加点小料:locate命令

在linux系统中,SGID多用于目录,但是也可以用于文件,locate命令就是其中一个。

  • #locate命令文件的存放路径如下,文件的属主是root,属组为是locate。
[root@centos7u6 test_SGID]# ls /usr/bin/locate -l
-rwx--s--x 1 root slocate 40520 Apr 11  2018 /usr/bin/locate

locate命令查找是基于对数据库中索引的搜索完成的,数据库的路径和名称是/var/lib/mlocate/mlocate.db,可以看到mlocate.db文件的属主是root,属组为slocate。

[root@centos7u6 ~]# ls -l /var/lib/mlocate/mlocate.db 
-rw-r----- 1 root slocate 597052 Mar 30 14:32 /var/lib/mlocate/mlocate.db

由于locate命令文件拥有SGID权限,所以在一般用户tom去使用locate命令查找,发起一个进程时,tom会临时被添加到slocate组中,而slocate组中的用户对mlocate.db文件有r权限(读权限),所以这个进程可以读取数据库文件中的内容,完成查找操作。

[tom@centos7u6 ~]$ locate fstab
/etc/fstab
/usr/lib/dracut/modules.d/95fstab-sys
/usr/lib/dracut/modules.d/95fstab-sys/module-setup.sh
/usr/lib/dracut/modules.d/95fstab-sys/mount-sys.sh
/usr/lib/systemd/system-generators/systemd-fstab-generator
/usr/share/man/man5/fstab.5.gz
/usr/share/man/man8/systemd-fstab-generator.8.gz

如果,我们将locate命令SGID权限取消,那么tom在去执行locate命令,则不会被临时添加至slocate组中,当这个进程以other用户的身份去访问数据库时,会因为没有read权限而被拒绝,最终导致查找操作失败。

[root@centos7u6 ~]# chmod g-s /usr/bin/locate 
[root@centos7u6 ~]# ls -l /usr/bin/locate
-rwx--x--x 1 root slocate 40520 Apr 11  2018 /usr/bin/locate
[root@centos7u6 ~]# su tom
[tom@centos7u6 root]$ locate fstab
locate: can not stat () `/var/lib/mlocate/mlocate.db': Permission denied
[tom@centos7u6 root]$ 

注: 测试完后,记得将其修改回去。

使用SGID的注意事项

  • 可以针对二进制文件使用
    • 文件的属组需要有x权限
    • SGID的生命周期:当这个程序运行为进程时SGID有效,进程结束后,SGID权限也随之消失
  • 可以针对目录使用
    • 目录的属组需要有r权限,这样才能进入到目录
    • 目录的属组需要拥有w权限,这样用户才可以在目录中创建文件
    • 目录的属组需要拥有x权限 ,这样SGID才能生效
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值