1 Each file has a set of @dfn{file mode bits} that control the kinds of
2 access that users have to that file. They can be represented either in
3 symbolic form or as an octal number.
6 * Mode Structure:: Structure of file mode bits.
7 * Symbolic Modes:: Mnemonic representation of file mode bits.
8 * Numeric Modes:: File mode bits as octal numbers.
9 * Directory Setuid and Setgid:: Set-user-ID and set-group-ID on directories.
13 @section Structure of File Mode Bits
15 The file mode bits have two parts: the @dfn{file permission bits},
16 which control ordinary access to the file, and @dfn{special mode
17 bits}, which affect only some files.
19 There are three kinds of permissions that a user can have for a file:
23 @cindex read permission
24 permission to read the file. For directories, this means permission to
25 list the contents of the directory.
27 @cindex write permission
28 permission to write to (change) the file. For directories, this means
29 permission to create and remove files in the directory.
31 @cindex execute/search permission
32 permission to execute the file (run it as a program). For directories,
33 this means permission to access files in the directory.
36 There are three categories of users who may have different permissions
37 to perform any of the above operations on a file:
43 other users who are in the file's group;
48 @cindex owner, default
49 @cindex group owner, default
50 Files are given an owner and group when they are created. Usually the
51 owner is the current user and the group is the group of the directory
52 the file is in, but this varies with the operating system, the
53 file system the file is created on, and the way the file is created. You
54 can change the owner and group of a file by using the @command{chown} and
55 @command{chgrp} commands.
57 In addition to the three sets of three permissions listed above, the
58 file mode bits have three special components, which affect only
59 executable files (programs) and, on most systems, directories:
65 Set the process's effective user ID to that of the file upon execution
66 (called the @dfn{set-user-ID bit}, or sometimes the @dfn{setgid bit}).
67 For directories on a few systems, give files created in the directory
68 the same owner as the directory, no matter who creates them, and set
69 the set-user-ID bit of newly-created subdirectories.
73 Set the process's effective group ID to that of the file upon execution
74 (called the @dfn{set-group-ID bit}, or sometimes the @dfn{setgid bit}).
75 For directories on most systems, give files created in the directory
76 the same group as the directory, no matter what group the user who
77 creates them is in, and set the set-group-ID bit of newly-created
81 @cindex swap space, saving text image in
82 @cindex text image, saving in swap space
83 @cindex restricted deletion flag
84 Prevent unprivileged users from removing or renaming a file in a directory
85 unless they own the file or the directory; this is called the
86 @dfn{restricted deletion flag} for the directory, and is commonly
87 found on world-writable directories like @file{/tmp}.
89 For regular files on some older systems, save the program's text image on the
90 swap device so it will load more quickly when run; this is called the
94 In addition to the file mode bits listed above, there may be file attributes
95 specific to the file system, e.g., access control lists (ACLs), whether a
96 file is compressed, whether a file can be modified (immutability), and whether
97 a file can be dumped. These are usually set using programs
98 specific to the file system. For example:
99 @c should probably say a lot more about ACLs... someday
103 On @acronym{GNU} and @acronym{GNU}/Linux the file attributes specific to
104 the ext2 file system are set using @command{chattr}.
107 On FreeBSD the file flags specific to the FFS
108 file system are set using @command{chflags}.
111 Even if a file's mode bits allow an operation on that file,
112 that operation may still fail, because:
116 the file-system-specific attributes or flags do not permit it; or
119 the file system is mounted as read-only.
122 For example, if the immutable attribute is set on a file,
123 it cannot be modified, regardless of the fact that you
124 may have just run @code{chmod a+w FILE}.
127 @section Symbolic Modes
129 @cindex symbolic modes
130 @dfn{Symbolic modes} represent changes to files' mode bits as
131 operations on single-character symbols. They allow you to modify either
132 all or selected parts of files' mode bits, optionally based on
133 their previous values, and perhaps on the current @code{umask} as well
134 (@pxref{Umask and Protection}).
136 The format of symbolic modes is:
139 @r{[}ugoa@dots{}@r{][}+-=@r{]}@var{perms}@dots{}@r{[},@dots{}@r{]}
143 where @var{perms} is either zero or more letters from the set
144 @samp{rwxXst}, or a single letter from the set @samp{ugo}.
146 The following sections describe the operators and other details of
150 * Setting Permissions:: Basic operations on permissions.
151 * Copying Permissions:: Copying existing permissions.
152 * Changing Special Mode Bits:: Special mode bits.
153 * Conditional Executability:: Conditionally affecting executability.
154 * Multiple Changes:: Making multiple changes.
155 * Umask and Protection:: The effect of the umask.
158 @node Setting Permissions
159 @subsection Setting Permissions
161 The basic symbolic operations on a file's permissions are adding,
162 removing, and setting the permission that certain users have to read,
163 write, and execute or search the file. These operations have the following
167 @var{users} @var{operation} @var{permissions}
171 The spaces between the three parts above are shown for readability only;
172 symbolic modes cannot contain spaces.
174 The @var{users} part tells which users' access to the file is changed.
175 It consists of one or more of the following letters (or it can be empty;
176 @pxref{Umask and Protection}, for a description of what happens then). When
177 more than one of these letters is given, the order that they are in does
182 @cindex owner of file, permissions for
183 the user who owns the file;
185 @cindex group, permissions for
186 other users who are in the file's group;
188 @cindex other permissions
191 all users; the same as @samp{ugo}.
194 The @var{operation} part tells how to change the affected users' access
195 to the file, and is one of the following symbols:
199 @cindex adding permissions
200 to add the @var{permissions} to whatever permissions the @var{users}
201 already have for the file;
203 @cindex removing permissions
204 @cindex subtracting permissions
205 to remove the @var{permissions} from whatever permissions the
206 @var{users} already have for the file;
208 @cindex setting permissions
209 to make the @var{permissions} the only permissions that the @var{users}
213 The @var{permissions} part tells what kind of access to the file should
214 be changed; it is normally zero or more of the following letters. As with the
215 @var{users} part, the order does not matter when more than one letter is
216 given. Omitting the @var{permissions} part is useful only with the
217 @samp{=} operation, where it gives the specified @var{users} no access
222 @cindex read permission, symbolic
223 the permission the @var{users} have to read the file;
225 @cindex write permission, symbolic
226 the permission the @var{users} have to write to the file;
228 @cindex execute/search permission, symbolic
229 the permission the @var{users} have to execute the file,
230 or search it if it is a directory.
233 For example, to give everyone permission to read and write a regular file,
234 but not to execute it, use:
240 To remove write permission for all users other than the file's
248 The above command does not affect the access that the owner of
249 the file has to it, nor does it affect whether other users can
250 read or execute the file.
252 To give everyone except a file's owner no permission to do anything with
253 that file, use the mode below. Other users could still remove the file,
254 if they have write permission on the directory it is in.
261 Another way to specify the same thing is:
267 @node Copying Permissions
268 @subsection Copying Existing Permissions
270 @cindex copying existing permissions
271 @cindex permissions, copying existing
272 You can base a file's permissions on its existing permissions. To do
273 this, instead of using a series of @samp{r}, @samp{w}, or @samp{x}
275 operator, you use the letter @samp{u}, @samp{g}, or @samp{o}. For
283 adds the permissions for users who are in a file's group to the
284 permissions that other users have for the file. Thus, if the file
285 started out as mode 664 (@samp{rw-rw-r--}), the above mode would change
286 it to mode 666 (@samp{rw-rw-rw-}). If the file had started out as mode
287 741 (@samp{rwxr----x}), the above mode would change it to mode 745
288 (@samp{rwxr--r-x}). The @samp{-} and @samp{=} operations work
291 @node Changing Special Mode Bits
292 @subsection Changing Special Mode Bits
294 @cindex changing special mode bits
295 In addition to changing a file's read, write, and execute/search permissions,
296 you can change its special mode bits. @xref{Mode Structure}, for a
297 summary of these special mode bits.
299 To change the file mode bits to set the user ID on execution, use
300 @samp{u} in the @var{users} part of the symbolic mode and
301 @samp{s} in the @var{permissions} part.
303 To change the file mode bits to set the group ID on execution, use
304 @samp{g} in the @var{users} part of the symbolic mode and
305 @samp{s} in the @var{permissions} part.
307 To set both user and group ID on execution, omit the @var{users} part
308 of the symbolic mode (or use @samp{a}) and use @samp{s} in the
309 @var{permissions} part.
311 To change the file mode bits to set the restricted deletion flag or sticky bit,
312 omit the @var{users} part of the symbolic mode (or use @samp{a}) and use
313 @samp{t} in the @var{permissions} part.
315 For example, to set the set-user-ID mode bit of a program,
316 you can use the mode:
322 To remove both set-user-ID and set-group-ID mode bits from
323 it, you can use the mode:
329 To set the restricted deletion flag or sticky bit, you can use
336 The combination @samp{o+s} has no effect. On @acronym{GNU} systems
337 the combinations @samp{u+t} and @samp{g+t} have no effect, and
338 @samp{o+t} acts like plain @samp{+t}.
340 The @samp{=} operator is not very useful with special mode bits.
341 For example, the mode:
348 does set the restricted deletion flag or sticky bit, but it also
349 removes all read, write, and execute/search permissions that users not in the
350 file's group might have had for it.
352 @xref{Directory Setuid and Setgid}, for additional rules concerning
353 set-user-ID and set-group-ID bits and directories.
355 @node Conditional Executability
356 @subsection Conditional Executability
358 @cindex conditional executability
359 There is one more special type of symbolic permission: if you use
360 @samp{X} instead of @samp{x}, execute/search permission is affected only if the
361 file is a directory or already had execute permission.
363 For example, this mode:
370 gives all users permission to search directories, or to execute files if
371 anyone could execute them before.
373 @node Multiple Changes
374 @subsection Making Multiple Changes
376 @cindex multiple changes to permissions
377 The format of symbolic modes is actually more complex than described
378 above (@pxref{Setting Permissions}). It provides two ways to make
379 multiple changes to files' mode bits.
381 The first way is to specify multiple @var{operation} and
382 @var{permissions} parts after a @var{users} part in the symbolic mode.
384 For example, the mode:
391 gives users other than the owner of the file read permission and, if
392 it is a directory or if someone already had execute permission
393 to it, gives them execute/search permission; and it also denies them write
394 permission to the file. It does not affect the permission that the
395 owner of the file has for it. The above mode is equivalent to
403 The second way to make multiple changes is to specify more than one
404 simple symbolic mode, separated by commas. For example, the mode:
411 gives everyone permission to read the file and removes write
412 permission on it for all users except its owner. Another example:
419 sets all of the permission bits for the file explicitly. (It
420 gives users who are not in the file's group no permission at all for
423 The two methods can be combined. The mode:
430 gives all users permission to read the file, and gives users who are in
431 the file's group permission to execute/search it as well, but not permission
432 to write to it. The above mode could be written in several different
439 @node Umask and Protection
440 @subsection The Umask and Protection
442 @cindex umask and modes
443 @cindex modes and umask
444 If the @var{users} part of a symbolic mode is omitted, it defaults to
445 @samp{a} (affect all users), except that any permissions that are
446 @emph{set} in the system variable @code{umask} are @emph{not affected}.
447 The value of @code{umask} can be set using the
448 @code{umask} command. Its default value varies from system to system.
450 @cindex giving away permissions
451 Omitting the @var{users} part of a symbolic mode is generally not useful
452 with operations other than @samp{+}. It is useful with @samp{+} because
453 it allows you to use @code{umask} as an easily customizable protection
454 against giving away more permission to files than you intended to.
456 As an example, if @code{umask} has the value 2, which removes write
457 permission for users who are not in the file's group, then the mode:
464 adds permission to write to the file to its owner and to other users who
465 are in the file's group, but @emph{not} to other users. In contrast,
473 ignores @code{umask}, and @emph{does} give write permission for
474 the file to all users.
477 @section Numeric Modes
479 @cindex numeric modes
480 @cindex file mode bits, numeric
481 @cindex octal numbers for file modes
483 alternative to giving a symbolic mode, you can give an octal (base 8)
484 number that represents the mode.
485 This number is always interpreted in octal; you do not have to add a
486 leading @samp{0}, as you do in C. Mode @samp{0055} is the same as
489 A numeric mode is usually shorter than the corresponding symbolic
490 mode, but it is limited in that normally it cannot take into account the
491 previous file mode bits; it can only set them absolutely.
492 (As discussed in the next section, the set-user-ID and set-group-ID
493 bits of directories are an exception to this general limitation.)
495 The permissions granted to the user,
496 to other users in the file's group,
497 and to other users not in the file's group each require three
498 bits, which are represented as one octal digit. The three special
499 mode bits also require one bit each, and they are as a group
500 represented as another octal digit. Here is how the bits are arranged,
501 starting with the lowest valued bit:
504 Value in Corresponding
507 Other users not in the file's group:
512 Other users in the file's group:
523 1000 Restricted deletion flag or sticky bit
524 2000 Set group ID on execution
525 4000 Set user ID on execution
528 For example, numeric mode @samp{4755} corresponds to symbolic mode
529 @samp{u=rwxs,go=rx}, and numeric mode @samp{664} corresponds to symbolic mode
530 @samp{ug=rw,o=r}. Numeric mode @samp{0} corresponds to symbolic mode
533 @node Directory Setuid and Setgid
534 @section Directories and the Set-User-ID and Set-Group-ID Bits
536 On most systems, if a directory's set-group-ID bit is set, newly
537 created subfiles inherit the same group as the directory, and newly
538 created subdirectories inherit the set-group-ID bit of the parent
539 directory. On a few systems, a directory's set-user-ID bit has a
540 similar effect on the ownership of new subfiles and the set-user-ID
541 bits of new subdirectories. These mechanisms let users share files
542 more easily, by lessening the need to use @command{chmod} or
543 @command{chown} to share new files.
545 These convenience mechanisms rely on the set-user-ID and set-group-ID
546 bits of directories. If commands like @command{chmod} and
547 @command{mkdir} routinely cleared these bits on directories, the
548 mechanisms would be less convenient and it would be harder to share
549 files. Therefore, a command like @command{chmod} does not affect the
550 set-user-ID or set-group-ID bits of a directory unless the user
551 specifically mentions them in a symbolic mode, or sets them in
552 a numeric mode. For example, on systems that support
553 set-group-ID inheritance:
556 # These commands leave the set-user-ID and
557 # set-group-ID bits of the subdirectories alone,
558 # so that they retain their default values.
565 mkdir -m u=rwx,go=rx F
568 If you want to try to set these bits, you must mention them
569 explicitly in the symbolic or numeric modes, e.g.:
572 # These commands try to set the set-user-ID
573 # and set-group-ID bits of the subdirectories.
576 chmod u=rwx,go=rx,a+s H
578 mkdir -m u=rwx,go=rx,a+s J
581 If you want to try to clear these bits, you must mention them
582 explicitly in a symbolic mode, e.g.:
585 # This command tries to clear the set-user-ID
586 # and set-group-ID bits of the directory D.
590 This behavior is a @acronym{GNU} extension. Portable scripts should
591 not rely on requests to set or clear these bits on directories, as
592 @acronym{POSIX} allows implementations to ignore these requests.