Linux指令——chmod

Chmod是Linux的权限指令,全称为Change Mode,即改变模式,而在Linux中即为改变权限的模式。下面我们一步一步了解这个指令的使用

权限在文件系统中的表现形式

[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
total 72
-rw-r--r--   1 root root     0 Jun 21 09:22 a.txt
lrwxrwxrwx.  1 root root     7 Oct 15  2017 bin -> usr/bin
dr-xr-xr-x.  5 root root  4096 Oct 15  2017 boot
drwxr-xr-x  19 root root  2980 Jun 20 19:17 dev
drwxr-xr-x. 80 root root  4096 Jun 20 21:21 etc
drwxr-xr-x   2 git  git   4096 Jun 20 21:28 gitrepo
drwxr-xr-x.  3 root root  4096 Jun 20 21:21 home
lrwxrwxrwx.  1 root root     7 Oct 15  2017 lib -> usr/lib
lrwxrwxrwx.  1 root root     9 Oct 15  2017 lib64 -> usr/lib64
drwx------.  2 root root 16384 Oct 15  2017 lost+found
drwxr-xr-x.  2 root root  4096 Nov  5  2016 media
drwxr-xr-x.  2 root root  4096 Nov  5  2016 mnt
drwxr-xr-x.  2 root root  4096 Nov  5  2016 opt
dr-xr-xr-x  73 root root     0 Jun 20 19:17 proc
drwxr-xr-x   2 root root  4096 Jun 20 21:20 resource
dr-xr-x---.  5 root root  4096 Jun 20 20:10 root
drwxr-xr-x  20 root root   560 Jun 20 20:19 run
lrwxrwxrwx.  1 root root     8 Oct 15  2017 sbin -> usr/sbin
drwxr-xr-x   4 root root  4096 Jun 20 21:20 software
drwxr-xr-x.  2 root root  4096 Nov  5  2016 srv
dr-xr-xr-x  13 root root     0 Jun 21 03:17 sys
drwxrwxrwt. 11 root root  4096 Jun 21 03:17 tmp
drwxr-xr-x. 13 root root  4096 Oct 15  2017 usr
drwxr-xr-x. 19 root root  4096 Oct 15  2017 var

上面是博主阿里云上centos7的根目录下的文件系统,可以看出其表现形式由九列组成,我们讲Chmod指令时只关注第一,三,四列。
这里写图片描述

编号 解释
1 文件类型
2 所属用户操作权限
3 所属用户组操作权限
4 其他用户的操作权限
5 文件所属用户(user)
6 文件所属用户组(group)
文件类型
符号 解释
- 文件
l 链接,即link,类似于windows中的快捷方式
d 文件夹,即dir,就是directory的缩写
权限类别
符号 解释
r read,读取权限,数字代号为“4”
w write,写入权限,数字代号为“2”
x execute,执行权限,数字代号为“1”
- 没有权限,数字代号为“0”
以上3个文件信息的描述
解释
1 a.txt,它是一个文件,它属于的用户为root,它属于的用户组为root,所属的用户对它有读写权限,所属的用户组和其他用户对它有读的权限
2 bin,它是一个链接,它指向了usr文件夹下面的bin文件夹,它属于的用户为root,它属于的用户组为root,所有的用户对它都有读写执行的权限
3 boot,它是一个文件夹,它所属的用户为root,它所属的用户组为root,所有用户对它有读和执行的权限

我们对文件权限已经有了基本的了解,下面开始使用Chmod指令队文件权限进行操作

1.获取chmod的版本

[root@iZ2ze9twtxjrbirmldp9owZ home]# chmod --version
chmod (GNU coreutils) 8.22
Copyright (C) 2013 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

2.使用chown(change owner)改变文件的所属用户和用户组

[root@iZ2ze9twtxjrbirmldp9owZ /]# chown git:git a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rw-r--r--   1 git  git      0 Jun 21 09:22 a.txt

以上指令产生的效果就是:修改了a.txt文件所属的用户为git,所属的用户组为git。(这里的git和git只是用户组和用户的名称恰巧一样而已)

3.增加和去除权限

[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rw-r--r--   1 git  git      0 Jun 21 09:22 a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# chmod u+x,g+w a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rwxrw-r--   1 git  git      0 Jun 21 09:22 a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# chmod o-r a.txt          
[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rwxrw----   1 git  git      0 Jun 21 09:22 a.txt
代号 解释
u User,即文件或目录的拥有者
g Group,即文件或目录的所属群组
o Other,除了文件或目录拥有者或所属群组之外,其他用户皆属于这个范围
a All,即全部的用户,包含拥有者,所属群组以及其他用户

以上指令产生的效果就是:给user添加了执行权限,给group添加了写入的权限,给other去除了读取的权限。

4.赋值形式设置权限

[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rwxrw----   1 git  git      0 Jun 21 09:22 a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# chmod u=w,g=r,o=x a.txt 
[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
--w-r----x   1 git  git      0 Jun 21 09:22 a.txt

你会发现这种形式对权限进行了重新赋值,而不是添加,没有在=后面的权限则不会被赋予制定文件

5.数字形式赋值权限

[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
--w-r----x   1 git  git      0 Jun 21 09:22 a.txt
[root@iZ2ze9twtxjrbirmldp9owZ /]# chmod 777 a.txt 
[root@iZ2ze9twtxjrbirmldp9owZ /]# ll
-rwxrwxrwx   1 git  git      0 Jun 21 09:22 a.txt
权限 数字
r 4
w 2
x 1
- 0

对应所有可能数字

数字 解释
7 4+2+1,即拥有读写执行的权限
6 4+2,即拥有读写的权限
5 5+1,即拥有读执行的权限
4 读权限
3 2+1,即拥有写和执行的权限
2 写权限
1 执行权限
0 没有任何权限

以上指令产生的效果就是:给user设置了7,给group设置了7,给other设置了7,即给所有用户都赋予了读写执行的权限。

6.递归设置权限

[root@iZ2ze9twtxjrbirmldp9owZ /]# chmod -R 777 boot

-R:递归处理,将指令目录下的所有文件及子目录一并处理;

总结

开发人员掌握这些就够了。

猜你喜欢

转载自blog.csdn.net/weixin_37490221/article/details/80757340
今日推荐