chmod 命令的功能是用于改变文件或目录权限的命令。默认只有文件的所有者和管理员可以设置文件权限,普通用户只能管理自己文件的权限属性。 设置权限时可以使用数字法,亦可使用字母表达式,对于目录文件建议加入 -R 参数进行递归操作,意味着不仅对于目录本身,也对目录内的子文件/目录都进行新权限的设定。
目录
一、语法格式
chmod 参数 文件
二、常用参数
-c | 若该文件权限确实已经更改,才显示其更改动作 |
-v | 显示权限变更的详细资料 |
-R | 对目前目录下的所有文件与子目录进行相同的权限变更(即以递回的方式逐个变更) |
三、命令实战
3.1 设定某个文件的权限为775
[root@linux1 ~]# ls -l aliases.txt
-rw-r--r--. 1 root root 12288 3月 28 10:40 aliases.txt
[root@linux1 ~]# chmod 775 aliases.txt
[root@linux1 ~]# ls -l aliases.txt
-rwxrwxr-x. 1 root root 12288 3月 28 10:40 aliases.txt
3.2 设定某个文件让任何人都不可以/可以读取
[root@linux1 ~]# chmod a-r aliases.txt
[root@linux1 ~]# ls -l aliases.txt
--wx-wx--x. 1 root root 12288 3月 28 10:40 aliases.txt
[root@linux1 ~]# chmod a+r aliases.txt
[root@linux1 ~]# ls -l aliases.txt
-rwxrwxr-x. 1 root root 12288 3月 28 10:40 aliases.txt
3.3 设定某个目录及其内子文件任何人都可以写入
[root@linux1 ~]# ls -l date
总用量 16
-rw-r--r--. 1 root root 12288 3月 28 10:40 aliases.txt
-rw-r--r--. 1 root root 1108 3月 28 11:01 chrony.txt
[root@linux1 ~]# chmod -R a+w date
[root@linux1 ~]# ls -l date
总用量 16
-rw-rw-rw-. 1 root root 12288 3月 28 10:40 aliases.txt
-rw-rw-rw-. 1 root root 1108 3月 28 11:01 chrony.txt
3.4 设定某个二进制命令文件上新增SUID特殊权限位
[root@linux1 ~]# chmod u+s aliases.txt
[root@linux1 ~]# ls -l aliases.txt
-rwsrwxr-x. 1 root root 12288 3月 28 10:40 aliases.txt
3.5 显示权限变更的详细资料
[root@linux1 ~]# chmod u-s -v aliases.txt
mode of "aliases.txt" changed from 4775 (rwsrwxr-x) to 0775 (rwxrwxr-x)