Linux常用命令-chmod

命令

chmod

描述

change file mode bits
更改文件权限

用法

1
2
3
chmod [OPTION]... MODE[,MODE]... FILE...
chmod [OPTION]... OCTAL-MODE FILE...
chmod [OPTION]... --reference=RFILE FILE...

选项

1
2
3
4
5
6
7
8
Options:
-c 只显示权限更改情况,如果没有更改则不显示
-f 不显示错误信息
-v 显示详情
-R 递归选项
--no-preserve-root 允许更改/根权限(默认选项)
--preserve-root 不允许递归更改/根权限
--reference=RFILE 根据指定文件权限更改目标文件权限

注意

链接文件的权限默认为777,无法修改,使用chmod对链接文件的权限修改将应用于源文件
递归更改目录权限之前,一定要细分到最小目录层级,并确定目标目录存在,否则可能导致目录权限错乱,如果是系统目录权限错乱,则可能导致系统运行出错
一旦递归更改权限后,将无法还原之前的文件权限

补充

指定权限有两种方式:数字模式和字母模式

数字模式
由4位八进制数字(0-7)组成,如果少于4位,则默认前导为0补齐4位,如755=0755,4=0004
第1位表示三个特殊权限suid-4,sgid-2,sticky-1,none-0
第2位表示所有者权限read-4,write-2,execute-1,none-0
第3位表示所属组权限r-4,w-2,x-1,n-0
第4位表示其他用户权限r-4,w-2,x-1,n-0

字母模式
由用户+运算符+权限三部分组成,格式为[ugoa][+-=][rwxXst]…
注意当指定特殊权限st时,必须同时指定x权限,如rxt,rwxs

三类用户ugo
u: owner user,所有者
g: group users,所属组
o: other users,其他用户
a: all users,所有用户,可省略,默认为所有用户,如a+r可简写为+r

三种运算符
+-表示相对权限,即在当前权限基础上增加或删除权限,如a+x,g-w
=表示指定权限,即指定目标权限,如g=rwx,o=rx

三种标准权限rwx,三种特殊权限Xst
r: read
w: write
x: execute for file,search for directory
X: 同x,但是仅对于目录或已有执行权限的文件生效
s: suid,sgid,在执行时设置uid或gid,继承目录所属组
t: sticky bit,限制删除标志或粘滞位,禁止有写权限的用户删除或重命名目录中其他用户的文件

suid和sgid权限
suid和sgid一般用于可执行文件或脚本,添加了suid权限后,表示任何用户执行该文件均以文件所有者身份执行;添加了sgid权限后,表示任何用户执行该文件均以文件所属组用户身份执行.例如系统中的/usr/bin/passwd命令就默认添加了suid权限,即所有用户均可以root身份执行
sgid还可以用于目录,用来继承目录的用户组属性,添加了sgid的目录,则任何用户在该目录下新建文件的所属组均为该目录的所属组,可用于共享目录

设置了suid或sgid权限后,将会更改文件所有者或所属组执行权限位置上的标识,有执行权限则更改为小写s,没有执行权限则更改为大写S,即如果对应的执行权限位置上原来为x则更改为小写s,原来为-则更改为大写S

1
2
3
4
5
6
7
8
9
10
11
-rwsr-xr-x  表示设置了suid且所有者有执行权限
-rwSr--r-- 表示设置了suid但所有者没有执行权限
-rwxr-sr-x 表示设置了sgid且所属组有执行权限
-rw-r-Sr-- 表示设置了sgid但所属组没有执行权限

# 添加或删除suid权限
chmod u+s file
chmod u-s file
# 添加或删除sgid权限
chmod g+s file
chmod g-s file

sticky权限
限制删除标志或粘滞位权限,粘滞位是针对有执行权限的目录的,对于文件添加粘滞位并没有什么作用.当目录设置了粘滞位权限以后,即便用户对该目录有写入权限,也不能删除或重命名该目录中其他用户的文件,只有文件的所有者和root用户才有权将其删除.例如系统中的/tmp目录就添加了sticky权限,以防止普通用户删除或移动其他用户的文件

设置了sticky权限后,将会更改其他用户执行权限位置上的标识,有执行权限则更改为小写t,没有执行权限则更改为大写T

1
2
3
4
5
6
-rwxr-xr-t  表示设置了粘滞位且其他用户有可执行权限
-rwxr--r-T 表示设置了粘滞位但其他用户没有可执行权限

# 添加或删除sticky权限
chmod o+t dir
chmod o-t dir

特殊权限 SUID、SGID、Sticky
https://www.cnblogs.com/Q--T/p/7864795.html

