1CHOWN(2)                   Linux Programmer's Manual                  CHOWN(2)
2
3
4

NAME

6       chown, fchown, lchown - change ownership of a file
7

SYNOPSIS

9       #include <unistd.h>
10
11       int chown(const char *path, uid_t owner, gid_t group);
12       int fchown(int fd, uid_t owner, gid_t group);
13       int lchown(const char *path, uid_t owner, gid_t group);
14
15   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
16
17       fchown(), lchown():
18           _BSD_SOURCE || _XOPEN_SOURCE >= 500 ||
19           _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
20           || /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
21

DESCRIPTION

23       These system calls change the owner and group of a file.   They  differ
24       only in how the file is specified:
25
26       * chown() changes the ownership of the file specified by path, which is
27         dereferenced if it is a symbolic link.
28
29       * fchown() changes the ownership of the file referred to  by  the  open
30         file descriptor fd.
31
32       * lchown() is like chown(), but does not dereference symbolic links.
33
34       Only  a  privileged  process (Linux: one with the CAP_CHOWN capability)
35       may change the owner of a file.  The owner of a  file  may  change  the
36       group  of  the  file  to  any group of which that owner is a member.  A
37       privileged process (Linux: with CAP_CHOWN) may change the  group  arbi‐
38       trarily.
39
40       If the owner or group is specified as -1, then that ID is not changed.
41
42       When the owner or group of an executable file are changed by an unpriv‐
43       ileged user the S_ISUID and S_ISGID mode bits are cleared.  POSIX  does
44       not specify whether this also should happen when root does the chown();
45       the Linux behavior depends on the kernel version.  In case  of  a  non-
46       group-executable  file (i.e., one for which the S_IXGRP bit is not set)
47       the S_ISGID bit indicates mandatory locking, and is not  cleared  by  a
48       chown().
49

RETURN VALUE

51       On  success,  zero is returned.  On error, -1 is returned, and errno is
52       set appropriately.
53

ERRORS

55       Depending on the file system, other errors can be returned.   The  more
56       general errors for chown() are listed below.
57
58       EACCES Search  permission  is denied on a component of the path prefix.
59              (See also path_resolution(7).)
60
61       EFAULT path points outside your accessible address space.
62
63       ELOOP  Too many symbolic links were encountered in resolving path.
64
65       ENAMETOOLONG
66              path is too long.
67
68       ENOENT The file does not exist.
69
70       ENOMEM Insufficient kernel memory was available.
71
72       ENOTDIR
73              A component of the path prefix is not a directory.
74
75       EPERM  The calling process did not have the required  permissions  (see
76              above) to change owner and/or group.
77
78       EROFS  The named file resides on a read-only file system.
79
80       The general errors for fchown() are listed below:
81
82       EBADF  The descriptor is not valid.
83
84       EIO    A low-level I/O error occurred while modifying the inode.
85
86       ENOENT See above.
87
88       EPERM  See above.
89
90       EROFS  See above.
91

CONFORMING TO

93       4.4BSD, SVr4, POSIX.1-2001.
94
95       The 4.4BSD version can be used only by the superuser (that is, ordinary
96       users cannot give away files).
97

NOTES

99       The original Linux chown(), fchown(), and lchown()  system  calls  sup‐
100       ported  only  16-bit user and group IDs.  Subsequently, Linux 2.4 added
101       chown32(), fchown32(), and  lchown32(),  supporting  32-bit  IDs.   The
102       glibc  chown(),  fchown(), and lchown() wrapper functions transparently
103       deal with the variations across kernel versions.
104
105       When a new file is created (by, for example, open(2) or mkdir(2)),  its
106       owner  is  made  the  same  as  the file system user ID of the creating
107       process.  The group of the file depends on a range of factors,  includ‐
108       ing the type of file system, the options used to mount the file system,
109       and whether or not the set-group-ID permission bit is  enabled  on  the
110       parent  directory.   If the file system supports the -o grpid (or, syn‐
111       onymously -o bsdgroups) and -o nogrpid (or, synonymously -o sysvgroups)
112       mount(8) options, then the rules are as follows:
113
114       * If  the file system is mounted with -o grpid, then the group of a new
115         file is made the same as that of the parent directory.
116
117       * If the file system is mounted with -o nogrpid  and  the  set-group-ID
118         bit is disabled on the parent directory, then the group of a new file
119         is made the same as the process's file system GID.
120
121       * If the file system is mounted with -o nogrpid  and  the  set-group-ID
122         bit  is enabled on the parent directory, then the group of a new file
123         is made the same as that of the parent directory.
124
125       As at Linux 2.6.25, the -o grpid and -o nogrpid mount options are  sup‐
126       ported  by  ext2, ext3, ext4, and XFS.  File systems that don't support
127       these mount options follow the -o nogrpid rules.
128
129       The chown() semantics are deliberately violated  on  NFS  file  systems
130       which  have  UID  mapping  enabled.  Additionally, the semantics of all
131       system calls which access  the  file  contents  are  violated,  because
132       chown()  may  cause  immediate access revocation on already open files.
133       Client side caching may lead to a delay between the time  where  owner‐
134       ship  have  been  changed to allow access for a user and the time where
135       the file can actually be accessed by the user on other clients.
136
137       In versions of Linux  prior  to  2.1.81  (and  distinct  from  2.1.46),
138       chown()  did  not  follow  symbolic links.  Since Linux 2.1.81, chown()
139       does follow symbolic links, and there is a  new  system  call  lchown()
140       that does not follow symbolic links.  Since Linux 2.1.86, this new call
141       (that has the same semantics as the  old  chown())  has  got  the  same
142       syscall number, and chown() got the newly introduced number.
143

EXAMPLE

145       The  following  program  changes the ownership of the file named in its
146       second command-line argument to the value specified in its  first  com‐
147       mand-line argument.  The new owner can be specified either as a numeric
148       user ID, or as a username (which is converted to a  user  ID  by  using
149       getpwnam(3) to perform a lookup in the system password file).
150
151       #include <pwd.h>
152       #include <stdio.h>
153       #include <stdlib.h>
154       #include <unistd.h>
155
156       int
157       main(int argc, char *argv[])
158       {
159           uid_t uid;
160           struct passwd *pwd;
161           char *endptr;
162
163           if (argc != 3 || argv[1][0] == '\0') {
164               fprintf(stderr, "%s <owner> <file>\n", argv[0]);
165               exit(EXIT_FAILURE);
166           }
167
168           uid = strtol(argv[1], &endptr, 10);  /* Allow a numeric string */
169
170           if (*endptr != '\0') {         /* Was not pure numeric string */
171               pwd = getpwnam(argv[1]);   /* Try getting UID for username */
172               if (pwd == NULL) {
173                   perror("getpwnam");
174                   exit(EXIT_FAILURE);
175               }
176
177               uid = pwd->pw_uid;
178           }
179
180           if (chown(argv[2], uid, -1) == -1) {
181               perror("chown");
182               exit(EXIT_FAILURE);
183           }
184
185           exit(EXIT_SUCCESS);
186       }
187

SEE ALSO

189       chmod(2), fchownat(2), flock(2), path_resolution(7), symlink(7)
190

COLOPHON

192       This  page  is  part of release 3.53 of the Linux man-pages project.  A
193       description of the project, and information about reporting  bugs,  can
194       be found at http://www.kernel.org/doc/man-pages/.
195
196
197
198Linux                             2010-11-22                          CHOWN(2)
Impressum