Linux常用基本命令(chmod)

chmod命令用来改变文件或者目录的权限,只有文件的属主和超级用户才能够执行这个命令

格式:

chmod [option] [mode] [file]

>常用参数选项 -R : 递归修改目录以及子目录下面的所有文件权限

>模式有两种格式,一种采用字母方式的表达式,另外一种是数字

1,首先需要了解文件的权限和属主和属组。

ghostwu@dev:~/linux/chown$ ls -l
total 4
-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,修改文件权限

>增加权限( + )

ghostwu@dev:~/linux/chown$ chmod a+x test.txt 
ghostwu@dev:~/linux/chown$ ls
test.txt
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxrwxr-x 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

a等价于 用户(u)+组(g)+其他组( o )。 a+x 就是给用户,组,其他组都加上x(可执行)权限

>去掉权限( - )

ghostwu@dev:~/linux/chown$ chmod a-x test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rw-rw-r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

>设置权限( = )

ghostwu@dev:~/linux/chown$ chmod a=r-- test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-r--r--r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

>给属主加上w( 可写 ), x( 可执行 ) 权限

ghostwu@dev:~/linux/chown$ chmod u+wx test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxr--r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

>给组加上wx权限

ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxr--r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ chmod g+wx test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxrwxr-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

>给其他组加上wx权限

ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxrwxr-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ chmod o+wx test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxrwxrwx 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

>r( 4 ), w( 2 ), x( 1 )

ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxrwxrwx 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ chmod 444 test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-r--r--r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

ghostwu@dev:~/linux/chown$ ls -l
total 4
-r--r--r-- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ chmod 755 test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxr-xr-x 1 ghostwu ghostwu 20 5月   9 22:55 test.txt

 

权限详解:

一、普通文件

可读r: 读取/阅读文件内容的权限

ghostwu@dev:~/linux/chown$ ls -l
total 4
-rwxr-xr-x 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ chmod 000 test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
---------- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ cat test.txt 
cat: test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod 400 test.txt 
ghostwu@dev:~/linux/chown$ ls -l
total 4
-r-------- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ cat test.txt 
this is a test file

 可写(w):具有新增,修改文件内容的权限

ghostwu@dev:~/linux/chown$ ls -l test.txt 
-r-------- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ echo 'aaa' > test.txt 
bash: test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+w test.txt 
ghostwu@dev:~/linux/chown$ ls -l test.txt 
-rw------- 1 ghostwu ghostwu 20 5月   9 22:55 test.txt
ghostwu@dev:~/linux/chown$ echo 'ghostwu' >> 'test.txt' 
ghostwu@dev:~/linux/chown$ cat test.txt 
this is a test file
ghostwu

可执行( x )

1,文件本身要用x权限

ghostwu@dev:~/linux/chown$ ls -l test.txt 
-rw------- 1 ghostwu ghostwu 28 5月   9 23:20 test.txt
ghostwu@dev:~/linux/chown$ ./test.txt
bash: ./test.txt: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+x test.txt 
ghostwu@dev:~/linux/chown$ ls -l test.txt 
-rwx------ 1 ghostwu ghostwu 28 5月   9 23:20 test.txt
ghostwu@dev:~/linux/chown$ ./test.txt 
./test.txt: line 1: this: command not found
./test.txt: line 2: ghostwu: command not found
ghostwu@dev:~/linux/chown$ echo 'ls /' > test.sh
ghostwu@dev:~/linux/chown$ ls -l
total 8
-rw-rw-r-- 1 ghostwu ghostwu  5 5月   9 23:22 test.sh
-rwx------ 1 ghostwu ghostwu 28 5月   9 23:20 test.txt
ghostwu@dev:~/linux/chown$ ./test.sh
bash: ./test.sh: Permission denied
ghostwu@dev:~/linux/chown$ chmod u+x test.sh
ghostwu@dev:~/linux/chown$ ls -l test.sh 
-rwxrw-r-- 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
ghostwu@dev:~/linux/chown$ ./test.sh 
bin    dev   initrd.img  lost+found  opt   run     srv  usr
boot   etc   lib     media         proc  sbin  sys  var
cdrom  home  lib64     mnt         root  snap  tmp  vmlinuz

 普通用户需要拥有r权限, 然后x权限 才能执行

