Linux 文件系统上的权限

栏目: 后端 · 发布时间: 8年前

内容简介:Linux 文件系统上的权限

文件系统上的权限是指文件系统上的文件和目录的权限,由于 Linux 是一种多用户的操作系统,而且允许同一时间登录多个用户操作,所以我们就需要一定的管理机制来对限定不同用户对同一文件或目录的操作权限。

文件系统上的权限主要针对三类对象(访问者)进行定义:
owner:属主 u; g 属组 group; o: 其它 other
每个文件针对每类访问者都定义了三种权限:r:读 w:写 x:执行

1)对文件来说:

r: 可读,可以使用类似cat等命令查看文件内容

w: 可写,可以编辑此文件,并修改内容

x: 可执行,eXecutable,可以在命令提示符下当作命令提交给内核运行

[root@centos7 ~]#ll bin/backup.sh     
-rwxr-xr-x. 1 root root 43 Jun  2 17:48 bin/backup.sh

以上信息可以看出,root用户对backup.sh脚本文件有读写执行的权限,而root组的成员和其它用户有读和执行的权限

2)对于目录的权限,我们可以通过一系列实验来总结出来,比如先设定其它用户对目录是只读权限,再看其它用户对目录及其下级文件的各个操作权限(先不考虑目录下的文件的权限影响,假定对其它用户有rwx权限)

[root@centos7 ~]#ll /app
total 4
-rw-r--r--. 1 root root  0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root 21 Jun  4 11:55 test.txt

对于/app目录来,ffu用户为其它用户,对目录只读

1.ffu是否可以查看目录及下级文件呢?

[ffu@centos7 ~]$cd /app
-bash: cd: /app: Permission denied
[ffu@centos7 ~]$ls -l /app
ls: cannot access /app/test1.txt: Permission denied
ls: cannot access /app/test.txt: Permission denied
total 0
-????????? ? ? ? ?            ? test1.txt
-????????? ? ? ? ?            ? test.txt
[ffu@centos7 ~]$cat /app/test.txt
cat: /app/test.txt: Permission denied

可以看出ffu可以执行ls以列出目录下的文件列表,但不能cd如目录和查看文件信息及内容

2.ffu是否能对目录下级文件进行修改呢?

[ffu@centos7 ~]$vi /app/test.txt
                                                                     
~                                                                                  
"/app/test.txt" [Permission Denied]                              0,0-1         All
[ffu@centos7 ~]$echo hehe >> /app/test.txt
-bash: /app/test.txt: Permission denied
[ffu@centos7 ~]$touch /app/test2.txt
touch: cannot touch ‘/app/test2.txt’: Permission denied

可以看出ffu不能对目录下的文件进行修改,也不能创建新文件

3.ffu是否能删除目录下级文件呢?

[ffu@centos7 ~]$rm /app/test.txt
rm: cannot remove ‘/app/test.txt’: Permission denied

可以看出ffu没有权限删除/app目录下的文件

类似的我们也可以设置/app目录对其它用户权限为-w-;–r;rw-…总结如下:

Linux 文件系统上的权限

从上面的实验结果,我们可以知道对目录来说:
r: 可以对此目录执行ls以列出内部的所有文件,但不能访问文件
w: 可以在此目录创建文件,修改文件名字,删除目录下的文件,但是要配合x权限才生效
x: 可以使用cd切换进此目录,但可以cat /file 访问查看文件(已知文件名);配合r权限可使用ls -l查看内部文件的详细信息
这里可以看出能不能删除文件是与文件权限是没有关系的,而是与其上级目录权限有关。实际上,因为目录的数据块储存的是其下级的文件名,所以我们删除的只是文件名,文件内容的数据块还在。
对于目录来说,还有个X权限,是x的特殊形式,它允许只对目录及下级目录加执行权限,而文件不会增加。举例来说:
要对目录/app及下级目录/dir/dir1增加对其它用户的执行权限,而对文件不加

[root@centos7 app]#ll -d /app
drwxr-xr--. 5 root root 4096 Jun  4 13:13 /app
[root@centos7 app]#ll /app
total 16
drwxr--r--. 2 root root 4096 Jun  4 13:06 dir
drwxr--r--. 2 root root 4096 Jun  4 13:13 dir2
-rw-r--r--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#chmod -R o+X /app
[root@centos7 app]#ll /app          
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r--r--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#ll -d /app
drwxr-xr-x. 5 root root 4096 Jun  4 13:13 /app