标准权限

数字模式 字母模式 权限显示 二进制 说明
0 --- 000 无权限
1 x --x 001 执行
2 w -w- 010
3 wx -wx 011 写+执行
4 r r-- 100
5 rx r-x 101 读+执行
6 rw rw- 110 读+写
7 rwx rwx 111 读+写+执行

特殊权限

数字模式 字母模式 权限显示 二进制 说明
0 --- 000 无权限
1 t --t 001 sticky
2 s -s- 010 sgid
3 st -st 011 sgid + sticky
4 s s-- 100 suid
5 st s-t 101 suid + sticky
6 ss ss- 110 suid + sgid
7 sst sst 111 suid + sgid + sticky

标准权限设置

八进制数 增加权限 删除权限 说明
1 +x -x 执行
2 +w -w
3 +wx -wx 写+执行
4 +r -r
5 +rx -rx 读+执行
6 +rw -rw 读+写
7 +rwx -rwx 读+写+执行

特殊权限设置
注意以下表格中的ogu标识不能省略

八进制数 增加权限 删除权限 说明
1 o+t o-t sticky
2 g+s g-s sgid
4 u+s u-s suid

设置标准权限推荐使用数字模式,设置特殊权限推荐使用字母模式

常用权限示例
数字模式=(字母模式)
644=0644=(u=rw,g=r,o=r)
755=0755=(u=rwx,g=rx,o=rx)
1755=(u=rwx,g=rx,o=rxt)
4775=(u=rwx,g=rwxs,o=rx)

示例

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
$ ll a
-rw-r--r-- 1 root root 12 Jan 11 21:01 a
$ chmod -c 755 a
changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
$ ll a
-rwxr-xr-x 1 root root 12 Jan 11 21:01 a
$ chmod -v 644 a
changed from 0757 (rwxr-xrwx) to 0644 (rw-r--r--)
$ chmod -v 644 a
retained as 0644 (rw-r--r--)

$ ll a b
-rwxr-xr-x 1 root root 12 Jan 11 21:01 a
-rw-r--r-- 1 root root 12 Jan 11 21:04 b
$ chmod --reference=a b
$ ll a b
-rwxr-xr-x 1 root root 12 Jan 11 21:01 a
-rwxr-xr-x 1 root root 12 Jan 11 21:04 b

# 不足4位,则默认用0填充
$ ll file
-rw-r--r-- 1 root root 0 Feb 24 15:25 file
$ chmod -v 2 file
mode of ‘file’ changed from 0644 (rw-r--r--) to 0002 (-------w-)
$ chmod -v 22 file
mode of ‘file’ changed from 0002 (-------w-) to 0022 (----w--w-)
$ chmod -v 222 file
mode of ‘file’ changed from 0022 (----w--w-) to 0222 (-w--w--w-)
$ chmod -v 2222 file
mode of ‘file’ changed from 0222 (-w--w--w-) to 2222 (-w--wS-w-)
$ chmod -v -2222 file
mode of ‘file’ changed from 2222 (-w--wS-w-) to 0000 (---------)
$ chmod -v 644 file
mode of ‘file’ changed from 2222 (-w--wS-w-) to 0644 (rw-r--r--)
$ chmod -v 755 file
mode of ‘file’ changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
# 指定的权限为文件的最终权限,特殊权限不会累加
$ chmod -v 4770 file
mode of ‘file’ changed from 0755 (rwxr-xr-x) to 4770 (rwsrwx---)
$ chmod -v 6770 file
mode of ‘file’ changed from 4770 (rwsrwx---) to 6770 (rwsrws---)
$ chmod -v 2770 file
mode of ‘file’ changed from 6770 (rwsrws---) to 2770 (rwxrws---)
$ chmod -v 644 file
mode of ‘file’ changed from 2770 (rwxrws---) to 0644 (rw-r--r--)
# 指定suid权限
$ chmod -v 4755 file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4755 (rwsr-xr-x)

