首页 >> 学习 >> Linux常用命令 >> chmod
chmod命令

本节重要性:★★★★☆    本节难度:★★★★☆

如果您是一个文件的拥有者或特权用户(比如root用户或拥有root权限的其他用户),那么您可以用chmod(Change Mode)命令精准控制文件拥有者、所属组的其他成员及其他用户对这个文件的权限,也就是ls -l命令的结果里的第2-10列。

命令用途

修改文件的权限(或存取模式,mode)

命令格式

chmod [option] [file(dir)]

常用选项

-R	修改目录及其中所有内容的权限

常见用法

chmod命令的选项有两种写法。第一种是用u、g、o表示用户类型,用r、w、x和-表示权限,用加号(+)和减号(-)表示权限操作:

用户类型:
u	文件拥有者
g	文件所属组除文件拥有者之外的用户
o	其他用户

权限:
r	可读
w	可写
x	可执行
-	表示没有该项权限

操作:
+	添加权限
-	取消权限

如给我们前面的Shell脚本hello.sh添加可执行权限(没有可执行权限时是不能直接运行的,但可以用bash命令运行):

[peter@ibi98 ~]$ cp prac/hello.sh .
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rw-rw-r-- 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ ./hello.sh
bash: ./hello.sh: 权限不够
[peter@ibi98 ~]$ chmod u+x hello.sh
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rwxrw-r-- 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ ./hello.sh
Hello World!

用户类型(u、g、o)和权限(r、w、x)可随意组合,如:

[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rwxrw-r-- 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ chmod go+x hello.sh
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rwxrwxr-x 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ chmod ugo-wx hello.sh
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-r--r--r-- 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

第二种是用数字表示权限的组合,每一种权限对应的数字为:

-	0
x	1
w	2
r	4

利用这几个数字,可以得到数字从0-7代表的各种权限的组合:

---	0
--x	1
-w-	2
-wx	3
r--	4
r-x	5
rw-	6
rwx	7

用3个数字,就可以表示u、g、o三种用户的权限。如:

[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-r--r--r-- 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ chmod 777 hello.sh
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rwxrwxrwx 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac
[peter@ibi98 ~]$ chmod 755 hello.sh
[peter@ibi98 ~]$ ll
总用量 20
-rw-rw-r-- 1 peter peter  365  2月 19 20:30 at_LEC1_protein.fa
-rw-rw-r-- 1 peter peter    0  2月 22 15:26 file1
drwxrwxr-x 3 peter peter 4096  2月 22 13:29 flower
-rwxr-xr-x 1 peter peter   62  2月 22 17:05 hello.sh
drwxrwxr-x 2 peter peter 4096  2月 22 10:56 my_fruit
drwxrwxr-x 5 peter peter 4096  2月 22 11:38 prac

最常用的是777、755、744几种权限数字组合。这种权限表示法简单方便,熟悉了常用的权限数字组合后可以大大提高效率。

<<上一节  下一节>>