这里其实是有一个条件的:目录下文件ugo都没有x权限才可以,否则全部加上x权限。以test1.txt为例:

[root@centos7 app]#ll
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r-xr--. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt
[root@centos7 app]#chmod -R o+X /app
[root@centos7 app]#ll
total 16
drwxr--r-x. 2 root root 4096 Jun  4 13:06 dir
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir2
drwxr--r-x. 2 root root 4096 Jun  4 13:13 dir3
-rw-r-xr-x. 1 root root    0 Jun  4 11:14 test1.txt
-rw-r--r--. 1 root root   21 Jun  4 11:55 test.txt

3)权限位的八进制数表示

Linux 文件系统上的权限

Linux文件系统上的特殊权限
除了上文提到的权限模型r,w,x;在Linux文件系统上对应ugo位又有三个特殊权限:SUID、SGID、Sticky
一、安全上下文
 前提:进程有属主和属组;文件有属主和属组
(1) 任何一个可执行程序文件能不能启动为进程,取决发起者对程序文件是否拥有执行权限
(2) 启动为进程之后,其进程的属主为发起者,进程的属组为发起者所属的组
(3) 进程访问文件时的权限,取决于进程的发起者
    (a) 进程的发起者,同文件的属主:则应用文件属主权限
    (b) 进程的发起者,属于文件属组:则应用文件属组权限
    (c) 应用文件“其它”权限
例如:
1.用户:ffu
    可执行文件:-rwxr-xr-x. 1 root root 115K Nov  6  2016 /bin/ls
    ffu能不能启动为进程,首先他不是可执行文件的属主,其次不属于root组,所以只能以other身份运行,other用户是具有执行权限;执行后,ls进程的属主为ffu(发起者)
2.进程:属主ffu 属组ffu
    对象:-rw-r–r–. 1 root root 21 Jun  4 11:55 /app/test.txt
    进程访问文本test.txt的时候,首先对比进程和文本文件的属主是否一样,为否,则看属组;注意不是对比两个属组,而是看进程的运行身份ffu是否属于文本文件的属组root,为否,以other身份访问文件。
    以上都是严格按照顺序,一旦匹配到不会向下匹配

二、SUID

占据的是属主的执行权限位;SUID只对二进制可执行程序有效,设置在目录上无意义

与上面所提到的不一样,对于设定了SUID的可执行文件启动为进程之后,其进程的属主为原程序文件的属主,而不再是进程的发起者

权限设定:

chmod u+s FILE…

chmod u-s FILE…

权限位的八进制数表示为4

举例:

设定test.txt只对root有读权限,则ffu作为文件的other用户,不能通过cat来访问文件

[ffu@centos7 app]$ll test.txt
-r--------. 1 root root 21 Jun  4 11:55 test.txt
[ffu@centos7 app]$/app/cat test.txt
/app/cat: test.txt: Permission denied

为/app/cat文件加上SUID后,ffu可以访问文件test.txt了,因为启动为进程之后,进程的属主是cat文件的属主即root

[root@centos7 app]#chmod u+s /app/cat
[root@centos7 app]#ll /app/cat
-rwsr-xr-x. 1 root root 54080 Jun  4 14:58 /app/cat
[ffu@centos7 app]$/app/cat test.txt
This is a test file.

三、SGID

占据的是属组的执行权限位;权限位的八进制数表示为2

1.对二进制可执行文件

对于设定了SGID的可执行文件启动为进程之后,其进程的属主虽然仍是进程的发起者,但是继承了原程序文件的属组的权限

权限设定:

chmod g+s FILE…

chmod g- s FILE…

举例:

设定设定test.txt只对属组root有读权限,则ffu作为文件的other用户,不能通过cat来访问文件;

为/app/cat文件加上SGID后,ffu可以访问文件test.txt了,因为启动为进程之后,ffu继承了原程序文件属组的权限

[root@centos7 ~]#chmod g+s /app/cat
[root@centos7 ~]#ll /app/cat
-rwxr-sr-x. 1 root root 54080 Jun  4 14:58 /app/cat
[ffu@centos7 ~]$/app/cat /app/test.txt
This is a test file.

2.对目录

默认情况下,用户创建文件时,其属组为此用户所属的主组。一旦某目录被设定了SGID,则对此目录有写权限的用户在此目录中创建的文件所属的组为此目录的属组;通常用于创建一个协作目录

权限设定:

chmod g+s DIR…

chmod g-s DIR…

举例:

ffu在/app/dir目录下创建文件,属主属组均为ffu

