linux系统权限管理拓展:特殊权限

文件与目录权限设置不止读、写、执行这些,还有所谓的特殊权限,由于特殊权限会拥有一些“特权”;

1 2 3 4
本章内容 SUID SGID SBIT 文件扩展权限ACL

1、SUID

作用:程序运行时执行者拥有程序属主(owner)的权限
限制1:SUID权限仅对二进制程序(binary program)有效,即系统中的一些命令,不能用在脚本上;
限制2:对目录设置无效
限制3:执行者对于该程序需要具有x的可执行权限;
表现形式:owner身份的x权限上显示s,如:/usr/bin/passwd这个文件的权限状态:“-rwsr-xr-x.”

[test1@localhost ~]$ ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27832 6月  10 2014 /usr/bin/passwd

2、SGID

作用:①程序运行时执行者拥有程序属组(group)的权限;
②在设置了SGID权限的目录下建立文件时,新创建的文件的所属组会,继承上级目录的所属组;
限制1:既可以给二进制可执行程序设置,也可以对目录设置,SGID主要用在目录上;
限制2:程序执行者对于该程序来说,需具备x的权限;
表现形式:属组(group)身份的x权限上显示s 如: -rwxrwsrwx 1 test1 test1 0 5月 13 10:21 dirname

[test1@localhost ~]$ chmod g+s 2
[test1@localhost ~]$ ll
总用量 0
-rwxrwsrwx 1 test1 test1 0 5月  13 10:21 2

3、SBIT

作用:当用户在该目录下建立文件或目录时,仅有owner与 root才有权力删除(针对other身份);
限制1:针对目录设置,对文件不起作用;
表现形式:其他人身份(other)的x权限上显示t 如:drwxrwxrwt. 43 root root 4096 5月 13 15:48 /tmp

[test1@localhost ~]$ ls -ld /tmp
drwxrwxrwt. 43 root root 4096 5月  13 15:48 /tmp

权限设置

SUID:chmod u+s  
SGID:chmod  g+s
SBIT:chmod o+t

数字模式就是四位数字中的第一位代表特殊权限(suid=4、sgid=2、sbit=1);不建议用数字模式因为有时会不好用

SUID:chmod 4777
SGID:chmod  2777
SBIT:chmod 1777       # 其中777指代三种基本权限(r、w、x)设置,因情况设定;

演示

SUID

╭─root@localhost.localdomain ~  
╰─➤  ll `which passwd`
-rwsr-xr-x. 1 root root 28K 6月  10 2014 /bin/passwd        #可以看出passwd已经设置了suid
╭─root@localhost.localdomain ~  
╰─➤  chmod u-s /bin/passwd           #删除suid的设置
╭─root@localhost.localdomain ~  
╰─➤  ll /bin/passwd
-rwxr-xr-x. 1 root root 28K 6月  10 2014 /bin/passwd    #可以看出已删除
╭─root@localhost.localdomain ~  
╰─➤  su - du      切换到du用户
上一次登录:一 5月 13 16:26:41 CST 2019pts/0 上
[du@localhost ~]$ passwd           #使用passwd修改du用户密码
更改用户 du 的密码 。
新的 密码:
重新输入新的 密码:
passwd: 鉴定令牌操作错误        #因为du用户没有权限所以不成功
[du@localhost ~]$ su - root
密码:
上一次登录:一 5月 13 18:09:08 CST 2019pts/0 上
╭─root@localhost.localdomain ~  
╰─➤  chmod u+s /bin/passwd    #给du用户设置suid
╭─root@localhost.localdomain ~  
╰─➤  ll /bin/passwd
-rwsr-xr-x. 1 root root 28K 6月  10 2014 /bin/passwd     #设置成功
╭─root@localhost.localdomain ~  
╰─➤  su - du
上一次登录:一 5月 13 18:09:50 CST 2019pts/0 上
[du@localhost ~]$ passwd
更改用户 du 的密码 。
新的 密码:
重新输入新的 密码: 
passwd:所有的身份验证令牌已经成功更新。     #使用passwd设置成功

SGID

╭─root@localhost.localdomain /home  
╰─➤  ll
总用量 4.0K
drwx------. 14 du du 4.0K 5月  13 17:59 du     
╭─root@localhost.localdomain /home  
╰─➤  cd du
╭─root@localhost.localdomain /home/du  
╰─➤  touch testfile1
╭─root@localhost.localdomain /home/du  
╰─➤  mkdir testdir1
╭─root@localhost.localdomain /home/du  
╰─➤  ll
总用量 0
drwxr-xr-x. 2 root root  6 5月  13 18:49 testdir1     #属组还是创建者的属组
-rw-r--r--. 1 root root  0 5月  13 18:49 testfile1       #属组还是创建者的属组
╭─root@localhost.localdomain /home/du    
╰─➤  cd ..
╭─root@localhost.localdomain /home  
╰─➤  chmod 2700 du          #数字形式设置sgid
╭─root@localhost.localdomain /home  
╰─➤  ll
总用量 4.0K
drwx--S---. 15 du du 4.0K 5月  13 18:49 du
╭─root@localhost.localdomain /home  
╰─➤  cd du
╭─root@localhost.localdomain /home/du  
╰─➤  touch testfile2
╭─root@localhost.localdomain /home/du  
╰─➤  mkdir testdir2
╭─root@localhost.localdomain /home/du  
╰─➤  ll
总用量 0
drwxr-xr-x. 2 root root  6 5月  13 18:49 testdir1
drwxr-sr-x. 2 root du    6 5月  13 18:51 testdir2     #属组继承了设置了sgid父目录du的属组
-rw-r--r--. 1 root root  0 5月  13 18:49 testfile1
-rw-r--r--. 1 root du    0 5月  13 18:50 testfile2      #属组继承了设置了sgid父目录du的属组

