|
上次讲了基本权限管理中的chown、chgrp、chmod以及umask四个命令,今天我们来讲一讲权限最后的一点内容特殊权限位:SUID、SGID、Sticky以及facl
一、SUID
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
| 定义:运行某程序时,相应进程的属主是程序文件自身的属主,而不是启动者
赋予SUID位方法:chmod u+s FILE
撤销SUID位方法:chmod u-s FILE
注:如果FILE本身原来就有执行权限,则SUID显示为s;否则显示S
[iyunv@soysauce ~]# ll `which tail`
-rwxr-xr-x. 1 root root 57560 Nov 22 2013 /usr/bin/tail # 默认没有SUID位
[iyunv@soysauce ~]# su - user1 # 切换至user1用户
[user1@soysauce ~]$ tail -1 /etc/shadow # 此时tail进程的属主属组都为user1
tail: cannot open `/etc/shadow' for reading: Permission denied
[user1@soysauce ~]$ exit
logout
[iyunv@soysauce ~]# chmod u+s /usr/bin/tail # 赋予tail命令SUID位
[iyunv@soysauce ~]# ll /usr/bin/tail
-rwsr-xr-x. 1 root root 57560 Nov 22 2013 /usr/bin/tail
[iyunv@soysauce ~]# su - user1
[user1@soysauce ~]$ tail -1 /etc/shadow # 此时tail进程的属主属组都为root
user2:!!:16760:0:99999:7:::
|
过程详述:
当tail命令没有SUID位时,user1用户发起tail进程,此时tail这个进程的属主为user1,属组为user1用户所在的基本组,当这个tail进程访问/etc/shadow文件时,tail进程的属主user1既不是/etc/shadow文件的属主,也不属于shadow文件的属组,于是以其他人的权限访问这个shadow文件,因为其他人并没有任何权限,所以读取不了,会提示权限拒绝;当tail命令有了SUID位时,任何用户发起tail进程时,tail进程的属主变为了这个tail文件本身的属主,属组变为了tail文件本身的基本组,再次访问/etc/shadow文件时,tail进程的属主刚好是shadow文件的属主,于是便应用属主的权限来访问。
二、SGID
1
2
3
4
5
6
7
8
9
| 定义:运行某程序时,相应进程的属组是程序文件自身的属组,而不是启动者所属的基本组
赋予SGID位方法:chmod g+s FILE
撤销SGID位方法:chmod g-s FILE
注:如果FILE本身原来就有执行权限,则SUID显示为s;否则显示S
对一个目录赋予SGID权限位时,任何用户在这个目录下创建文件时,文件的属组是这个目录的属组,而不是用户的基本组
|
例:user1,user2,user3都属于Develop组,且都需要在/tmp/project目录下编辑这个Develop组内其他用户创建的文件,此时就需要用到SGID了
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
| [iyunv@soysauce tmp]# groupadd Develop
[iyunv@soysauce tmp]# usermod -a -G Develop user1 # 将user1、user2、user3添加到Develop组
[iyunv@soysauce tmp]# usermod -a -G Develop user2
[iyunv@soysauce tmp]# usermod -a -G Develop user3
[iyunv@soysauce tmp]# id user1 # 此时可以看到user1有个附加组Develop
uid=500(user1) gid=500(user1) groups=500(user1),503(Develop)
[iyunv@soysauce tmp]# chown :Develop project/
[iyunv@soysauce tmp]# ll
total 820
-rw-r--r-- 1 root root 832104 Sep 18 12:54 nginx-1.8.0.tar.gz
drwxrwxr-x 2 root Develop 4096 Nov 21 20:26 project
[iyunv@soysauce tmp]# su - user1 # 切换为user1用户
[user1@soysauce ~]$ cd /tmp/project/
[user1@soysauce project]$ touch a
[user1@soysauce project]$ ll
total 0
-rw-rw-r-- 1 user1 user1 0 Nov 21 20:31 a # a文件的属主属组都为user1
[user1@soysauce project]$ exit
logout
[iyunv@soysauce tmp]# su - user2 # 切换用户为user2
[user2@soysauce ~]$ cd /tmp/project/
[user2@soysauce project]$ echo "hello" >> a # 往a文件中添加一行hello
-bash: a: Permission denied # 其他用户并没有写权限
[user2@soysauce project]$ exit # 退出,切换为root用户
logout
[iyunv@soysauce tmp]# chmod g+s /tmp/project/ # 给project目录添加SGID
[iyunv@soysauce tmp]# ll -d /tmp/project/
drwxrwsr-x 2 root Develop 4096 Nov 21 20:31 /tmp/project/ # 此时project目录基本组已变为了Develop组
[iyunv@soysauce tmp]# su - user1 # 重新切换为user1用户
[user1@soysauce ~]$ touch /tmp/project/b # 创建一个新文件b
[user1@soysauce ~]$ ll /tmp/project/b
-rw-rw-r-- 1 user1 Develop 0 Nov 21 20:39 /tmp/project/b # b的基本组为Develop组
[user1@soysauce ~]$ exit
logout
[iyunv@soysauce tmp]# su - user2 # 切换为Develop组内的另一个用户user2
[user2@soysauce ~]$ echo "Hello" >> /tmp/project/b # 往b文件中写字符串Hello
[user2@soysauce ~]$ cat /tmp/project/b
Hello # 写入成功,此时Develop组内用户可以随意其他用户在/tmp/project目录下新创建的文件
|
三、Sticky
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
| 定义:在一个公共目录,每个都可以创建文件,删除自己的文件,但不能删除别人的文件
赋予Sticky位方法:chmod o+t DIR
撤销Sticky位方法:chmod o-t DIR
注:如果FILE本身原来就有执行权限,则SUID显示为s;否则显示S
对一个目录赋予Sticky位时,用户只能删除在该目录下自己创建的文件,并不能删除其他用户的文件
[user2@soysauce project]$ ll
total 4
-rw-rw-r-- 1 user1 Develop 0 Nov 21 20:31 a
-rw-rw-r-- 1 user1 Develop 6 Nov 21 20:40 b
[user2@soysauce project]$ rm -rf b # 已经删除成功
[user2@soysauce project]$ touch c
[user2@soysauce project]$ ll
total 0
-rw-rw-r-- 1 user1 Develop 0 Nov 21 20:31 a
-rw-rw-r-- 1 user2 Develop 0 Nov 21 20:53 c
[user2@soysauce project]$ exit
logout
[iyunv@soysauce tmp]# chmod o+t /tmp/project/ # 给project目录添加Sticky权限位
[iyunv@soysauce tmp]# su - user1
[user1@soysauce ~]$ rm -rf /tmp/project/c # 删除user2用户创建的文件,提示不允许
rm: cannot remove `/tmp/project/c': Operation not permitted
[user1@soysauce ~]$ rm -rf /tmp/project/a # 可以删除自己创建的文件,并不能删除其他用户的文件
|
四、facl
FACL(Filesystem Access Control List):利用文件扩展保存额外的访问控制权限
- 设置facl命令:setfacl [-R] -m|-x u|g:USER:MODE file_name
- 查看facl命令:getfacl file_name
1.设置facl权限命令:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [iyunv@soysauce test]# touch test.txt
[iyunv@soysauce test]# ll
total 0
-rw-r--r-- 1 root root 0 Nov 21 22:48 test.txt
[iyunv@soysauce test]# su - user2
[user2@soysauce ~]$ echo "hello,world" >> /tmp/test/test.txt
-bash: /tmp/test/test.txt: Permission denied # user2用户对test文件并没有写权限
[user2@soysauce ~]$ exit
logout
[iyunv@soysauce test]# setfacl -m u:user2:rw /tmp/test/test.txt # 设置user2用户有读写权限
[iyunv@soysauce test]# getfacl /tmp/test/test.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/test/test.txt
# owner: root
# group: root
user::rw-
user:user2:rw- # 可以看到user2用户对test.txt文件已经有了读写权限
group::r--
mask::rw-
other::r--
[iyunv@soysauce test]# su - user2
[user2@soysauce ~]$ echo "hello,world" >> /tmp/test/test.txt
[user2@soysauce ~]$ cat /tmp/test/test.txt
hello,world # facl生效,写入内容成功
|
2.取消facl权限
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
| [iyunv@soysauce test]# getfacl /tmp/test/test.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/test/test.txt
# owner: root
# group: root
user::rw-
user:user2:rw- # 可以看到user2用户对test.txt文件已经有了读写权限
group::r--
mask::rw-
other::r--
[iyunv@soysauce test]# setfacl -x u:user2 /tmp/test/test.txt
[iyunv@soysauce test]# getfacl /tmp/test/test.txt
getfacl: Removing leading '/' from absolute path names
# file: tmp/test/test.txt
# owner: root
# group: root
user::rw-
group::r--
mask::r--
other::r--
[iyunv@soysauce test]# su - user2
[user2@soysauce ~]$ echo "hello,python" >> /tmp/test/test.txt
-bash: /tmp/test/test.txt: Permission denied # facl规则取消了,所以user2便不能写入
|
|
|
|