[ffu@centos7 app]$ll -d dir 
drwxrwxr-x. 2 ffu ffu 4096 Jun  4 16:56 dir
[ffu@centos7 app]$touch dir/f{1,2}
[ffu@centos7 app]$ll dir
total 0
-rw-rw-r--. 1 ffu ffu 0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu ffu 0 Jun  4 17:01 f2

把user1加到ffu组中,user1在/app/dir目录下创建文件,属主属组均为user1

[root@centos7 ~]#groupmems -g ffu -a user1
[user1@centos7 dir]$touch f{3,4}
[user1@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f2
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f3
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f4

为/app/dir目录加上SGID后,user1在/app/dir目录下创建文件f5,属组均为ffu即目录dir的属组

[root@centos7 ~]#chmod g+s /app/dir
[root@centos7 ~]#ll /app/dir -d    
drwxrwsr-x. 2 ffu ffu 4096 Jun  4 17:04 /app/dir
[user1@centos7 dir]$touch f5
[user1@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f1
-rw-rw-r--. 1 ffu   ffu   0 Jun  4 17:01 f2
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f3
-rw-rw-r--. 1 user1 user1 0 Jun  4 17:04 f4
-rw-rw-r--. 1 user1 ffu   0 Jun  4 17:08 f5

四、Sticky

占据的是other的执行权限位;sticky 设置在文件上无意义

权限设定:

chmod o+t DIR…

chmod o-t DIR…

权限位的八进制数表示为1

举例:

ffu和user1对/app/dir目录均有写权限,他们对目录下所以文件都具有删除权限

[user1@centos7 dir]$rm -v f2
removed ‘f2’
[ffu@centos7 app]$rm -v dir/f4
removed ‘dir/f4’
[root@centos7 ~]#rm -v /app/dir/f5
removed ‘/app/dir/f5’

实际上,有写权限的目录通常用户可以删除该目录中的任何文件,无论该文件的权限或拥有权

在目录设置Sticky位,只有文件的所有者、目录的属主、root可以删除该文件。仍然引用上例,对/app/dir加上Sticky位:

[root@centos7 ~]#chmod o+t /app/dir
[root@centos7 ~]#ll /app/dir -d
drwxrwsr-t. 2 ffu ffu 4096 Jun  4 17:20 /app/dir

user1和user2均为属组ffu的成员,分别创建文件f2\f3

[ffu@centos7 dir]$ll
total 0
-rw-rw-r--. 1 ffu   ffu 0 Jun  4 17:53 f1
-rw-rw-r--. 1 user1 ffu 0 Jun  4 17:53 f2
-rw-rw-r--. 1 user2 ffu 0 Jun  4 17:53 f3

user1不能删除f1,f3

[user1@centos7 dir]$rm -v f1
rm: cannot remove ‘f1’: Operation not permitted
[user1@centos7 dir]$rm -v f3
rm: cannot remove ‘f3’: Operation not permitted

user2不能删除f1,f2

[user2@centos7 dir]$rm -v f1
rm: cannot remove ‘f1’: Operation not permitted
[user2@centos7 dir]$rm -v f2
rm: cannot remove ‘f2’: Operation not permitted

由于ffu是目录属主,可以删除f2,f3

[ffu@centos7 dir]$rm -v f2
removed ‘f2’
[ffu@centos7 dir]$rm -v f3
removed ‘f3’

最后, 上文提到SUID、SGID、Sticky权限位的八进制数分别为4,2,1;三个特殊位可以组成一组权限:suidsgidsticky 允许我们通过八进制数字与普通权限一起对目录及文件进行修改

比如:
chmod 4755 /app/dir   修改SUID
chmod 5755 /app/dir   修改SUID、Sticky


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持 码农网

查看所有标签

猜你喜欢:

本站部分资源来源于网络,本站转载出于传递更多信息之目的,版权归原作者或者来源机构所有,如转载稿涉及版权问题,请联系我们

Math Adventures with Python

Math Adventures with Python

Peter Farrell / No Starch Press / 2018-11-13 / GBP 24.99

Learn math by getting creative with code! Use the Python programming language to transform learning high school-level math topics like algebra, geometry, trigonometry, and calculus! In Math Adventu......一起来看看 《Math Adventures with Python》 这本书的介绍吧!

在线进制转换器
在线进制转换器

各进制数互转换器

XML 在线格式化
XML 在线格式化

在线 XML 格式化压缩工具

RGB HSV 转换
RGB HSV 转换

RGB HSV 互转工具