chmod命令用来改变文件或者目录的权限,只有文件的属主和超级用户才能够执行这个命令
格式:
chmod [option] [mode] [file]
>常用参数选项 -R : 递归修改目录以及子目录下面的所有文件权限
>模式有两种格式,一种采用字母方式的表达式,另外一种是数字
1,首先需要了解文件的权限和属主和属组。
1ghostwu@dev:~/linux/chown$ ls -l 2total 4 3-rw-rw-r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
-rw-rw-r-- 这个就是文件的权限, 除去第一位, 一共有9位组成
第一位表示文件类型,- 表示这个是一个常规文件
后面9位,每3位一组. 第一个3位rw-表示属主权限, 第二个3位rw-表示属组权限,第三个3位r--表示其他用户权限,
后面有2个ghostwu, 第一个ghostwu, 表示属主, 也就是这个test.txt文件的拥有者是ghostwu
第二个ghostwu,表示属组,也就是这个test.txt文件可以被ghostwu这个组的用户 rw-( 可读,可写)
2,修改文件权限
>增加权限( + )
1ghostwu@dev:~/linux/chown$ chmod a+x test.txt 2ghostwu@dev:~/linux/chown$ ls 3test.txt 4ghostwu@dev:~/linux/chown$ ls -l 5total 4 6-rwxrwxr-x 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
a等价于 用户(u)+组(g)+其他组( o )。 a+x 就是给用户,组,其他组都加上x(可执行)权限
>去掉权限( - )
1ghostwu@dev:~/linux/chown$ chmod a-x test.txt 2ghostwu@dev:~/linux/chown$ ls -l 3total 4 4-rw-rw-r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
>设置权限( = )
1ghostwu@dev:~/linux/chown$ chmod a=r-- test.txt 2ghostwu@dev:~/linux/chown$ ls -l 3total 4 4-r--r--r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
>给属主加上w( 可写 ), x( 可执行 ) 权限
1ghostwu@dev:~/linux/chown$ chmod u+wx test.txt 2ghostwu@dev:~/linux/chown$ ls -l 3total 4 4-rwxr--r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
>给组加上wx权限
1ghostwu@dev:~/linux/chown$ ls -l 2total 4 3-rwxr--r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 4ghostwu@dev:~/linux/chown$ chmod g+wx test.txt 5ghostwu@dev:~/linux/chown$ ls -l 6total 4 7-rwxrwxr-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
>给其他组加上wx权限
1ghostwu@dev:~/linux/chown$ ls -l 2total 4 3-rwxrwxr-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 4ghostwu@dev:~/linux/chown$ chmod o+wx test.txt 5ghostwu@dev:~/linux/chown$ ls -l 6total 4 7-rwxrwxrwx 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
>r( 4 ), w( 2 ), x( 1 )
1ghostwu@dev:~/linux/chown$ ls -l 2total 4 3-rwxrwxrwx 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 4ghostwu@dev:~/linux/chown$ chmod 444 test.txt 5ghostwu@dev:~/linux/chown$ ls -l 6total 4 7-r--r--r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 8 9ghostwu@dev:~/linux/chown$ ls -l 10total 4 11-r--r--r-- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 12ghostwu@dev:~/linux/chown$ chmod 755 test.txt 13ghostwu@dev:~/linux/chown$ ls -l 14total 4 15-rwxr-xr-x 1 ghostwu ghostwu 20 5月 9 22:55 test.txt
权限详解:
一、普通文件
可读r: 读取/阅读文件内容的权限
1ghostwu@dev:~/linux/chown$ ls -l 2total 4 3-rwxr-xr-x 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 4ghostwu@dev:~/linux/chown$ chmod 000 test.txt 5ghostwu@dev:~/linux/chown$ ls -l 6total 4 7---------- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 8ghostwu@dev:~/linux/chown$ cat test.txt 9cat: test.txt: Permission denied 10ghostwu@dev:~/linux/chown$ chmod 400 test.txt 11ghostwu@dev:~/linux/chown$ ls -l 12total 4 13-r-------- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 14ghostwu@dev:~/linux/chown$ cat test.txt 15this is a test file
可写(w):具有新增,修改文件内容的权限
1ghostwu@dev:~/linux/chown$ ls -l test.txt 2-r-------- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 3ghostwu@dev:~/linux/chown$ echo 'aaa' > test.txt 4bash: test.txt: Permission denied 5ghostwu@dev:~/linux/chown$ chmod u+w test.txt 6ghostwu@dev:~/linux/chown$ ls -l test.txt 7-rw------- 1 ghostwu ghostwu 20 5月 9 22:55 test.txt 8ghostwu@dev:~/linux/chown$ echo 'ghostwu' >> 'test.txt' 9ghostwu@dev:~/linux/chown$ cat test.txt 10this is a test file 11ghostwu
可执行( x )
1,文件本身要用x权限
1ghostwu@dev:~/linux/chown$ ls -l test.txt 2-rw------- 1 ghostwu ghostwu 28 5月 9 23:20 test.txt 3ghostwu@dev:~/linux/chown$ ./test.txt 4bash: ./test.txt: Permission denied 5ghostwu@dev:~/linux/chown$ chmod u+x test.txt 6ghostwu@dev:~/linux/chown$ ls -l test.txt 7-rwx------ 1 ghostwu ghostwu 28 5月 9 23:20 test.txt 8ghostwu@dev:~/linux/chown$ ./test.txt 9./test.txt: line 1: this: command not found 10./test.txt: line 2: ghostwu: command not found 11 12ghostwu@dev:~/linux/chown$ echo 'ls /' > test.sh 13ghostwu@dev:~/linux/chown$ ls -l 14total 8 15-rw-rw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 16-rwx------ 1 ghostwu ghostwu 28 5月 9 23:20 test.txt 17ghostwu@dev:~/linux/chown$ ./test.sh 18bash: ./test.sh: Permission denied 19ghostwu@dev:~/linux/chown$ chmod u+x test.sh 20ghostwu@dev:~/linux/chown$ ls -l test.sh 21-rwxrw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 22ghostwu@dev:~/linux/chown$ ./test.sh 23bin dev initrd.img lost+found opt run srv usr 24boot etc lib media proc sbin sys var 25cdrom home lib64 mnt root snap tmp vmlinuz
普通用户需要拥有r权限, 然后x权限 才能执行
1ghostwu@dev:~/linux/chown$ ls -l test.sh 2-rwxrw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 3ghostwu@dev:~/linux/chown$ chmod u-r test.sh 4ghostwu@dev:~/linux/chown$ ls -l test. 5ls: cannot access 'test.': No such file or directory 6ghostwu@dev:~/linux/chown$ ls -l test.sh 7--wxrw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 8ghostwu@dev:~/linux/chown$ ./test.sh 9bash: ./test.sh: Permission denied
root用户不需要r权限,只要有x权限就能执行
1root@dev:/home/ghostwu/linux/chown# ls -l test.sh 2--wxrw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 3root@dev:/home/ghostwu/linux/chown# ./test.sh 4bin dev initrd.img lost+found opt run srv usr 5boot etc lib media proc sbin sys var 6cdrom home lib64 mnt root snap tmp vmlinuz 7root@dev:/home/ghostwu/linux/chown# chmod a-x test.sh 8root@dev:/home/ghostwu/linux/chown# ls -l test.sh 9--w-rw-r-- 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 10root@dev:/home/ghostwu/linux/chown# ./test.sh 11-su: ./test.sh: Permission denied 12root@dev:/home/ghostwu/linux/chown# chmod o+x test.sh 13root@dev:/home/ghostwu/linux/chown# ls -l test.sh 14--w-rw-r-x 1 ghostwu ghostwu 5 5月 9 23:22 test.sh 15root@dev:/home/ghostwu/linux/chown# ./test.sh 16bin dev initrd.img lost+found opt run srv usr 17boot etc lib media proc sbin sys var 18cdrom home lib64 mnt root snap tmp vmlinuz
二、目录权限
可读r: 具有浏览目录下面文件及其子目录的权限,即:ls 目录
1ghostwu@dev:~/linux$ ls -l 2total 12 3drwxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 4drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 5drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 6ghostwu@dev:~/linux$ ls chown 7test.sh test.txt 8ghostwu@dev:~/linux$ chmod u-r chown 9ghostwu@dev:~/linux$ ls -l chown 10ls: cannot open directory 'chown': Permission denied 11ghostwu@dev:~/linux$ ls -l 12total 12 13d-wxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown
没有x权限,不能cd切换到目录
1ghostwu@dev:~/linux$ ls -l 2total 12 3d-wxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 4drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 5drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 6ghostwu@dev:~/linux$ cd chown 7ghostwu@dev:~/linux/chown$ ls 8ls: cannot open directory '.': Permission denied 9 10ghostwu@dev:~/linux/chown$ cd .. 11ghostwu@dev:~/linux$ ls -l 12total 12 13d-wxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 14drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 15drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 16ghostwu@dev:~/linux$ chmod u-x chown 17ghostwu@dev:~/linux$ ls -l 18total 12 19d-w-rwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 20drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 21drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 22ghostwu@dev:~/linux$ cd chown 23-su: cd: chown: Permission denied
w: 具有增加,删除或者修改目录内文件名的权限,需要x权限配合
1ghostwu@dev:~/linux$ ls -l 2total 12 3d-w-rwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 4drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 5drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 6ghostwu@dev:~/linux$ cd chown 7-su: cd: chown: Permission denied 8ghostwu@dev:~/linux$ chmod u+x chown 9ghostwu@dev:~/linux$ ls -l 10total 12 11d-wxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:22 chown 12drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 13drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 14ghostwu@dev:~/linux$ cd chown 15ghostwu@dev:~/linux/chown$ ls -l 16ls: cannot open directory '.': Permission denied 17ghostwu@dev:~/linux/chown$ touch a.txt 18ghostwu@dev:~/linux/chown$ ls -l . 19ls: cannot open directory '.': Permission denied 20ghostwu@dev:~/linux/chown$ ls -l a.txt 21-rw-rw-r-- 1 ghostwu ghostwu 0 5月 9 23:34 a.txt
如果父目录没有w权限,是不能删除目录下面的文件的
1ghostwu@dev:~/linux/chown$ ls -l a.txt 2-rw-rw-r-- 1 ghostwu ghostwu 0 5月 9 23:34 a.txt 3ghostwu@dev:~/linux/chown$ rm -f a.txt 4ghostwu@dev:~/linux/chown$ ls -l a.txt 5ls: cannot access 'a.txt': No such file or directory 6ghostwu@dev:~/linux/chown$ touch a.txt 7ghostwu@dev:~/linux/chown$ cd .. 8ghostwu@dev:~/linux$ ls -l 9total 12 10d-wxrwxr-x 2 ghostwu ghostwu 4096 5月 9 23:35 chown 11drwxr-xrwx 6 root root 4096 5月 7 22:38 cp 12drwxrwxr-x 3 ghostwu ghostwu 4096 5月 8 23:01 rename 13ghostwu@dev:~/linux$ chmod u-w chown 14ghostwu@dev:~/linux$ ls -l chown/a.txt 15-rw-rw-r-- 1 ghostwu ghostwu 0 5月 9 23:35 chown/a.txt 16ghostwu@dev:~/linux$ rm -f chown/a.txt 17rm: cannot remove 'chown/a.txt': Permission denied
x: 具有进入目录的权限:如cd dir
>没有r无法列表
>没有w无法新建文件