cvsimport
[findutils.git] / doc / perm.texi
blobf5190730e5d1965551aef0fbe9e2efc564ab9154
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.
5 @menu
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.
10 @end menu
12 @node Mode Structure
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:
21 @enumerate
22 @item
23 @cindex read permission
24 permission to read the file.  For directories, this means permission to
25 list the contents of the directory.
26 @item
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.
30 @item
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.
34 @end enumerate
36 There are three categories of users who may have different permissions
37 to perform any of the above operations on a file:
39 @enumerate
40 @item
41 the file's owner;
42 @item
43 other users who are in the file's group;
44 @item
45 everyone else.
46 @end enumerate
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:
61 @enumerate
62 @item
63 @cindex set-user-ID
64 @cindex setuid
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.
70 @item
71 @cindex set-group-ID
72 @cindex setgid
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
78 subdirectories.
79 @item
80 @cindex sticky
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
91 @dfn{sticky bit}.
92 @end enumerate
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
101 @table @asis
102 @item ext2
103 On @acronym{GNU} and @acronym{GNU}/Linux the file attributes specific to
104 the ext2 file system are set using @command{chattr}.
106 @item FFS
107 On FreeBSD the file flags specific to the FFS
108 file system are set using @command{chflags}.
109 @end table
111 Even if a file's mode bits allow an operation on that file,
112 that operation may still fail, because:
114 @itemize
115 @item
116 the file-system-specific attributes or flags do not permit it; or
118 @item
119 the file system is mounted as read-only.
120 @end itemize
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}.
126 @node Symbolic Modes
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:
138 @example
139 @r{[}ugoa@dots{}@r{][}+-=@r{]}@var{perms}@dots{}@r{[},@dots{}@r{]}
140 @end example
142 @noindent
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
147 symbolic modes.
149 @menu
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.
156 @end menu
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
164 format:
166 @example
167 @var{users} @var{operation} @var{permissions}
168 @end example
170 @noindent
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
178 not matter.
180 @table @code
181 @item u
182 @cindex owner of file, permissions for
183 the user who owns the file;
184 @item g
185 @cindex group, permissions for
186 other users who are in the file's group;
187 @item o
188 @cindex other permissions
189 all other users;
190 @item a
191 all users; the same as @samp{ugo}.
192 @end table
194 The @var{operation} part tells how to change the affected users' access
195 to the file, and is one of the following symbols:
197 @table @code
198 @item +
199 @cindex adding permissions
200 to add the @var{permissions} to whatever permissions the @var{users}
201 already have for the file;
202 @item -
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;
207 @item =
208 @cindex setting permissions
209 to make the @var{permissions} the only permissions that the @var{users}
210 have for the file.
211 @end table
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
218 at all to the file.
220 @table @code
221 @item r
222 @cindex read permission, symbolic
223 the permission the @var{users} have to read the file;
224 @item w
225 @cindex write permission, symbolic
226 the permission the @var{users} have to write to the file;
227 @item x
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.
231 @end table
233 For example, to give everyone permission to read and write a regular file,
234 but not to execute it, use:
236 @example
237 a=rw
238 @end example
240 To remove write permission for all users other than the file's
241 owner, use:
243 @example
244 go-w
245 @end example
247 @noindent
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.
256 @example
258 @end example
260 @noindent
261 Another way to specify the same thing is:
263 @example
264 og-rwx
265 @end example
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}
274 letters after the
275 operator, you use the letter @samp{u}, @samp{g}, or @samp{o}.  For
276 example, the mode
278 @example
280 @end example
282 @noindent
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
289 analogously.
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:
318 @example
320 @end example
322 To remove both set-user-ID and set-group-ID mode bits from
323 it, you can use the mode:
325 @example
327 @end example
329 To set the restricted deletion flag or sticky bit, you can use
330 the mode:
332 @example
334 @end example
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:
343 @example
345 @end example
347 @noindent
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:
365 @example
367 @end example
369 @noindent
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:
386 @example
387 og+rX-w
388 @end example
390 @noindent
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
396 the two modes:
398 @example
399 og+rX
400 og-w
401 @end example
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:
406 @example
407 a+r,go-w
408 @end example
410 @noindent
411 gives everyone permission to read the file and removes write
412 permission on it for all users except its owner.  Another example:
414 @example
415 u=rwx,g=rx,o=
416 @end example
418 @noindent
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
421 it.)
423 The two methods can be combined.  The mode:
425 @example
426 a+r,g+x-w
427 @end example
429 @noindent
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
433 ways; another is:
435 @example
436 u+r,g+rx,o+r,g-w
437 @end example
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:
459 @example
461 @end example
463 @noindent
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,
466 the mode:
468 @example
470 @end example
472 @noindent
473 ignores @code{umask}, and @emph{does} give write permission for
474 the file to all users.
476 @node Numeric Modes
477 @section Numeric Modes
479 @cindex numeric modes
480 @cindex file mode bits, numeric
481 @cindex octal numbers for file modes
482 As an
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
487 mode @samp{55}.
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:
503 @example
504 Value in  Corresponding
505 Mode      Mode Bit
507           Other users not in the file's group:
508    1      Execute/search
509    2      Write
510    4      Read
512           Other users in the file's group:
513   10      Execute/search
514   20      Write
515   40      Read
517           The file's owner:
518  100      Execute/search
519  200      Write
520  400      Read
522           Special mode bits:
523 1000      Restricted deletion flag or sticky bit
524 2000      Set group ID on execution
525 4000      Set user ID on execution
526 @end example
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
531 @samp{a=}.
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:
555 @example
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.
559 mkdir A B C
560 chmod 755 A
561 chmod 0755 B
562 chmod u=rwx,go=rx C
563 mkdir -m 755 D
564 mkdir -m 0755 E
565 mkdir -m u=rwx,go=rx F
566 @end example
568 If you want to try to set these bits, you must mention them
569 explicitly in the symbolic or numeric modes, e.g.:
571 @example
572 # These commands try to set the set-user-ID
573 # and set-group-ID bits of the subdirectories.
574 mkdir G H
575 chmod 6755 G
576 chmod u=rwx,go=rx,a+s H
577 mkdir -m 6755 I
578 mkdir -m u=rwx,go=rx,a+s J
579 @end example
581 If you want to try to clear these bits, you must mention them
582 explicitly in a symbolic mode, e.g.:
584 @example
585 # This command tries to clear the set-user-ID
586 # and set-group-ID bits of the directory D.
587 chmod a-s D
588 @end example
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.