$ ll -d dir
drwxr-xr-x 2 root root 6 Feb 24 15:28 dir/
$ chmod -v 770 dir
mode of ‘dir’ changed from 0755 (rwxr-xr-x) to 0770 (rwxrwx---)
$ chmod -v 4770 dir
mode of ‘dir’ changed from 0770 (rwxrwx---) to 4770 (rwsrwx---)
# 目录的特殊权限为自动累加,而非指定的权限,即4770->2770->6770,这里和文件权限有差别
$ chmod -v 2770 dir
mode of ‘dir’ changed from 4770 (rwsrwx---) to 6770 (rwsrws---)
# 特殊权限位指定为0也无法删除对应权限
$ chmod -v 0770 dir
mode of ‘dir’ retained as 6770 (rwsrws---)
# 可以指定-当前权限值或-7777来清空所有权限,如下的-6770
$ chmod -v -6770 dir
mode of ‘dir’ changed from 6770 (rwsrws---) to 0000 (---------)
$ chmod -v -7777 dir
mode of ‘dir’ changed from 6770 (rwsrws---) to 0000 (---------)
# 再还原目录的默认权限755
$ chmod -v 755 dir
mode of ‘dir’ changed from 0000 (---------) to 0755 (rwxr-xr-x)
# 指定sgid和sticky权限,可以拆分开2755+1755,也可以直接指定为3755
$ chmod -v 2755 dir
mode of ‘dir’ changed from 0755 (rwxr-xr-x) to 2755 (rwxr-sr-x)
$ chmod -v 1755 dir
mode of ‘dir’ changed from 2755 (rwxr-sr-x) to 3755 (rwxr-sr-t)
$ chmod -v 3755 dir
mode of ‘dir’ retained as 3755 (rwxr-sr-t)

# 添加和删除所有权限,包括特殊权限
$ chmod -v 7777 dir
mode of ‘dir’ changed from 0755 (rwxr-xr-x) to 7777 (rwsrwsrwt)
$ chmod -v -7777 dir
mode of ‘dir’ changed from 7777 (rwsrwsrwt) to 0000 (---------)



# 默认用户是a,即所有用户,644 -> 755
$ chmod -v +x file
mode of ‘file’ changed from 0644 (rw-r--r--) to 0755 (rwxr-xr-x)
$ chmod -v -x file
mode of ‘file’ changed from 0755 (rwxr-xr-x) to 0644 (rw-r--r--)
# 指定所有者加x权限
$ chmod -v u+x file
mode of ‘file’ changed from 0644 (rw-r--r--) to 0744 (rwxr--r--)
# 指定所有者和所属组加x
$ chmod -v ug+x file
mode of ‘file’ changed from 0744 (rwxr--r--) to 0754 (rwxr-xr--)
$ chmod -v a+x file
mode of ‘file’ changed from 0754 (rwxr-xr--) to 0755 (rwxr-xr-x)
# 指定所属组的权限和所有者的权限相同g=u
$ chmod -v g=u file
mode of ‘file’ changed from 0755 (rwxr-xr-x) to 0775 (rwxrwxr-x)
# 指定权限
$ chmod -v u=rw,g=r,o=r file
mode of ‘file’ changed from 0775 (rwxrwxr-x) to 0644 (rw-r--r--)
# 指定所有用户权限均和所有者的权限相同a=u
$ chmod -v a=u file
mode of ‘file’ changed from 0644 (rw-r--r--) to 0666 (rw-rw-rw-)
# 指定suid权限,注意需要同步增加x权限
$ chmod -v u=rwxs,g=rx,o=r file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4754 (rwsr-xr--)
$ chmod -v u-s file
mode of ‘file’ changed from 4754 (rwsr-xr--) to 0754 (rwxr-xr--)
# 清空所有权限可以使用ugo=或a=,即指定空权限
$ chmod -v ugo= file
mode of ‘file’ changed from 0754 (rwxr-xr--) to 0000 (---------)
# 指定suid和sgid
$ chmod -v u=rwxs,g=rwxs,o=r file
mode of ‘file’ changed from 0000 (---------) to 6774 (rwsrwsr--)
$ chmod -v a= file
mode of ‘file’ changed from 6774 (rwsrwsr--) to 0000 (---------)

# 大小写Xx
$ ll file
-rwx------ 1 root root 0 Feb 24 15:25 file
# 文件原来有x权限的,+X有效
$ chmod -v +X file
mode of ‘file’ changed from 0700 (rwx------) to 0711 (rwx--x--x)
$ chmod -v -X file
mode of ‘file’ changed from 0711 (rwx--x--x) to 0600 (rw-------)
# 文件原来没有任何x权限的,+X无效,需要使用+x
$ chmod -v +X file
mode of ‘file’ retained as 0600 (rw-------)
$ chmod -v +x file
mode of ‘file’ changed from 0600 (rw-------) to 0711 (rwx--x--x)

# suid和sgid权限
$ chmod 644 file
# 单独加s权限无效,显示为大写S,需要配合x一起使用才行,即+xs
$ chmod -v u+s file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4644 (rwSr--r--)
# 增加suid权限
$ chmod -v u+xs file
mode of ‘file’ changed from 4644 (rwSr--r--) to 4744 (rwsr--r--)
# 增加sgid权限
$ chmod -v g+xs file
mode of ‘file’ changed from 4744 (rwsr--r--) to 6754 (rwsr-sr--)

