source: trunk/essentials/sys-apps/findutils/doc/perm.texi

Last change on this file was 3170, checked in by bird, 18 years ago

findutils 4.3.2

File size: 18.4 KB
Line 
1Each file has a set of @dfn{file mode bits} that control the kinds of
2access that users have to that file. They can be represented either in
3symbolic form or as an octal number.
4
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
11
12@node Mode Structure
13@section Structure of File Mode Bits
14
15The file mode bits have two parts: the @dfn{file permission bits},
16which control ordinary access to the file, and @dfn{special mode
17bits}, which affect only some files.
18
19There are three kinds of permissions that a user can have for a file:
20
21@enumerate
22@item
23@cindex read permission
24permission to read the file. For directories, this means permission to
25list the contents of the directory.
26@item
27@cindex write permission
28permission to write to (change) the file. For directories, this means
29permission to create and remove files in the directory.
30@item
31@cindex execute/search permission
32permission to execute the file (run it as a program). For directories,
33this means permission to access files in the directory.
34@end enumerate
35
36There are three categories of users who may have different permissions
37to perform any of the above operations on a file:
38
39@enumerate
40@item
41the file's owner;
42@item
43other users who are in the file's group;
44@item
45everyone else.
46@end enumerate
47
48@cindex owner, default
49@cindex group owner, default
50Files are given an owner and group when they are created. Usually the
51owner is the current user and the group is the group of the directory
52the file is in, but this varies with the operating system, the
53file system the file is created on, and the way the file is created. You
54can change the owner and group of a file by using the @command{chown} and
55@command{chgrp} commands.
56
57In addition to the three sets of three permissions listed above, the
58file mode bits have three special components, which affect only
59executable files (programs) and, on most systems, directories:
60
61@enumerate
62@item
63@cindex set-user-ID
64@cindex setuid
65Set 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}).
67For directories on a few systems, give files created in the directory
68the same owner as the directory, no matter who creates them, and set
69the set-user-ID bit of newly-created subdirectories.
70@item
71@cindex set-group-ID
72@cindex setgid
73Set 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}).
75For directories on most systems, give files created in the directory
76the same group as the directory, no matter what group the user who
77creates them is in, and set the set-group-ID bit of newly-created
78subdirectories.
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
84Prevent unprivileged users from removing or renaming a file in a directory
85unless they own the file or the directory; this is called the
86@dfn{restricted deletion flag} for the directory, and is commonly
87found on world-writable directories like @file{/tmp}.
88
89For regular files on some older systems, save the program's text image on the
90swap device so it will load more quickly when run; this is called the
91@dfn{sticky bit}.
92@end enumerate
93
94In addition to the file mode bits listed above, there may be file attributes
95specific to the file system, e.g., access control lists (ACLs), whether a
96file is compressed, whether a file can be modified (immutability), and whether
97a file can be dumped. These are usually set using programs
98specific to the file system. For example:
99@c should probably say a lot more about ACLs... someday
100
101@table @asis
102@item ext2
103On @acronym{GNU} and @acronym{GNU}/Linux the file attributes specific to
104the ext2 file system are set using @command{chattr}.
105
106@item FFS
107On FreeBSD the file flags specific to the FFS
108file system are set using @command{chflags}.
109@end table
110
111Even if a file's mode bits allow an operation on that file,
112that operation may still fail, because:
113
114@itemize
115@item
116the file-system-specific attributes or flags do not permit it; or
117
118@item
119the file system is mounted as read-only.
120@end itemize
121
122For example, if the immutable attribute is set on a file,
123it cannot be modified, regardless of the fact that you
124may have just run @code{chmod a+w FILE}.
125
126@node Symbolic Modes
127@section Symbolic Modes
128
129@cindex symbolic modes
130@dfn{Symbolic modes} represent changes to files' mode bits as
131operations on single-character symbols. They allow you to modify either
132all or selected parts of files' mode bits, optionally based on
133their previous values, and perhaps on the current @code{umask} as well
134(@pxref{Umask and Protection}).
135
136The format of symbolic modes is:
137
138@example
139@r{[}ugoa@dots{}@r{][}+-=@r{]}@var{perms}@dots{}@r{[},@dots{}@r{]}
140@end example
141
142@noindent
143where @var{perms} is either zero or more letters from the set
144@samp{rwxXst}, or a single letter from the set @samp{ugo}.
145
146The following sections describe the operators and other details of
147symbolic modes.
148
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
157
158@node Setting Permissions
159@subsection Setting Permissions
160
161The basic symbolic operations on a file's permissions are adding,
162removing, and setting the permission that certain users have to read,
163write, and execute or search the file. These operations have the following
164format:
165
166@example
167@var{users} @var{operation} @var{permissions}
168@end example
169
170@noindent
171The spaces between the three parts above are shown for readability only;
172symbolic modes cannot contain spaces.
173
174The @var{users} part tells which users' access to the file is changed.
175It 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
177more than one of these letters is given, the order that they are in does
178not matter.
179
180@table @code
181@item u
182@cindex owner of file, permissions for
183the user who owns the file;
184@item g
185@cindex group, permissions for
186other users who are in the file's group;
187@item o
188@cindex other permissions
189all other users;
190@item a
191all users; the same as @samp{ugo}.
192@end table
193
194The @var{operation} part tells how to change the affected users' access
195to the file, and is one of the following symbols:
196
197@table @code
198@item +
199@cindex adding permissions
200to add the @var{permissions} to whatever permissions the @var{users}
201already have for the file;
202@item -
203@cindex removing permissions
204@cindex subtracting permissions
205to remove the @var{permissions} from whatever permissions the
206@var{users} already have for the file;
207@item =
208@cindex setting permissions
209to make the @var{permissions} the only permissions that the @var{users}
210have for the file.
211@end table
212
213The @var{permissions} part tells what kind of access to the file should
214be 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
216given. Omitting the @var{permissions} part is useful only with the
217@samp{=} operation, where it gives the specified @var{users} no access
218at all to the file.
219
220@table @code
221@item r
222@cindex read permission, symbolic
223the permission the @var{users} have to read the file;
224@item w
225@cindex write permission, symbolic
226the permission the @var{users} have to write to the file;
227@item x
228@cindex execute/search permission, symbolic
229the permission the @var{users} have to execute the file,
230or search it if it is a directory.
231@end table
232
233For example, to give everyone permission to read and write a regular file,
234but not to execute it, use:
235
236@example
237a=rw
238@end example
239
240To remove write permission for all users other than the file's
241owner, use:
242
243@example
244go-w
245@end example
246
247@noindent
248The above command does not affect the access that the owner of
249the file has to it, nor does it affect whether other users can
250read or execute the file.
251
252To give everyone except a file's owner no permission to do anything with
253that file, use the mode below. Other users could still remove the file,
254if they have write permission on the directory it is in.
255
256@example
257go=
258@end example
259
260@noindent
261Another way to specify the same thing is:
262
263@example
264og-rwx
265@end example
266
267@node Copying Permissions
268@subsection Copying Existing Permissions
269
270@cindex copying existing permissions
271@cindex permissions, copying existing
272You can base a file's permissions on its existing permissions. To do
273this, instead of using a series of @samp{r}, @samp{w}, or @samp{x}
274letters after the
275operator, you use the letter @samp{u}, @samp{g}, or @samp{o}. For
276example, the mode
277
278@example
279o+g
280@end example
281
282@noindent
283adds the permissions for users who are in a file's group to the
284permissions that other users have for the file. Thus, if the file
285started out as mode 664 (@samp{rw-rw-r--}), the above mode would change
286it to mode 666 (@samp{rw-rw-rw-}). If the file had started out as mode
287741 (@samp{rwxr----x}), the above mode would change it to mode 745
288(@samp{rwxr--r-x}). The @samp{-} and @samp{=} operations work
289analogously.
290
291@node Changing Special Mode Bits
292@subsection Changing Special Mode Bits
293
294@cindex changing special mode bits
295In addition to changing a file's read, write, and execute/search permissions,
296you can change its special mode bits. @xref{Mode Structure}, for a
297summary of these special mode bits.
298
299To 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.
302
303To 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.
306
307To set both user and group ID on execution, omit the @var{users} part
308of the symbolic mode (or use @samp{a}) and use @samp{s} in the
309@var{permissions} part.
310
311To change the file mode bits to set the restricted deletion flag or sticky bit,
312omit the @var{users} part of the symbolic mode (or use @samp{a}) and use
313@samp{t} in the @var{permissions} part.
314
315For example, to set the set-user-ID mode bit of a program,
316you can use the mode:
317
318@example
319u+s
320@end example
321
322To remove both set-user-ID and set-group-ID mode bits from
323it, you can use the mode:
324
325@example
326a-s
327@end example
328
329To set the restricted deletion flag or sticky bit, you can use
330the mode:
331
332@example
333+t
334@end example
335
336The combination @samp{o+s} has no effect. On @acronym{GNU} systems
337the combinations @samp{u+t} and @samp{g+t} have no effect, and
338@samp{o+t} acts like plain @samp{+t}.
339
340The @samp{=} operator is not very useful with special mode bits.
341For example, the mode:
342
343@example
344o=t
345@end example
346
347@noindent
348does set the restricted deletion flag or sticky bit, but it also
349removes all read, write, and execute/search permissions that users not in the
350file's group might have had for it.
351
352@xref{Directory Setuid and Setgid}, for additional rules concerning
353set-user-ID and set-group-ID bits and directories.
354
355@node Conditional Executability
356@subsection Conditional Executability
357
358@cindex conditional executability
359There 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
361file is a directory or already had execute permission.
362
363For example, this mode:
364
365@example
366a+X
367@end example
368
369@noindent
370gives all users permission to search directories, or to execute files if
371anyone could execute them before.
372
373@node Multiple Changes
374@subsection Making Multiple Changes
375
376@cindex multiple changes to permissions
377The format of symbolic modes is actually more complex than described
378above (@pxref{Setting Permissions}). It provides two ways to make
379multiple changes to files' mode bits.
380
381The first way is to specify multiple @var{operation} and
382@var{permissions} parts after a @var{users} part in the symbolic mode.
383
384For example, the mode:
385
386@example
387og+rX-w
388@end example
389
390@noindent
391gives users other than the owner of the file read permission and, if
392it is a directory or if someone already had execute permission
393to it, gives them execute/search permission; and it also denies them write
394permission to the file. It does not affect the permission that the
395owner of the file has for it. The above mode is equivalent to
396the two modes:
397
398@example
399og+rX
400og-w
401@end example
402
403The second way to make multiple changes is to specify more than one
404simple symbolic mode, separated by commas. For example, the mode:
405
406@example
407a+r,go-w
408@end example
409
410@noindent
411gives everyone permission to read the file and removes write
412permission on it for all users except its owner. Another example:
413
414@example
415u=rwx,g=rx,o=
416@end example
417
418@noindent
419sets all of the permission bits for the file explicitly. (It
420gives users who are not in the file's group no permission at all for
421it.)
422
423The two methods can be combined. The mode:
424
425@example
426a+r,g+x-w
427@end example
428
429@noindent
430gives all users permission to read the file, and gives users who are in
431the file's group permission to execute/search it as well, but not permission
432to write to it. The above mode could be written in several different
433ways; another is:
434
435@example
436u+r,g+rx,o+r,g-w
437@end example
438
439@node Umask and Protection
440@subsection The Umask and Protection
441
442@cindex umask and modes
443@cindex modes and umask
444If 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}.
447The value of @code{umask} can be set using the
448@code{umask} command. Its default value varies from system to system.
449
450@cindex giving away permissions
451Omitting the @var{users} part of a symbolic mode is generally not useful
452with operations other than @samp{+}. It is useful with @samp{+} because
453it allows you to use @code{umask} as an easily customizable protection
454against giving away more permission to files than you intended to.
455
456As an example, if @code{umask} has the value 2, which removes write
457permission for users who are not in the file's group, then the mode:
458
459@example
460+w
461@end example
462
463@noindent
464adds permission to write to the file to its owner and to other users who
465are in the file's group, but @emph{not} to other users. In contrast,
466the mode:
467
468@example
469a+w
470@end example
471
472@noindent
473ignores @code{umask}, and @emph{does} give write permission for
474the file to all users.
475
476@node Numeric Modes
477@section Numeric Modes
478
479@cindex numeric modes
480@cindex file mode bits, numeric
481@cindex octal numbers for file modes
482As an
483alternative to giving a symbolic mode, you can give an octal (base 8)
484number that represents the mode.
485This number is always interpreted in octal; you do not have to add a
486leading @samp{0}, as you do in C. Mode @samp{0055} is the same as
487mode @samp{55}.
488
489A numeric mode is usually shorter than the corresponding symbolic
490mode, but it is limited in that normally it cannot take into account the
491previous file mode bits; it can only set them absolutely.
492(As discussed in the next section, the set-user-ID and set-group-ID
493bits of directories are an exception to this general limitation.)
494
495The permissions granted to the user,
496to other users in the file's group,
497and to other users not in the file's group each require three
498bits, which are represented as one octal digit. The three special
499mode bits also require one bit each, and they are as a group
500represented as another octal digit. Here is how the bits are arranged,
501starting with the lowest valued bit:
502
503@example
504Value in Corresponding
505Mode Mode Bit
506
507 Other users not in the file's group:
508 1 Execute/search
509 2 Write
510 4 Read
511
512 Other users in the file's group:
513 10 Execute/search
514 20 Write
515 40 Read
516
517 The file's owner:
518 100 Execute/search
519 200 Write
520 400 Read
521
522 Special mode bits:
5231000 Restricted deletion flag or sticky bit
5242000 Set group ID on execution
5254000 Set user ID on execution
526@end example
527
528For 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=}.
532
533@node Directory Setuid and Setgid
534@section Directories and the Set-User-ID and Set-Group-ID Bits
535
536On most systems, if a directory's set-group-ID bit is set, newly
537created subfiles inherit the same group as the directory, and newly
538created subdirectories inherit the set-group-ID bit of the parent
539directory. On a few systems, a directory's set-user-ID bit has a
540similar effect on the ownership of new subfiles and the set-user-ID
541bits of new subdirectories. These mechanisms let users share files
542more easily, by lessening the need to use @command{chmod} or
543@command{chown} to share new files.
544
545These convenience mechanisms rely on the set-user-ID and set-group-ID
546bits of directories. If commands like @command{chmod} and
547@command{mkdir} routinely cleared these bits on directories, the
548mechanisms would be less convenient and it would be harder to share
549files. Therefore, a command like @command{chmod} does not affect the
550set-user-ID or set-group-ID bits of a directory unless the user
551specifically mentions them in a symbolic mode, or sets them in
552a numeric mode. For example, on systems that support
553set-group-ID inheritance:
554
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.
559mkdir A B C
560chmod 755 A
561chmod 0755 B
562chmod u=rwx,go=rx C
563mkdir -m 755 D
564mkdir -m 0755 E
565mkdir -m u=rwx,go=rx F
566@end example
567
568If you want to try to set these bits, you must mention them
569explicitly in the symbolic or numeric modes, e.g.:
570
571@example
572# These commands try to set the set-user-ID
573# and set-group-ID bits of the subdirectories.
574mkdir G H
575chmod 6755 G
576chmod u=rwx,go=rx,a+s H
577mkdir -m 6755 I
578mkdir -m u=rwx,go=rx,a+s J
579@end example
580
581If you want to try to clear these bits, you must mention them
582explicitly in a symbolic mode, e.g.:
583
584@example
585# This command tries to clear the set-user-ID
586# and set-group-ID bits of the directory D.
587chmod a-s D
588@end example
589
590This behavior is a @acronym{GNU} extension. Portable scripts should
591not rely on requests to set or clear these bits on directories, as
592@acronym{POSIX} allows implementations to ignore these requests.
Note: See TracBrowser for help on using the repository browser.