回到手册索引

命令用途

chmod(change mode)是 Linux/Unix 系统中用来更改文件或目录权限的命令。它可以设置文件或目录的读、写、执行权限,并且可以指定权限的设置方式,通常用于控制文件的访问权限。

常用用法示例

  1. 给文件添加执行权限

    1
    chmod +x script.sh

    这条命令给 script.sh 文件添加了执行权限。执行后,script.sh 文件将变为可执行文件。

  2. 去除文件的写权限

    1
    chmod -w file.txt

    去除了 file.txt 文件的写权限,执行后文件内容将无法编辑。

  3. 设置文件权限为 755

    1
    chmod 755 program

    将 program 文件的权限设置为 rwxr-xr-x,即所有者可以读、写、执行,组用户和其他用户可以读取和执行。

  4. 设置文件权限为 644

    1
    chmod 644 document.txt

    将 document.txt 文件的权限设置为 rw-r–r–,即所有者可以读、写,组用户和其他用户只能读取。

  5. 递归更改目录及其子文件权限

    1
    chmod -R 755 /path/to/directory

    使用 -R 选项递归地将 /path/to/directory 目录及其下所有文件的权限设置为 rwxr-xr-x。

  6. 给所有者和组用户添加写权限

    1
    chmod ug+w file.txt

    给 file.txt 文件的所有者和组用户添加写权限。此命令执行后,所有者和组用户都能修改文件内容。

  7. 给文件去除执行权限

    1
    chmod -x script.sh

    去除 script.sh 文件的执行权限,执行后文件将不再可以作为可执行文件运行。

  8. 通过符号模式设置权限

    1
    chmod u=rwx,g=rx,o=r myfile

    通过符号模式设置 myfile 文件的权限为所有者可读、写、执行,组用户可读、执行,其他用户只能读取。

常用参数选项

  • -R, –recursive
    递归地更改目录及其所有子目录和文件的权限。例如,chmod -R 755 /path/to/directory。

  • -v, –verbose
    显示权限更改的详细信息。使用该选项后,每次权限更改都会在终端中输出提示信息。

  • -c, –changes
    仅在权限更改时输出信息。与 -v 类似,但只会在更改发生时打印出变更内容。

  • -f, –silent, –quiet
    忽略所有错误消息。如果操作失败,系统不会显示错误信息。

  • –no-dereference
    不改变符号链接的权限,仅影响文件本身。

  • –reference=file
    将 file 文件的权限复制到目标文件。例如,chmod –reference=example.txt myfile 会将 example.txt 的权限应用到 myfile 上。

  • u, g, o
    用于指定文件的所有者(user)、组(group)和其他用户(other)。例如,chmod u+x file 表示给所有者添加执行权限。

  • +/-/
    用于增加、去除或设置权限。例如,chmod +x file 表示添加执行权限,chmod -x file 表示去除执行权限,chmod =r file 表示只保留读取权限。

原厂文档

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

NAME

chmod - change file mode bits

SYNOPSIS

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

DESCRIPTION

This manual page documents the GNU version of chmod. chmod
changes the file mode bits of each given file according to mode,
which can be either a symbolic representation of changes to make,
or an octal number representing the bit pattern for the new mode
bits.

The format of a symbolic mode is [ugoa...][[-+=][perms...]...],
where perms is either zero or more letters from the set rwxXst, or
a single letter from the set ugo. Multiple symbolic modes can be
given, separated by commas.

A combination of the letters ugoa controls which users' access to
the file will be changed: the user who owns it (u), other users in
the file's group (g), other users not in the file's group (o), or
all users (a). If none of these are given, the effect is as if
(a) were given, but bits that are set in the umask are not
affected.

The operator + causes the selected file mode bits to be added to
the existing file mode bits of each file; - causes them to be
removed; and = causes them to be added and causes unmentioned bits
to be removed except that a directory's unmentioned set user and
group ID bits are not affected.

The letters rwxXst select file mode bits for the affected users:
read (r), write (w), execute (or search for directories) (x),
execute/search only if the file is a directory or already has
execute permission for some user (X), set user or group ID on
execution (s), restricted deletion flag or sticky bit (t).
Instead of one or more of these letters, you can specify exactly
one of the letters ugo: the permissions granted to the user who
owns the file (u), the permissions granted to other users who are
members of the file's group (g), and the permissions granted to
users that are in neither of the two preceding categories (o).

