df: add --output=file to directly output specified arguments
[coreutils.git] / doc / perm.texi
blob3a2f8d4e837400554080c5b879f9649c644d2576
1 @c File mode bits
3 @c Copyright (C) 1994-2013 Free Software Foundation, Inc.
5 @c Permission is granted to copy, distribute and/or modify this document
6 @c under the terms of the GNU Free Documentation License, Version 1.3 or
7 @c any later version published by the Free Software Foundation; with no
8 @c Invariant Sections, with no Front-Cover Texts, and with no Back-Cover
9 @c Texts.  A copy of the license is included in the ``GNU Free
10 @c Documentation License'' file as part of this distribution.
12 Each file has a set of @dfn{file mode bits} that control the kinds of
13 access that users have to that file.  They can be represented either in
14 symbolic form or as an octal number.
16 @menu
17 * Mode Structure::              Structure of file mode bits.
18 * Symbolic Modes::              Mnemonic representation of file mode bits.
19 * Numeric Modes::               File mode bits as octal numbers.
20 * Operator Numeric Modes::      ANDing, ORing, and setting modes octally.
21 * Directory Setuid and Setgid:: Set-user-ID and set-group-ID on directories.
22 @end menu
24 @node Mode Structure
25 @section Structure of File Mode Bits
27 The file mode bits have two parts: the @dfn{file permission bits},
28 which control ordinary access to the file, and @dfn{special mode
29 bits}, which affect only some files.
31 There are three kinds of permissions that a user can have for a file:
33 @enumerate
34 @item
35 @cindex read permission
36 permission to read the file.  For directories, this means permission to
37 list the contents of the directory.
38 @item
39 @cindex write permission
40 permission to write to (change) the file.  For directories, this means
41 permission to create and remove files in the directory.
42 @item
43 @cindex execute/search permission
44 permission to execute the file (run it as a program).  For directories,
45 this means permission to access files in the directory.
46 @end enumerate
48 There are three categories of users who may have different permissions
49 to perform any of the above operations on a file:
51 @enumerate
52 @item
53 the file's owner;
54 @item
55 other users who are in the file's group;
56 @item
57 everyone else.
58 @end enumerate
60 @cindex owner, default
61 @cindex group owner, default
62 Files are given an owner and group when they are created.  Usually the
63 owner is the current user and the group is the group of the directory
64 the file is in, but this varies with the operating system, the
65 file system the file is created on, and the way the file is created.  You
66 can change the owner and group of a file by using the @command{chown} and
67 @command{chgrp} commands.
69 In addition to the three sets of three permissions listed above, the
70 file mode bits have three special components, which affect only
71 executable files (programs) and, on most systems, directories:
73 @enumerate
74 @item
75 @cindex set-user-ID
76 @cindex setuid
77 Set the process's effective user ID to that of the file upon execution
78 (called the @dfn{set-user-ID bit}, or sometimes the @dfn{setuid bit}).
79 For directories on a few systems, give files created in the directory
80 the same owner as the directory, no matter who creates them, and set
81 the set-user-ID bit of newly-created subdirectories.
82 @item
83 @cindex set-group-ID
84 @cindex setgid
85 Set the process's effective group ID to that of the file upon execution
86 (called the @dfn{set-group-ID bit}, or sometimes the @dfn{setgid bit}).
87 For directories on most systems, give files created in the directory
88 the same group as the directory, no matter what group the user who
89 creates them is in, and set the set-group-ID bit of newly-created
90 subdirectories.
91 @item
92 @cindex sticky
93 @cindex swap space, saving text image in
94 @cindex text image, saving in swap space
95 @cindex restricted deletion flag
96 Prevent unprivileged users from removing or renaming a file in a directory
97 unless they own the file or the directory; this is called the
98 @dfn{restricted deletion flag} for the directory, and is commonly
99 found on world-writable directories like @file{/tmp}.
101 For regular files on some older systems, save the program's text image on the
102 swap device so it will load more quickly when run; this is called the
103 @dfn{sticky bit}.
104 @end enumerate
106 In addition to the file mode bits listed above, there may be file attributes
107 specific to the file system, e.g., access control lists (ACLs), whether a
108 file is compressed, whether a file can be modified (immutability), and whether
109 a file can be dumped.  These are usually set using programs
110 specific to the file system.  For example:
111 @c should probably say a lot more about ACLs... someday
113 @table @asis
114 @item ext2
115 On GNU and GNU/Linux the file attributes specific to
116 the ext2 file system are set using @command{chattr}.
118 @item FFS
119 On FreeBSD the file flags specific to the FFS
120 file system are set using @command{chflags}.
121 @end table
123 Even if a file's mode bits allow an operation on that file,
124 that operation may still fail, because:
126 @itemize
127 @item
128 the file-system-specific attributes or flags do not permit it; or
130 @item
131 the file system is mounted as read-only.
132 @end itemize
134 For example, if the immutable attribute is set on a file,
135 it cannot be modified, regardless of the fact that you
136 may have just run @code{chmod a+w FILE}.
138 @node Symbolic Modes
139 @section Symbolic Modes
141 @cindex symbolic modes
142 @dfn{Symbolic modes} represent changes to files' mode bits as
143 operations on single-character symbols.  They allow you to modify either
144 all or selected parts of files' mode bits, optionally based on
145 their previous values, and perhaps on the current @code{umask} as well
146 (@pxref{Umask and Protection}).
148 The format of symbolic modes is:
150 @example
151 @r{[}ugoa@dots{}@r{][}+-=@r{]}@var{perms}@dots{}@r{[},@dots{}@r{]}
152 @end example
154 @noindent
155 where @var{perms} is either zero or more letters from the set
156 @samp{rwxXst}, or a single letter from the set @samp{ugo}.
158 The following sections describe the operators and other details of
159 symbolic modes.
161 @menu
162 * Setting Permissions::          Basic operations on permissions.
163 * Copying Permissions::          Copying existing permissions.
164 * Changing Special Mode Bits::   Special mode bits.
165 * Conditional Executability::    Conditionally affecting executability.
166 * Multiple Changes::             Making multiple changes.
167 * Umask and Protection::              The effect of the umask.
168 @end menu
170 @node Setting Permissions
171 @subsection Setting Permissions
173 The basic symbolic operations on a file's permissions are adding,
174 removing, and setting the permission that certain users have to read,
175 write, and execute or search the file.  These operations have the following
176 format:
178 @example
179 @var{users} @var{operation} @var{permissions}
180 @end example
182 @noindent
183 The spaces between the three parts above are shown for readability only;
184 symbolic modes cannot contain spaces.
186 The @var{users} part tells which users' access to the file is changed.
187 It consists of one or more of the following letters (or it can be empty;
188 @pxref{Umask and Protection}, for a description of what happens then).  When
189 more than one of these letters is given, the order that they are in does
190 not matter.
192 @table @code
193 @item u
194 @cindex owner of file, permissions for
195 the user who owns the file;
196 @item g
197 @cindex group, permissions for
198 other users who are in the file's group;
199 @item o
200 @cindex other permissions
201 all other users;
202 @item a
203 all users; the same as @samp{ugo}.
204 @end table
206 The @var{operation} part tells how to change the affected users' access
207 to the file, and is one of the following symbols:
209 @table @code
210 @item +
211 @cindex adding permissions
212 to add the @var{permissions} to whatever permissions the @var{users}
213 already have for the file;
214 @item -
215 @cindex removing permissions
216 @cindex subtracting permissions
217 to remove the @var{permissions} from whatever permissions the
218 @var{users} already have for the file;
219 @item =
220 @cindex setting permissions
221 to make the @var{permissions} the only permissions that the @var{users}
222 have for the file.
223 @end table
225 The @var{permissions} part tells what kind of access to the file should
226 be changed; it is normally zero or more of the following letters.  As with the
227 @var{users} part, the order does not matter when more than one letter is
228 given.  Omitting the @var{permissions} part is useful only with the
229 @samp{=} operation, where it gives the specified @var{users} no access
230 at all to the file.
232 @table @code
233 @item r
234 @cindex read permission, symbolic
235 the permission the @var{users} have to read the file;
236 @item w
237 @cindex write permission, symbolic
238 the permission the @var{users} have to write to the file;
239 @item x
240 @cindex execute/search permission, symbolic
241 the permission the @var{users} have to execute the file,
242 or search it if it is a directory.
243 @end table
245 For example, to give everyone permission to read and write a regular file,
246 but not to execute it, use:
248 @example
249 a=rw
250 @end example
252 To remove write permission for all users other than the file's
253 owner, use:
255 @example
256 go-w
257 @end example
259 @noindent
260 The above command does not affect the access that the owner of
261 the file has to it, nor does it affect whether other users can
262 read or execute the file.
264 To give everyone except a file's owner no permission to do anything with
265 that file, use the mode below.  Other users could still remove the file,
266 if they have write permission on the directory it is in.
268 @example
270 @end example
272 @noindent
273 Another way to specify the same thing is:
275 @example
276 og-rwx
277 @end example
279 @node Copying Permissions
280 @subsection Copying Existing Permissions
282 @cindex copying existing permissions
283 @cindex permissions, copying existing
284 You can base a file's permissions on its existing permissions.  To do
285 this, instead of using a series of @samp{r}, @samp{w}, or @samp{x}
286 letters after the
287 operator, you use the letter @samp{u}, @samp{g}, or @samp{o}.  For
288 example, the mode
290 @example
292 @end example
294 @noindent
295 adds the permissions for users who are in a file's group to the
296 permissions that other users have for the file.  Thus, if the file
297 started out as mode 664 (@samp{rw-rw-r--}), the above mode would change
298 it to mode 666 (@samp{rw-rw-rw-}).  If the file had started out as mode
299 741 (@samp{rwxr----x}), the above mode would change it to mode 745
300 (@samp{rwxr--r-x}).  The @samp{-} and @samp{=} operations work
301 analogously.
303 @node Changing Special Mode Bits
304 @subsection Changing Special Mode Bits
306 @cindex changing special mode bits
307 In addition to changing a file's read, write, and execute/search permissions,
308 you can change its special mode bits.  @xref{Mode Structure}, for a
309 summary of these special mode bits.
311 To change the file mode bits to set the user ID on execution, use
312 @samp{u} in the @var{users} part of the symbolic mode and
313 @samp{s} in the @var{permissions} part.
315 To change the file mode bits to set the group ID on execution, use
316 @samp{g} in the @var{users} part of the symbolic mode and
317 @samp{s} in the @var{permissions} part.
319 To set both user and group ID on execution, omit the @var{users} part
320 of the symbolic mode (or use @samp{a}) and use @samp{s} in the
321 @var{permissions} part.
323 To change the file mode bits to set the restricted deletion flag or sticky bit,
324 omit the @var{users} part of the symbolic mode (or use @samp{a}) and use
325 @samp{t} in the @var{permissions} part.
327 For example, to set the set-user-ID mode bit of a program,
328 you can use the mode:
330 @example
332 @end example
334 To remove both set-user-ID and set-group-ID mode bits from
335 it, you can use the mode:
337 @example
339 @end example
341 To set the restricted deletion flag or sticky bit, you can use
342 the mode:
344 @example
346 @end example
348 The combination @samp{o+s} has no effect.  On GNU systems
349 the combinations @samp{u+t} and @samp{g+t} have no effect, and
350 @samp{o+t} acts like plain @samp{+t}.
352 The @samp{=} operator is not very useful with special mode bits.
353 For example, the mode:
355 @example
357 @end example
359 @noindent
360 does set the restricted deletion flag or sticky bit, but it also
361 removes all read, write, and execute/search permissions that users not in the
362 file's group might have had for it.
364 @xref{Directory Setuid and Setgid}, for additional rules concerning
365 set-user-ID and set-group-ID bits and directories.
367 @node Conditional Executability
368 @subsection Conditional Executability
370 @cindex conditional executability
371 There is one more special type of symbolic permission: if you use
372 @samp{X} instead of @samp{x}, execute/search permission is affected only if the
373 file is a directory or already had execute permission.
375 For example, this mode:
377 @example
379 @end example
381 @noindent
382 gives all users permission to search directories, or to execute files if
383 anyone could execute them before.
385 @node Multiple Changes
386 @subsection Making Multiple Changes
388 @cindex multiple changes to permissions
389 The format of symbolic modes is actually more complex than described
390 above (@pxref{Setting Permissions}).  It provides two ways to make
391 multiple changes to files' mode bits.
393 The first way is to specify multiple @var{operation} and
394 @var{permissions} parts after a @var{users} part in the symbolic mode.
396 For example, the mode:
398 @example
399 og+rX-w
400 @end example
402 @noindent
403 gives users other than the owner of the file read permission and, if
404 it is a directory or if someone already had execute permission
405 to it, gives them execute/search permission; and it also denies them write
406 permission to the file.  It does not affect the permission that the
407 owner of the file has for it.  The above mode is equivalent to
408 the two modes:
410 @example
411 og+rX
412 og-w
413 @end example
415 The second way to make multiple changes is to specify more than one
416 simple symbolic mode, separated by commas.  For example, the mode:
418 @example
419 a+r,go-w
420 @end example
422 @noindent
423 gives everyone permission to read the file and removes write
424 permission on it for all users except its owner.  Another example:
426 @example
427 u=rwx,g=rx,o=
428 @end example
430 @noindent
431 sets all of the permission bits for the file explicitly.  (It
432 gives users who are not in the file's group no permission at all for
433 it.)
435 The two methods can be combined.  The mode:
437 @example
438 a+r,g+x-w
439 @end example
441 @noindent
442 gives all users permission to read the file, and gives users who are in
443 the file's group permission to execute/search it as well, but not permission
444 to write to it.  The above mode could be written in several different
445 ways; another is:
447 @example
448 u+r,g+rx,o+r,g-w
449 @end example
451 @node Umask and Protection
452 @subsection The Umask and Protection
454 @cindex umask and modes
455 @cindex modes and umask
456 If the @var{users} part of a symbolic mode is omitted, it defaults to
457 @samp{a} (affect all users), except that any permissions that are
458 @emph{set} in the system variable @code{umask} are @emph{not affected}.
459 The value of @code{umask} can be set using the
460 @code{umask} command.  Its default value varies from system to system.
462 @cindex giving away permissions
463 Omitting the @var{users} part of a symbolic mode is generally not useful
464 with operations other than @samp{+}.  It is useful with @samp{+} because
465 it allows you to use @code{umask} as an easily customizable protection
466 against giving away more permission to files than you intended to.
468 As an example, if @code{umask} has the value 2, which removes write
469 permission for users who are not in the file's group, then the mode:
471 @example
473 @end example
475 @noindent
476 adds permission to write to the file to its owner and to other users who
477 are in the file's group, but @emph{not} to other users.  In contrast,
478 the mode:
480 @example
482 @end example
484 @noindent
485 ignores @code{umask}, and @emph{does} give write permission for
486 the file to all users.
488 @node Numeric Modes
489 @section Numeric Modes
491 @cindex numeric modes
492 @cindex file mode bits, numeric
493 @cindex octal numbers for file modes
494 As an
495 alternative to giving a symbolic mode, you can give an octal (base 8)
496 number that represents the mode.
497 This number is always interpreted in octal; you do not have to add a
498 leading @samp{0}, as you do in C.  Mode @samp{0055} is the same as
499 mode @samp{55}.  (However, modes of five digits or more, such as
500 @samp{00055}, are sometimes special.  @xref{Directory Setuid and Setgid}.)
502 A numeric mode is usually shorter than the corresponding symbolic
503 mode, but it is limited in that normally it cannot take into account the
504 previous file mode bits; it can only set them absolutely.
505 The set-user-ID and set-group-ID bits of directories are an exception
506 to this general limitation.  @xref{Directory Setuid and Setgid}.
507 Also, operator numeric modes can take previous file mode bits into
508 account.  @xref{Operator Numeric Modes}.
510 The permissions granted to the user,
511 to other users in the file's group,
512 and to other users not in the file's group each require three
513 bits, which are represented as one octal digit.  The three special
514 mode bits also require one bit each, and they are as a group
515 represented as another octal digit.  Here is how the bits are arranged,
516 starting with the lowest valued bit:
518 @example
519 Value in  Corresponding
520 Mode      Mode Bit
522           Other users not in the file's group:
523    1      Execute/search
524    2      Write
525    4      Read
527           Other users in the file's group:
528   10      Execute/search
529   20      Write
530   40      Read
532           The file's owner:
533  100      Execute/search
534  200      Write
535  400      Read
537           Special mode bits:
538 1000      Restricted deletion flag or sticky bit
539 2000      Set group ID on execution
540 4000      Set user ID on execution
541 @end example
543 For example, numeric mode @samp{4755} corresponds to symbolic mode
544 @samp{u=rwxs,go=rx}, and numeric mode @samp{664} corresponds to symbolic mode
545 @samp{ug=rw,o=r}.  Numeric mode @samp{0} corresponds to symbolic mode
546 @samp{a=}.
548 @node Operator Numeric Modes
549 @section Operator Numeric Modes
551 An operator numeric mode is a numeric mode that is prefixed by a
552 @samp{-}, @samp{+}, or @samp{=} operator, which has the same
553 interpretation as in symbolic modes.  For example, @samp{+440} enables
554 read permission for the file's owner and group, @samp{-1} disables
555 execute permission for other users, and @samp{=600} clears all
556 permissions except for enabling read-write permissions for the file's
557 owner.  Operator numeric modes can be combined with symbolic modes by
558 separating them with a comma; for example, @samp{=0,u+r} clears all
559 permissions except for enabling read permission for the file's owner.
561 The commands @samp{chmod =755 @var{dir}} and @samp{chmod 755
562 @var{dir}} differ in that the former clears the directory @var{dir}'s
563 setuid and setgid bits, whereas the latter preserves them.
564 @xref{Directory Setuid and Setgid}.
566 Operator numeric modes are a GNU extension.
568 @node Directory Setuid and Setgid
569 @section Directories and the Set-User-ID and Set-Group-ID Bits
571 On most systems, if a directory's set-group-ID bit is set, newly
572 created subfiles inherit the same group as the directory, and newly
573 created subdirectories inherit the set-group-ID bit of the parent
574 directory.  On a few systems, a directory's set-user-ID bit has a
575 similar effect on the ownership of new subfiles and the set-user-ID
576 bits of new subdirectories.  These mechanisms let users share files
577 more easily, by lessening the need to use @command{chmod} or
578 @command{chown} to share new files.
580 These convenience mechanisms rely on the set-user-ID and set-group-ID
581 bits of directories.  If commands like @command{chmod} and
582 @command{mkdir} routinely cleared these bits on directories, the
583 mechanisms would be less convenient and it would be harder to share
584 files.  Therefore, a command like @command{chmod} does not affect the
585 set-user-ID or set-group-ID bits of a directory unless the user
586 specifically mentions them in a symbolic mode, or uses an operator
587 numeric mode such as @samp{=755}, or sets them in a numeric mode, or
588 clears them in a numeric mode that has five or more octal digits.
589 For example, on systems that support
590 set-group-ID inheritance:
592 @example
593 # These commands leave the set-user-ID and
594 # set-group-ID bits of the subdirectories alone,
595 # so that they retain their default values.
596 mkdir A B C
597 chmod 755 A
598 chmod 0755 B
599 chmod u=rwx,go=rx C
600 mkdir -m 755 D
601 mkdir -m 0755 E
602 mkdir -m u=rwx,go=rx F
603 @end example
605 If you want to try to set these bits, you must mention them
606 explicitly in the symbolic or numeric modes, e.g.:
608 @example
609 # These commands try to set the set-user-ID
610 # and set-group-ID bits of the subdirectories.
611 mkdir G
612 chmod 6755 G
613 chmod +6000 G
614 chmod u=rwx,go=rx,a+s G
615 mkdir -m 6755 H
616 mkdir -m +6000 I
617 mkdir -m u=rwx,go=rx,a+s J
618 @end example
620 If you want to try to clear these bits, you must mention them
621 explicitly in a symbolic mode, or use an operator numeric mode, or
622 specify a numeric mode with five or more octal digits, e.g.:
624 @example
625 # These commands try to clear the set-user-ID
626 # and set-group-ID bits of the directory D.
627 chmod a-s D
628 chmod -6000 D
629 chmod =755 D
630 chmod 00755 D
631 @end example
633 This behavior is a GNU extension.  Portable scripts should
634 not rely on requests to set or clear these bits on directories, as
635 POSIX allows implementations to ignore these requests.
636 The GNU behavior with numeric modes of four or fewer digits
637 is intended for scripts portable to systems that preserve these bits;
638 the behavior with numeric modes of five or more digits is for scripts
639 portable to systems that do not preserve the bits.