SBIT

╭─root@localhost.localdomain /home/testdir1  
╰─➤  touch testfile{1..3}
╭─root@localhost.localdomain /home/testdir1  
╰─➤  mkdir testdir{1..3}
╭─root@localhost.localdomain /home  
╰─➤  su - user1
上一次登录:一 5月 13 19:09:09 CST 2019pts/0 上
[user1@localhost ~]$ cd /home/testdir1
[user1@localhost testdir1]$ rm test*
rm: 无法删除"testdir1": 是一个目录
rm: 无法删除"testdir2": 是一个目录
rm: 无法删除"testdir3": 是一个目录
rm:是否删除有写保护的普通空文件 "testfile1"?y
rm:是否删除有写保护的普通空文件 "testfile2"?y
rm:是否删除有写保护的普通空文件 "testfile3"?y
[user1@localhost testdir1]$ ll
总用量 0
drwxr-xr-x. 2 root root 6 5月  13 19:08 testdir1
drwxr-xr-x. 2 root root 6 5月  13 19:08 testdir2
drwxr-xr-x. 2 root root 6 5月  13 19:08 testdir3
[user1@localhost testdir1]$ rm -r test*
rm:是否删除有写保护的目录 "testdir1"?y
rm:是否删除有写保护的目录 "testdir2"?y
rm:是否删除有写保护的目录 "testdir3"?y     # testdir1目录没有设置sbit,user用户可以删除root用户创建的文件
[user1@localhost testdir1]$ ll
总用量 0
[user1@localhost testdir1]$ su - root
密码:
上一次登录:一 5月 13 19:11:10 CST 2019pts/0 上
最后一次失败的登录:一 5月 13 19:14:30 CST 2019pts/0 上
最有一次成功登录后有 2 次失败的登录尝试。
╭─root@localhost.localdomain ~  
╰─➤  cd /home 
╭─root@localhost.localdomain /home  
╰─➤  ll
总用量 4.0K
drwx------. 16 du    du    4.0K 5月  13 18:51 du
drwxrwxrwx.  2 root  root     6 5月  13 19:13 testdir1
drwx------.  5 user1 user1  121 5月  13 19:09 user1
╭─root@localhost.localdomain /home  
╰─➤  chmod o+t testdir1          #给testdir1目录设置sbit                                                  
╭─root@localhost.localdomain /home  
╰─➤  ll
总用量 4.0K
drwx------. 16 du    du    4.0K 5月  13 18:51 du
drwxrwxrwt.  2 root  root     6 5月  13 19:13 testdir1
drwx------.  5 user1 user1  121 5月  13 19:09 user1
╭─root@localhost.localdomain /home  
╰─➤  cd testdir1
╭─root@localhost.localdomain /home/testdir1  
╰─➤  touch file{1..3}
╭─root@localhost.localdomain /home/testdir1  
╰─➤  su - user1
上一次登录:一 5月 13 19:11:39 CST 2019pts/0 上
[user1@localhost ~]$ cd ..
[user1@localhost home]$ cd testdir1
[user1@localhost testdir1]$ rm file*
rm:是否删除有写保护的普通空文件 "file1"?y
rm: 无法删除"file1": 不允许的操作
rm:是否删除有写保护的普通空文件 "file2"?y
rm: 无法删除"file2": 不允许的操作
rm:是否删除有写保护的普通空文件 "file3"?y
rm: 无法删除"file3": 不允许的操作             # user1用户不能删除root用户创建的文件
[user1@localhost testdir1]$ 

文件扩展权限ACL

作用:对特定的用户进行权限管控,
[root@cjk ~]# setfacl -m u:testuser:rw filename或dirname #u-->user ;指定testuser用户有特殊权限rw;-m表设置
[root@cjk ~]# getfacl filename或dirname #查看acl权限
[root@cjk ~]# setfacl -b filename或dirname #删除acl权限
[root@cjk ~]# setfacl -R -m u:du:rw- testdirectory/ #-R一定要在-m前面,表示目录下所有文件
[root@cjk ~]# setfacl -x u:du /tmp/a.txt # 去掉单个权限
[root@cjk ~]# setfacl -b /tmp/a.txt # 去掉所有acl权限

演示

╭─root@localhost.localdomain /home  
╰─➤  setfacl -m u:user1:x file1                                                  
╭─root@localhost.localdomain /home  
╰─➤  su - user1
上一次登录:一 5月 13 19:16:18 CST 2019pts/0 上
[user1@localhost ~]$ cd /home
[user1@localhost home]$ getfacl file1
# file: file1
# owner: root
# group: root
user::rwx
user:user1:--x
group::rwx
mask::rwx
other::rwx

[user1@localhost home]$ cat file1
cat: file1: 权限不够
[user1@localhost home]$ su - du
上一次登录:一 5月 13 19:51:51 CST 2019pts/0 上
[du@localhost ~]$ cat /home/file1
hello
[du@localhost ~]$ su - root 
密码:
上一次登录:一 5月 13 19:52:57 CST 2019pts/0 上
╭─root@localhost.localdomain ~  
╰─➤  setfacl -b /home/file1
╭─root@localhost.localdomain ~  
╰─➤  getfacl /home/file1
getfacl: Removing leading '/' from absolute path names
# file: home/file1
# owner: root
# group: root
user::rwx
group::rwx
other::rwx

原文地址:https://www.cnblogs.com/du-z/p/10848567.html