权限管理命令--chmod
说明:示例中以"#","~"开头的表示输入
权限管理命令--chmod
- 功能描述:改变文件或者目录权限
- 执行权限:所有用户
- 所在路径:/bin/chmod
示例1
# ls -l chmod.txt
-rw-r--r-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod u+x chmod.txt
# ls -l chmod.txt
-rwxr--r-- 1 root root 10 9月 16 20:52 chmod.txt
如上,chmod u+x chmod.txt 表示给所有者(u)添加执行(+x)权限.
示例2
# ls -l chmod.txt
-rwxr--r-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod u-x chmod.txt
# ls -l chmod.txt
-rw-r--r-- 1 root root 10 9月 16 20:52 chmod.txt
如上,chmod u-x chmod.txt 表示给所有者(u)减去执行(-x)权限.
示例3
# ls -l chmod.txt
-rw-r--r-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod g+x chmod.txt
# ls -l chmod.txt
-rw-r-xr-- 1 root root 10 9月 16 20:52 chmod.txt
如上,chmod g+x chmod.txt 表示给所属组(g)添加执行(+x)权限.
示例4
# ls -l chmod.txt
-rw-r-xr-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod o+x chmod.txt
# ls -l chmod.txt
-rw-r-xr-x 1 root root 10 9月 16 20:52 chmod.txt
如上,chmod o+x chmod.txt 表示给其他人(o)添加执行(+x)权限.
示例5
# ls -l chmod.txt
-rw-r-xr-x 1 root root 10 9月 16 20:52 chmod.txt
# chmod u+x,o-x chmod.txt
# ls -l chmod.txt
-rwxr-xr-- 1 root root 10 9月 16 20:52 chmod.txt
如上,可同时更改ugo的权限.
示例6
# ls -l chmod.txt
-rwxr-xr-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod g=rwx chmod.txt
# ls -l chmod.txt
-rwxrwxr-- 1 root root 10 9月 16 20:52 chmod.txt
如上,采用g=rwx的方式,更改所属组的权限
权限的数字表示
- r -----4
- w -----2
x -----1
所以:
rwx = 7
rw- = 6
r-x = 5
......
示例7
# ls -l chmod.txt
-rwxrwxr-- 1 root root 10 9月 16 20:52 chmod.txt
# chmod 764 chmod.txt
# ls -l chmod.txt
-rwxrw-r-- 1 root root 10 9月 16 20:52 chmod.txt
如上,采用数字更改权限的方式,rwxrw-r--对应764.
示例8
# ll testdir/
total 8
drwxr-xr-x 2 root root 4096 9月 16 21:30 ./
drwxr-xr-x 3 root root 4096 9月 16 21:30 ../
-rw-r--r-- 1 root root 0 9月 16 21:30 testfile
# chmod -R 777 testdir/
# ll testdir/
total 8
drwxrwxrwx 2 root root 4096 9月 16 21:30 ./
drwxr-xr-x 3 root root 4096 9月 16 21:30 ../
-rwxrwxrwx 1 root root 0 9月 16 21:30 testfile*
如上,chmod -R 777 testdir,-R递归修改testdir及其目录下文件的权限.
详解rwx权限
对于文件的含义
- r:可以查看文件内容
- w:可以修改文件内容
- x:可以执行该文件
对目录的含义
- r:可以列出目录中的内容
- w:可以在目录中创建、删除文件
- x:可以进入目录
注意:对于目录而言,有r和x权限一般同时存在