文件权限及管理

文件权限介绍:

文件属性:

  • 核实发起进程的用户即进程的属主与文件的属主是否一致,如一致则应用属主权限
  • 如属主不一致,则检查进程的属主是否与文件的属组一致,如一致则应用该属组权限
  • 如都不一致,则应用other权限

文件的权限主要针对三类对象进行定义

owner: 属主, u
group: 属组, g
other: 其他, o

每个文件针对每类访问者都定义了三种权限

r: Readable  读
w: Writable   写
x: eXcutable  执行
X:只给目录x权限,不给文件x权限

下面开始介绍文件的权限类型,如下命令显示:

[root@centos7data]#ls -l f1
-rw-r--r-- 1 root root 0 Oct 27 22:33 f1

我们来解释一下上面的-rw-r--r--都代表什么意思?

rw-:代表所有者有读写权限   
r--:代表所属组只有读权限
r--:代表other(其他用户)有读权限 

每个文件或目录在创建之时都拥有自身的权限分配,如上,现在开始讲解-rw-r--r--代表什么权限。

权限模式分两类:

mode:即ll所显示的rwxrwxrwx之类的,其中:
    r:readable,可读权限
        于文件而言:可读取文件的数据
        于目录而言:可使用ls命令读取其下的所有文件列表,不能cd到目录里
    w:writable,可写权限
        于文件而言:可修改文件的数据,也可以向文件中追加内容
        于目录而言:可修改此目录里下的文件列表,即增删文件,能不能删除文件取决于文件所在的目录是否有写权限,跟文件权限无关。
    x:excutable,可执行权限
        于文件而言:可将此文件运行为进程,对于执行权限而言,就算root用户没有执行权限都不能执行文件。
        于目录而言:可cd至此目录中,且可使用ll来获取所有文件的详细属性信息。

在Linux中,模式转换数字关系:

--- 000 0   可以看到权限位上要么为空要么有权限,因此用0和1来表示,0即空,1即有权限位,这就是二进制表示法
r-- 100 4   三位二进制对应一个八进制,即2^3,四位二进制对应一个十六进制,即2^4
rw- 110 6   
r-x 101 5   二进制换算八进制公式如下:
rwx 111 7   从右到左开始相加,1*2^0+1*2^1+1*2^2=7 第一位是1乘以2的0次方,每往左一位+1次方
-w- 010 2   必须要记住这几样权限转换八进制的结果,做到所见即所知
-wx 011 3
--x 001 1
小结:常见的是r w x三个组合,也就是6 4 1,记住三个,出现的所有者,所属组,other权限都可以将其三个数字进行累加。

权限管理常用命令以及用法

根据上面的知识点我们知道有几类用户:u属主、g属组、 o其它、 a所有者
我们使用man chmod命令来获取chmod命令的用法和说明,如下所示:

模式表示法:
    (1). 赋权表示法:直接操作一类用户的所有权限位
        如 u=rwx  类似这样
        如 g=wx
        如 o=   空
        如 a=
[root@centos7data]#ll  
total 0 
-rwxrw-r-- 1 root root 0 Oct 27 22:33 f1     我们可以查看到f1的所有者有读写执行权限,所属组有读写权限
[root@centos7data]#chmod u=rw,g=r  f1   下来我们赋予所有者读写权限,所属组赋予读权限
[root@centos7data]#ll
total 0
-rw-r--r-- 1 root root 0 Oct 27 22:33 f1       显示结果
    (2). 授权表示法:直接操作一类用户的单一权限位
        u+r,u-w  类似这样
        g+r,g-w
        o+r,o-x
        a+,a-
[root@centos7data]#ll 
total 0
-rw-r--r-- 1 root root 0 Oct 27 22:33 f1  可以看到一开始f1所有者只有读写权限,所属组只有读权限
[root@centos7data]#chmod u+x f1       给所有者加执行权限
[root@centos7data]#ll
total 0
-rwxr--r-- 1 root root 0 Oct 27 22:33 f1  
[root@centos7data]#chmod g+w f1     给所属组加写权限
[root@centos7data]#ll
total 0
-rwxrw-r-- 1 root root 0 Oct 27 22:33 f1

八进制表示法:

[root@centos7data]#ll
total 0
-rw-r--r-- 1 root root 0 Oct 27 23:35 f2
[root@centos7data]#chmod 666 f2
[root@centos7data]#ll
total 0
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f2