ghostwu@dev:~/linux/chown$ ls -l test.sh
-rwxrw-r-- 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
ghostwu@dev:~/linux/chown$ chmod u-r test.sh 
ghostwu@dev:~/linux/chown$ ls -l test.
ls: cannot access 'test.': No such file or directory
ghostwu@dev:~/linux/chown$ ls -l test.sh 
--wxrw-r-- 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
ghostwu@dev:~/linux/chown$ ./test.sh 
bash: ./test.sh: Permission denied

root用户不需要r权限,只要有x权限就能执行

root@dev:/home/ghostwu/linux/chown# ls -l test.sh 
--wxrw-r-- 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh 
bin    dev   initrd.img  lost+found  opt   run     srv  usr
boot   etc   lib     media         proc  sbin  sys  var
cdrom  home  lib64     mnt         root  snap  tmp  vmlinuz
root@dev:/home/ghostwu/linux/chown# chmod a-x test.sh
root@dev:/home/ghostwu/linux/chown# ls -l test.sh 
--w-rw-r-- 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh
-su: ./test.sh: Permission denied
root@dev:/home/ghostwu/linux/chown# chmod o+x test.sh
root@dev:/home/ghostwu/linux/chown# ls -l test.sh 
--w-rw-r-x 1 ghostwu ghostwu 5 5月   9 23:22 test.sh
root@dev:/home/ghostwu/linux/chown# ./test.sh
bin    dev   initrd.img  lost+found  opt   run     srv  usr
boot   etc   lib     media         proc  sbin  sys  var
cdrom  home  lib64     mnt         root  snap  tmp  vmlinuz

 

二、目录权限

可读r: 具有浏览目录下面文件及其子目录的权限,即:ls 目录

ghostwu@dev:~/linux$ ls -l
total 12
drwxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ ls chown
test.sh  test.txt
ghostwu@dev:~/linux$ chmod u-r chown
ghostwu@dev:~/linux$ ls -l chown
ls: cannot open directory 'chown': Permission denied
ghostwu@dev:~/linux$ ls -l
total 12
d-wxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown

没有x权限,不能cd切换到目录

ghostwu@dev:~/linux$ ls -l
total 12
d-wxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ cd chown
ghostwu@dev:~/linux/chown$ ls
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ cd ..
ghostwu@dev:~/linux$ ls -l
total 12
d-wxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ chmod u-x chown
ghostwu@dev:~/linux$ ls -l
total 12
d-w-rwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ cd chown
-su: cd: chown: Permission denied

w: 具有增加,删除或者修改目录内文件名的权限,需要x权限配合

ghostwu@dev:~/linux$ ls -l
total 12
d-w-rwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ cd chown
-su: cd: chown: Permission denied
ghostwu@dev:~/linux$ chmod u+x chown
ghostwu@dev:~/linux$ ls -l
total 12
d-wxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:22 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ cd chown
ghostwu@dev:~/linux/chown$ ls -l
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ touch a.txt
ghostwu@dev:~/linux/chown$ ls -l .
ls: cannot open directory '.': Permission denied
ghostwu@dev:~/linux/chown$ ls -l a.txt
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   9 23:34 a.txt

如果父目录没有w权限,是不能删除目录下面的文件的

ghostwu@dev:~/linux/chown$ ls -l a.txt
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   9 23:34 a.txt
ghostwu@dev:~/linux/chown$ rm -f a.txt
ghostwu@dev:~/linux/chown$ ls -l a.txt
ls: cannot access 'a.txt': No such file or directory
ghostwu@dev:~/linux/chown$ touch a.txt
ghostwu@dev:~/linux/chown$ cd ..
ghostwu@dev:~/linux$ ls -l
total 12
d-wxrwxr-x 2 ghostwu ghostwu 4096 5月   9 23:35 chown
drwxr-xrwx 6 root    root    4096 5月   7 22:38 cp
drwxrwxr-x 3 ghostwu ghostwu 4096 5月   8 23:01 rename
ghostwu@dev:~/linux$ chmod u-w chown
ghostwu@dev:~/linux$ ls -l chown/a.txt
-rw-rw-r-- 1 ghostwu ghostwu 0 5月   9 23:35 chown/a.txt
ghostwu@dev:~/linux$ rm -f chown/a.txt
rm: cannot remove 'chown/a.txt': Permission denied

x: 具有进入目录的权限:如cd dir

>没有r无法列表

>没有w无法新建文件

 

posted @ 2018-05-09 23:37  ghostwu  阅读(2346)  评论(0编辑  收藏  举报
Copyright ©2017 ghostwu