OSDN Git Service

LDP: Update original to LDP v3.68
[linuxjm/LDP_man-pages.git] / original / man2 / chown.2
1 .\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
2 .\" and Copyright (c) 1998 Andries Brouwer (aeb@cwi.nl)
3 .\" and Copyright (c) 2006, 2007, 2008, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
4 .\"
5 .\" %%%LICENSE_START(VERBATIM)
6 .\" Permission is granted to make and distribute verbatim copies of this
7 .\" manual provided the copyright notice and this permission notice are
8 .\" preserved on all copies.
9 .\"
10 .\" Permission is granted to copy and distribute modified versions of this
11 .\" manual under the conditions for verbatim copying, provided that the
12 .\" entire resulting derived work is distributed under the terms of a
13 .\" permission notice identical to this one.
14 .\"
15 .\" Since the Linux kernel and libraries are constantly changing, this
16 .\" manual page may be incorrect or out-of-date.  The author(s) assume no
17 .\" responsibility for errors or omissions, or for damages resulting from
18 .\" the use of the information contained herein.  The author(s) may not
19 .\" have taken the same level of care in the production of this manual,
20 .\" which is licensed free of charge, as they might when working
21 .\" professionally.
22 .\"
23 .\" Formatted or processed versions of this manual, if unaccompanied by
24 .\" the source, must acknowledge the copyright and authors of this work.
25 .\" %%%LICENSE_END
26 .\"
27 .\" Modified by Michael Haardt <michael@moria.de>
28 .\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
29 .\" Modified 1996-07-09 by Andries Brouwer <aeb@cwi.nl>
30 .\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
31 .\" Modified 1997-05-18 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
32 .\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
33 .\" 2007-07-08, mtk, added an example program; updated SYNOPSIS
34 .\" 2008-05-08, mtk, Describe rules governing ownership of new files
35 .\"     (bsdgroups versus sysvgroups, and the effect of the parent
36 .\"     directory's set-group-ID permission bit).
37 .\"
38 .TH CHOWN 2 2014-05-21 "Linux" "Linux Programmer's Manual"
39 .SH NAME
40 chown, fchown, lchown, fchownat \- change ownership of a file
41 .SH SYNOPSIS
42 .nf
43 .B #include <unistd.h>
44 .sp
45 .BI "int chown(const char *" pathname ", uid_t " owner ", gid_t " group );
46 .br
47 .BI "int fchown(int " fd ", uid_t " owner ", gid_t " group );
48 .br
49 .BI "int lchown(const char *" pathname ", uid_t " owner ", gid_t " group );
50 .sp
51 .BR "#include <fcntl.h>           " "/* Definition of AT_* constants */"
52 .B #include <unistd.h>
53 .sp
54 .BI "int fchownat(int " dirfd ", const char *" pathname ,
55 .BI "             uid_t " owner ", gid_t " group ", int " flags );
56 .fi
57 .sp
58 .sp
59 .in -4n
60 Feature Test Macro Requirements for glibc (see
61 .BR feature_test_macros (7)):
62 .in
63 .sp
64 .BR fchown (),
65 .BR lchown ():
66 .PD 0
67 .ad l
68 .RS 4
69 _BSD_SOURCE || _XOPEN_SOURCE\ >=\ 500 ||
70 _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
71 .br
72 || /* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
73 .RE
74 .sp
75 .BR fchownat ():
76 .PD 0
77 .ad l
78 .RS 4
79 .TP 4
80 Since glibc 2.10:
81 _XOPEN_SOURCE\ >=\ 700 || _POSIX_C_SOURCE\ >=\ 200809L
82 .TP
83 Before glibc 2.10:
84 _ATFILE_SOURCE
85 .RE
86 .ad
87 .PD
88 .SH DESCRIPTION
89 These system calls change the owner and group of a file.
90 The
91 .BR chown (),
92 .BR fchown (),
93 and
94 .BR lchown ()
95 system calls differ only in how the file is specified:
96 .IP * 2
97 .BR chown ()
98 changes the ownership of the file specified by
99 .IR pathname ,
100 which is dereferenced if it is a symbolic link.
101 .IP *
102 .BR fchown ()
103 changes the ownership of the file referred to by the open file descriptor
104 .IR fd .
105 .IP *
106 .BR lchown ()
107 is like
108 .BR chown (),
109 but does not dereference symbolic links.
110 .PP
111 Only a privileged process (Linux: one with the
112 .B CAP_CHOWN
113 capability) may change the owner of a file.
114 The owner of a file may change the group of the file
115 to any group of which that owner is a member.
116 A privileged process (Linux: with
117 .BR CAP_CHOWN )
118 may change the group arbitrarily.
119
120 If the
121 .I owner
122 or
123 .I group
124 is specified as \-1, then that ID is not changed.
125
126 When the owner or group of an executable file are
127 changed by an unprivileged user the
128 .B S_ISUID
129 and
130 .B S_ISGID
131 mode bits are cleared.
132 POSIX does not specify whether
133 this also should happen when root does the
134 .BR chown ();
135 the Linux behavior depends on the kernel version.
136 .\" In Linux 2.0 kernels, superuser was like everyone else
137 .\" In 2.2, up to 2.2.12, these bits were not cleared for superuser.
138 .\" Since 2.2.13, superuser is once more like everyone else.
139 In case of a non-group-executable file (i.e., one for which the
140 .B S_IXGRP
141 bit is not set) the
142 .B S_ISGID
143 bit indicates mandatory locking, and is not cleared by a
144 .BR chown ().
145 .SS fchownat()
146 The
147 .BR fchownat ()
148 system call operates in exactly the same way as
149 .BR chown (),
150 except for the differences described here.
151
152 If the pathname given in
153 .I pathname
154 is relative, then it is interpreted relative to the directory
155 referred to by the file descriptor
156 .I dirfd
157 (rather than relative to the current working directory of
158 the calling process, as is done by
159 .BR chown ()
160 for a relative pathname).
161
162 If
163 .I pathname
164 is relative and
165 .I dirfd
166 is the special value
167 .BR AT_FDCWD ,
168 then
169 .I pathname
170 is interpreted relative to the current working
171 directory of the calling process (like
172 .BR chown ()).
173
174 If
175 .I pathname
176 is absolute, then
177 .I dirfd
178 is ignored.
179
180 The
181 .I flags
182 argument is a bit mask created by ORing together
183 0 or more of the following values;
184 .TP
185 .BR AT_EMPTY_PATH " (since Linux 2.6.39)"
186 .\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
187 If
188 .I pathname
189 is an empty string, operate on the file referred to by
190 .IR dirfd
191 (which may have been obtained using the
192 .BR open (2)
193 .B O_PATH
194 flag).
195 In this case,
196 .I dirfd
197 can refer to any type of file, not just a directory.
198 If
199 .I dirfd
200 is
201 .BR AT_FDCWD ,
202 the call operates on the current working directory.
203 This flag is Linux-specific; define
204 .B _GNU_SOURCE
205 .\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
206 to obtain its definition.
207
208 .TP
209 .B AT_SYMLINK_NOFOLLOW
210 If
211 .I pathname
212 is a symbolic link, do not dereference it:
213 instead operate on the link itself, like
214 .BR lchown ().
215 (By default,
216 .BR fchownat ()
217 dereferences symbolic links, like
218 .BR chown ().)
219 .PP
220 See
221 .BR openat (2)
222 for an explanation of the need for
223 .BR fchownat ().
224 .SH RETURN VALUE
225 On success, zero is returned.
226 On error, \-1 is returned, and
227 .I errno
228 is set appropriately.
229 .SH ERRORS
230 Depending on the filesystem,
231 errors other than those listed below can be returned.
232
233 The more general errors for
234 .BR chown ()
235 are listed below.
236 .TP
237 .B EACCES
238 Search permission is denied on a component of the path prefix.
239 (See also
240 .BR path_resolution (7).)
241 .TP
242 .B EFAULT
243 .I pathname
244 points outside your accessible address space.
245 .TP
246 .B ELOOP
247 Too many symbolic links were encountered in resolving
248 .IR pathname .
249 .TP
250 .B ENAMETOOLONG
251 .I pathname
252 is too long.
253 .TP
254 .B ENOENT
255 The file does not exist.
256 .TP
257 .B ENOMEM
258 Insufficient kernel memory was available.
259 .TP
260 .B ENOTDIR
261 A component of the path prefix is not a directory.
262 .TP
263 .B EPERM
264 The calling process did not have the required permissions
265 (see above) to change owner and/or group.
266 .TP
267 .B EROFS
268 The named file resides on a read-only filesystem.
269 .PP
270 The general errors for
271 .BR fchown ()
272 are listed below:
273 .TP
274 .B EBADF
275 The descriptor is not valid.
276 .TP
277 .B EIO
278 A low-level I/O error occurred while modifying the inode.
279 .TP
280 .B ENOENT
281 See above.
282 .TP
283 .B EPERM
284 See above.
285 .TP
286 .B EROFS
287 See above.
288 .PP
289 The same errors that occur for
290 .BR chown ()
291 can also occur for
292 .BR fchownat ().
293 The following additional errors can occur for
294 .BR fchownat ():
295 .TP
296 .B EBADF
297 .I dirfd
298 is not a valid file descriptor.
299 .TP
300 .B EINVAL
301 Invalid flag specified in
302 .IR flags .
303 .TP
304 .B ENOTDIR
305 .I pathname
306 is relative and
307 .I dirfd
308 is a file descriptor referring to a file other than a directory.
309 .SH VERSIONS
310 .BR fchownat ()
311 was added to Linux in kernel 2.6.16;
312 library support was added to glibc in version 2.4.
313 .SH CONFORMING TO
314 .BR chown (),
315 .BR fchown (),
316 .BR lchown ():
317 4.4BSD, SVr4, POSIX.1-2001, POSIX.1-2008.
318
319 The 4.4BSD version can be
320 used only by the superuser (that is, ordinary users cannot give away files).
321 .\" chown():
322 .\" SVr4 documents EINVAL, EINTR, ENOLINK and EMULTIHOP returns, but no
323 .\" ENOMEM.  POSIX.1 does not document ENOMEM or ELOOP error conditions.
324 .\" fchown():
325 .\" SVr4 documents additional EINVAL, EIO, EINTR, and ENOLINK
326 .\" error conditions.
327
328 .BR fchownat ():
329 POSIX.1-2008.
330 .SH NOTES
331 .SS Ownership of new files
332 When a new file is created (by, for example,
333 .BR open (2)
334 or
335 .BR mkdir (2)),
336 its owner is made the same as the filesystem user ID of the
337 creating process.
338 The group of the file depends on a range of factors,
339 including the type of filesystem,
340 the options used to mount the filesystem,
341 and whether or not the set-group-ID permission bit is enabled
342 on the parent directory.
343 If the filesystem supports the
344 .I "\-o\ grpid"
345 (or, synonymously
346 .IR "\-o\ bsdgroups" )
347 and
348 .I "\-o\ nogrpid"
349 (or, synonymously
350 .IR "\-o\ sysvgroups" )
351 .BR mount (8)
352 options, then the rules are as follows:
353 .IP * 2
354 If the filesystem is mounted with
355 .IR "\-o\ grpid" ,
356 then the group of a new file is made
357 the same as that of the parent directory.
358 .IP *
359 If the filesystem is mounted with
360 .IR "\-o\ nogrpid"
361 and the set-group-ID bit is disabled on the parent directory,
362 then the group of a new file is made the same as the
363 process's filesystem GID.
364 .IP *
365 If the filesystem is mounted with
366 .IR "\-o\ nogrpid"
367 and the set-group-ID bit is enabled on the parent directory,
368 then the group of a new file is made
369 the same as that of the parent directory.
370 .PP
371 As at Linux 2.6.25,
372 the
373 .IR "\-o\ grpid"
374 and
375 .IR "\-o\ nogrpid"
376 mount options are supported by ext2, ext3, ext4, and XFS.
377 Filesystems that don't support these mount options follow the
378 .IR "\-o\ nogrpid"
379 rules.
380 .SS NFS
381 The
382 .BR chown ()
383 semantics are deliberately violated on NFS filesystems
384 which have UID mapping enabled.
385 Additionally, the semantics of all system
386 calls which access the file contents are violated, because
387 .BR chown ()
388 may cause immediate access revocation on already open files.
389 Client side
390 caching may lead to a delay between the time where ownership have
391 been changed to allow access for a user and the time where the file can
392 actually be accessed by the user on other clients.
393 .SS Historical details
394 The original Linux
395 .BR chown (),
396 .BR fchown (),
397 and
398 .BR lchown ()
399 system calls supported only 16-bit user and group IDs.
400 Subsequently, Linux 2.4 added
401 .BR chown32 (),
402 .BR fchown32 (),
403 and
404 .BR lchown32 (),
405 supporting 32-bit IDs.
406 The glibc
407 .BR chown (),
408 .BR fchown (),
409 and
410 .BR lchown ()
411 wrapper functions transparently deal with the variations across kernel versions.
412
413 In versions of Linux prior to 2.1.81 (and distinct from 2.1.46),
414 .BR chown ()
415 did not follow symbolic links.
416 Since Linux 2.1.81,
417 .BR chown ()
418 does follow symbolic links, and there is a new system call
419 .BR lchown ()
420 that does not follow symbolic links.
421 Since Linux 2.1.86, this new call (that has the same semantics
422 as the old
423 .BR chown ())
424 has got the same syscall number, and
425 .BR chown ()
426 got the newly introduced number.
427 .SH EXAMPLE
428 .PP
429 The following program changes the ownership of the file named in
430 its second command-line argument to the value specified in its
431 first command-line argument.
432 The new owner can be specified either as a numeric user ID,
433 or as a username (which is converted to a user ID by using
434 .BR getpwnam (3)
435 to perform a lookup in the system password file).
436 .SS Program source
437 .nf
438 #include <pwd.h>
439 #include <stdio.h>
440 #include <stdlib.h>
441 #include <unistd.h>
442
443 int
444 main(int argc, char *argv[])
445 {
446     uid_t uid;
447     struct passwd *pwd;
448     char *endptr;
449
450     if (argc != 3 || argv[1][0] == \(aq\\0\(aq) {
451         fprintf(stderr, "%s <owner> <file>\\n", argv[0]);
452         exit(EXIT_FAILURE);
453     }
454
455     uid = strtol(argv[1], &endptr, 10);  /* Allow a numeric string */
456
457     if (*endptr != \(aq\\0\(aq) {         /* Was not pure numeric string */
458         pwd = getpwnam(argv[1]);   /* Try getting UID for username */
459         if (pwd == NULL) {
460             perror("getpwnam");
461             exit(EXIT_FAILURE);
462         }
463
464         uid = pwd\->pw_uid;
465     }
466
467     if (chown(argv[2], uid, \-1) == \-1) {
468         perror("chown");
469         exit(EXIT_FAILURE);
470     }
471
472     exit(EXIT_SUCCESS);
473 }
474 .fi
475 .SH SEE ALSO
476 .BR chmod (2),
477 .BR flock (2),
478 .BR path_resolution (7),
479 .BR symlink (7)
480 .SH COLOPHON
481 This page is part of release 3.68 of the Linux
482 .I man-pages
483 project.
484 A description of the project,
485 information about reporting bugs,
486 and the latest version of this page,
487 can be found at
488 \%http://www.kernel.org/doc/man\-pages/.