引用别的文件权限:

[root@centos7data]#chmod 666 f2
[root@centos7data]#ll
total 0
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f2
[root@centos7data]#touch f3
[root@centos7data]#chmod --reference=f2 f3
[root@centos7data]#ll
total 0
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f2
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3

chmod +X file 用法:

-R, --recursive 递归修改目录及子目录下面的所有文件的权限;慎重使用

X:只给目录x权限,不给文件x权限

含义:就是只给目录加执行权限而不给目录下的文件加执行权限,如果目录下的文件有执行权限,会给文件的全部属性加权限

[root@centos7bak]#ll  bak里边的文件是没有执行权限
total 0
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f2
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3
[root@centos7data]#chmod -R +X bak  给bak目录加权限
[root@centos7data]#ll
total 0
d-wx--x--x 2 root root 26 Oct 27 23:41 bak
[root@centos7data]#cd bak 
[root@centos7bak]#ll    cd到bak目录中,目录下的文件不会有权限
total 0
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f2
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3
[root@centos7bak]#chmod u+x f2  给bak目录的f2加执行权限
[root@centos7data]#chmod -R +X bak  给bak加执行权限
[root@centos7bak]#ll
total 0
-rwxrwxrwx 1 root root 0 Oct 27 23:35 f2  目录下的文件所有属性都会加执行权限
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3

 chown:可以修改当前文件或目录的所有者、所属组权限

[root@centos7bak]#chown wang. f2   书写格式:后面加.会默认将所有者和所属组的文件信息都改为wang
[root@centos7bak]#ll
total 0
-rwxrwxrwx 1 wang wang 0 Oct 27 23:35 f2
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3
[root@centos7bak]#chown .liu f2    书写格式:用户前面加.只修改文件的所属组,将所属组修改为liu
[root@centos7bak]#ll
total 0
-rwxrwxrwx 1 wang liu 0 Oct 27 23:35 f2
-rw-rw-rw- 1 root root 0 Oct 27 23:35 f3
[root@centos7bak]#chown wang f3    书写格式:只写一个用户名,相当于只是修改了文件所有者的信息
[root@centos7bak]#ll
total 0
-rwxrwxrwx 1 wang liu 0 Oct 27 23:35 f2
-rw-rw-rw- 1 wang root 0 Oct 27 23:35 f3

chgrp:修改所属组的文件信息

[root@centos7bak]#ll   当前f2的所属组是liu
total 0
-rwxrwxrwx 1 wang liu  0 Oct 27 23:35 f2
-rw-rw-rw- 1 wang root 0 Oct 27 23:35 f3
[root@centos7bak]#chgrp wang f2    修改f2文件的所属组为wang
[root@centos7bak]#ll
total 0
-rwxrwxrwx 1 wang wang 0 Oct 27 23:35 f2
-rw-rw-rw- 1 wang root 0 Oct 27 23:35 f3

思考:

  如果目录有写权限,是否能删除目录里的文件,文件没有写权限,是否能在文件中写入呢?如果目录没有执行权限,是否能cd切换到目录里呢?

答:目录有写权限可以删文件,文件没有写权限不能写入内容,目录没有执行权限,不能cd到目录中。

[wang@centos7data]$ll
total 0
drwxrwxrwx 2 root root 26 Oct 27 23:41 bak  当前bak目录有读写执行权限
[wang@centos7bak]$ll 
total 0
-r-xr-xr-x 1 wang wang 0 Oct 27 23:35 f2 当前f2没有读权限,只有执行权限
[wang@centos7bak]$echo biubiu >> f2  向文件中不能写入内容
bash: f2: Permission denied
[wang@centos7bak]$rm -rf f2  可以删除f2文件

[wang@centos7bak]$ll total 0 -r-xr-xr-x 1 wang root 0 Oct 27 23:35 f3 [root@centos7data]#chmod 666 bak [root@centos7data]#ll total 0 drw-rw-rw- 2 root root 26 Oct 27 23:41 bak [root@centos7data]#su wang [wang@centos7data]$ll total 0 drw-rw-rw- 2 root root 26 Oct 27 23:41 当前bak属于other,只有读写权限,没有执行权限 [wang@centos7data]$cd [wang@centos7~]$cd /data/bak bash: cd: /data/bak: Permission denied 没有权限cd到目录下的文件

  

原文地址:https://www.cnblogs.com/struggle-1216/p/11749949.html