A numeric mode is from one to four octal digits (0-7), derived by
adding up the bits with values 4, 2, and 1. Omitted digits are
assumed to be leading zeros. The first digit selects the set user
ID (4) and set group ID (2) and restricted deletion or sticky (1)
attributes. The second digit selects permissions for the user who
owns the file: read (4), write (2), and execute (1); the third
selects permissions for other users in the file's group, with the
same values; and the fourth for other users not in the file's
group, with the same values.

chmod doesn't change the permissions of symbolic links; the chmod
system call cannot change their permissions on most systems, and
most systems ignore permissions of symbolic links. However, for
each symbolic link listed on the command line, chmod changes the
permissions of the pointed-to file. In contrast, chmod ignores
symbolic links encountered during recursive directory traversals.
Options that modify this behavior are described in the OPTIONS
section.

SETUID AND SETGID BITS

chmod clears the set-group-ID bit of a regular file if the file's
group ID does not match the user's effective group ID or one of
the user's supplementary group IDs, unless the user has
appropriate privileges. Additional restrictions may cause the
set-user-ID and set-group-ID bits of MODE or RFILE to be ignored.
This behavior depends on the policy and functionality of the
underlying chmod system call. When in doubt, check the underlying
system behavior.

For directories chmod preserves set-user-ID and set-group-ID bits
unless you explicitly specify otherwise. You can set or clear the
bits with symbolic modes like u+s and g-s. To clear these bits
for directories with a numeric mode requires an additional leading
zero like 00755, leading minus like -6000, or leading equals like
=755.

RESTRICTED DELETION FLAG OR STICKY BIT

The restricted deletion flag or sticky bit is a single bit, whose
interpretation depends on the file type. For directories, it
prevents unprivileged users from removing or renaming a file in
the directory unless they own the file or the directory; this is
called the restricted deletion flag for the directory, and is
commonly found on world-writable directories like /tmp. For
regular files on some older systems, the bit saves the program's
text image on the swap device so it will load more quickly when
run; this is called the sticky bit.

OPTIONS

Change the mode of each FILE to MODE. With --reference, change
the mode of each FILE to that of RFILE.

-c, --changes
like verbose but report only when a change is made

-f, --silent, --quiet
suppress most error messages

-v, --verbose
output a diagnostic for every file processed

--dereference
affect the referent of each symbolic link, rather than the
symbolic link itself

-h, --no-dereference
affect each symbolic link, rather than the referent

--no-preserve-root
do not treat '/' specially (the default)

--preserve-root
fail to operate recursively on '/'

--reference=RFILE
use RFILE's mode instead of specifying MODE values. RFILE
is always dereferenced if a symbolic link.

-R, --recursive
change files and directories recursively

The following options modify how a hierarchy is traversed when the
-R option is also specified. If more than one is specified, only
the final one takes effect. -H is the default.

-H if a command line argument is a symbolic link to a
directory, traverse it

-L traverse every symbolic link to a directory encountered

-P do not traverse any symbolic links

--help display this help and exit

--version
output version information and exit

Each MODE is of the form
'[ugoa]*([-+=]([rwxXst]*|[ugo]))+|[-+=][0-7]+'.

AUTHOR

Written by David MacKenzie and Jim Meyering.

REPORTING BUGS

GNU coreutils online help:
<https://www.gnu.org/software/coreutils/>
Report any translation bugs to
<https://translationproject.org/team/>

COPYRIGHT

Copyright © 2025 Free Software Foundation, Inc. License GPLv3+:
GNU GPL version 3 or later <https://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

SEE ALSO

chmod(2)

Full documentation <https://www.gnu.org/software/coreutils/chmod>
or available locally via: info '(coreutils) chmod invocation'

COLOPHON

This page is part of the coreutils (basic file, shell and text
manipulation utilities) project. Information about the project
can be found at ⟨http://www.gnu.org/software/coreutils/⟩. If you
have a bug report for this manual page, see
⟨http://www.gnu.org/software/coreutils/⟩. This page was obtained
from the tarball coreutils-9.6.tar.xz fetched from
⟨http://ftp.gnu.org/gnu/coreutils/⟩ on 2024-02-02. If you
discover any rendering problems in this HTML version of the page,
or you believe there is a better or more up-to-date source for the
page, or you have corrections or improvements to the information
in this COLOPHON (which is not part of the original manual page),
send a mail to man-pages@man7.org