# 注意s权限和x权限的不同模式之间可能会覆盖,不建议数字模式和字母模式混用
# 如先+s后指定755权限就会覆盖
$ chmod 644 file
$ chmod -v u+s file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4644 (rwSr--r--)
# 指定755后suid权限被覆盖
$ chmod -v 755 file
mode of ‘file’ changed from 4644 (rwSr--r--) to 0755 (rwxr-xr-x)
$ chmod -v 644 file
mode of ‘file’ changed from 0755 (rwxr-xr-x) to 0644 (rw-r--r--)
# 分开指定+s和+x权限则为累加不会覆盖
$ chmod -v u+s file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4644 (rwSr--r--)
$ chmod -v u+x file
mode of ‘file’ changed from 4644 (rwSr--r--) to 4744 (rwsr--r--)
# 推荐直接合并指定+xs权限
$ chmod -v u+xs file
mode of ‘file’ changed from 0644 (rw-r--r--) to 4744 (rwsr--r--)

# sgid作用于目录,则可以继承目录的用户组属性
$ chown root:usera dir
# 目录增加写权限
$ chmod 777 dir
# 目录增加sgid权限
$ chmod -v g+s dir
mode of ‘dir’ changed from 0777 (rwxrwxrwx) to 2777 (rwxrwsrwx)
# 也可以直接通过数字模式指定sgid权限和写权限
$ chmod 2777 dir
$ ll -d dir
drwxrwsrwx 2 root usera 54 Feb 23 20:18 dir/
# 在目录中新建文件的用户组属性为usera而非创建者,自动继承dir目录的用户组
$ echo aaa > dir/aaa
$ ll dir/
-rw-r--r-- 1 root usera 4 Feb 23 20:19 aaa

# 系统中passwd命令默认具有suid权限
$ ll /usr/bin/passwd
-rwsr-xr-x. 1 root root 27856 Apr 1 2020 /usr/bin/passwd
# 系统中/tmp目录默认具有sticky权限
$ ll -d /tmp
drwxrwxrwt. 13 root root 4096 Feb 22 11:29 /tmp/

# /tmp具有sticky权限且所有用户均具有写权限,即所有用户均可以创建和删除文件,但是只能删除自己创建的文件
$ su - usera
$ echo aaa > /tmp/aaa
$ exit
$ su - userb
$ echo bbb > /tmp/bbb
$ ll -d /tmp
drwxrwxrwt. 14 root root 4096 Feb 24 16:55 /tmp
# 用户userb想要删除usera的aaa文件,提示没有权限,只能删除自己创建的bbb文件
$ rm /tmp/aaa
rm: remove write-protected regular file ‘/tmp/aaa’? y
rm: cannot remove ‘/tmp/aaa’: Operation not permitted
$ rm /tmp/bbb
$ ll /tmp/bbb
ls: cannot access /tmp/bbb: No such file or directory

# 添加删除特殊权限推荐先使用数字模式更改标准权限,然后使用字母模式更改特殊权限
$ chmod -v -7777 dir
mode of ‘dir’ changed from 6755 (rwsr-sr-x) to 0000 (---------)
$ chmod -v 755 dir
mode of ‘dir’ changed from 0000 (---------) to 0755 (rwxr-xr-x)
$ chmod -v o+t dir
mode of ‘dir’ changed from 0755 (rwxr-xr-x) to 1755 (rwxr-xr-t)
$ chmod -v g+s dir
mode of ‘dir’ changed from 1755 (rwxr-xr-t) to 3755 (rwxr-sr-t)
$ chmod -v u+s dir
mode of ‘dir’ changed from 3755 (rwxr-sr-t) to 7755 (rwsr-sr-t)
$ chmod -v u-s dir
mode of ‘dir’ changed from 7755 (rwsr-sr-t) to 3755 (rwxr-sr-t)
$ chmod -v g-s dir
mode of ‘dir’ changed from 3755 (rwxr-sr-t) to 1755 (rwxr-xr-t)
$ chmod -v o-t dir
mode of ‘dir’ changed from 1755 (rwxr-xr-t) to 0755 (rwxr-xr-x)
$ chmod -v g+s,o+t dir
mode of ‘dir’ changed from 0755 (rwxr-xr-x) to 3755 (rwxr-sr-t)
$ chmod -v g-s,o-t dir
mode of ‘dir’ changed from 3755 (rwxr-sr-t) to 0755 (rwxr-xr-x)