1. chmod命令介绍
chmod命令来自于英文词组”change mode“的缩写,其功能是用于改变文件或目录权限的命令。
默认只有文件的所有者和管理员root可以设置文件权限,普通用户只能管理自己文件的权限属性。
设置权限时可以使用数字法,亦可使用字母表达式,对于目录文件建议加入-R参数进行递归操作,意味着不仅对于目录本身,也对目录内的子文件/目录都进行新权限的设定。
2. 语法格式及常用选项
常用参数:
我们可以使用 --help查看所有参数的使用。
[root@doc ~]# chmod --help
从chmod的帮助文档中,我们发现 chmod的常用参数为:
3. 参考案例
修改权限的方式有两种:
第一种: 使用字符的设定进行修改
语法:chmod [对谁操作] [操作符] [赋于什么权限] 文件名
u----> 用户user,表示文件或目录的所有者
g---->用户组group,表示文件或目录所属的用户组
o---->其它用户others
a---->所有用户all
第二种: 使用八进制数字表示权限
数字型文件权限:
r=4 可读权限
w=2 可写权限
x=1 执行权限
没有任何权限为:0
我们一起看一下常见的权限操作:
3.1 对全部用户增加写的权限
读的权限为r,对所有用户增加读权限,意味着: 所有者,所有组,其他用户都设置为读的权限。
我们使用 字符设定来进行修改:
[root@mufenggrow test]# touch a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod a+w a.txt
[root@mufenggrow test]# ll a.txt
-rw-rw-rw-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
可以看到,原本所有组和其他用户只有读的权限,使用chmod后,a.txt所有用户都具有了写权限。
3.2 所有用户减去读的权限
根据3.1的写法,我们可以猜测,减去读的权限为 a-r, 接下来我们测试下:
[root@mufenggrow test]# ll a.txt
-rw-rw-rw-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod a-r a.txt
[root@mufenggrow test]# ll a.txt
--w--w--w-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
可以看到,现在a.txt文件之后写权限了。
3.3 给文件的所有者和所有组加上读写权限
读写权限为 rw
所有者和所有组为: gu
意味着我们要使用 chmod gu+rw,接下来我们测试下:
[root@mufenggrow test]# ll a.txt
--w--w--w-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod ug+rw a.txt
[root@mufenggrow test]# ll a.txt
-rw-rw--w-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
3.4 设置所有用户为读写执行的权限
读写执行为: rwx
所有用户可以用: a=rwx
[root@mufenggrow test]# ll a.txt
-rw-rw--w-. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod a=rwx a.txt
[root@mufenggrow test]# ll a.txt
-rwxrwxrwx. 1 root root 0 1月 21 16:17 a.txt
3.5 文件拥有着为rwx,所属组为rw,其它为r
我们来分析下这个题目:
所有组 为rwx ,意味着 u=rwx
所有组为 g=rw
其他为r ,以为着 o=r
所以我们应该执行:
[root@mufenggrow test]# chmod u=rwx,g=rw,o=r a.txt
[root@mufenggrow test]# ll a.txt
-rwxrw-r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
这里出现多个的时候,一定要加逗号分割开。
3.6 去掉所有者的rw权限
去掉所有这点rw权限,这里我们使用的操作符是 -
所有这是U ,最终答案为: U-rw
root@mufenggrow test]# ll a.txt
-rwxrw-r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod u-rw a.txt
[root@mufenggrow test]# ll a.txt
---xrw-r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
从结果可以看到,a.txt文件的所有者的读写权限都被去掉了。
3.7 将test目录下所有文件都变成读写执行的权限
此时,所有文件都变为读写执行,即: a=rwx
涉及到所有文件,我们用到参数 -R
[root@mufenggrow test]# touch {1..3}.txt
[root@mufenggrow test]# ll
总用量 0
-rw-r--r--. 1 root root 0 1月 21 16:31 1.txt
-rw-r--r--. 1 root root 0 1月 21 16:31 2.txt
-rw-r--r--. 1 root root 0 1月 21 16:31 3.txt
---xrw-r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]# chmod -R a=rwx ./*
[root@mufenggrow test]# ll
总用量 0
-rwxrwxrwx. 1 root root 0 1月 21 16:31 1.txt
-rwxrwxrwx. 1 root root 0 1月 21 16:31 2.txt
-rwxrwxrwx. 1 root root 0 1月 21 16:31 3.txt
-rwxrwxrwx. 1 root root 0 1月 21 16:17 a.txt
使用八进制的形式设置权限
3.8 设置所有者为读写,其他为读
根据八进制,可以得知,只需要设置644即可
[root@mufenggrow test]# chmod 644 a.txt
[root@mufenggrow test]# ll a.txt
-rw-r--r--. 1 root root 0 1月 21 16:17 a.txt
[root@mufenggrow test]#
可以发现用数字相对来说更简单一些,我们可以直接进行换算即可:
r=4 可读权限
w=2 可写权限
x=1 执行权限
没有任何权限为:0
4.总结
chmod在日常工作中用的比较多,我们多数情况下用到的都是八进制的形式。