table of contents
other versions
- wheezy 3.44-1
 - jessie 3.74-1
 - jessie-backports 4.10-2~bpo8+1
 - testing 4.10-2
 - unstable 4.10-2
 
other sections
| CHOWN(2) | Linux Programmer's Manual | CHOWN(2) | 
NAME¶
chown, fchown, lchown - change ownership of a fileSYNOPSIS¶
#include <unistd.h>Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
_BSD_SOURCE ||
  _XOPEN_SOURCE >= 500 ||
  _XOPEN_SOURCE && _XOPEN_SOURCE_EXTENDED
 
|| /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L
DESCRIPTION¶
These system calls change the owner and group of a file. The differ only in how the file is specified:- *
 - chown() changes the ownership of the file specified by path, which is dereferenced if it is a symbolic link.
 
- *
 - fchown() changes the ownership of the file referred to by the open file descriptor fd.
 
- *
 - lchown() is like chown(), but does not dereference symbolic links.
 
RETURN VALUE¶
On success, zero is returned. On error, -1 is returned, and errno is set appropriately.ERRORS¶
Depending on the file system, other errors can be returned. The more general errors for chown() are listed below.- EACCES
 - Search permission is denied on a component of the path prefix. (See also path_resolution(7).)
 
- EFAULT
 - path points outside your accessible address space.
 
- ELOOP
 - Too many symbolic links were encountered in resolving path.
 
- ENAMETOOLONG
 - path is too long.
 
- ENOENT
 - The file does not exist.
 
- ENOMEM
 - Insufficient kernel memory was available.
 
- ENOTDIR
 - A component of the path prefix is not a directory.
 
- EPERM
 - The calling process did not have the required permissions (see above) to change owner and/or group.
 
- EROFS
 - The named file resides on a read-only file system.
 
- EBADF
 - The descriptor is not valid.
 
- EIO
 - A low-level I/O error occurred while modifying the inode.
 
- ENOENT
 - See above.
 
- EPERM
 - See above.
 
- EROFS
 - See above.
 
CONFORMING TO¶
4.4BSD, SVr4, POSIX.1-2001.NOTES¶
The original Linux chown(), fchown(), and lchown() system calls supported only 16-bit user and group IDs. Subsequently, Linux 2.4 added chown32(), fchown32(), and lchown32(), supporting 32-bit IDs. The glibc chown(), fchown(), and lchown() wrapper functions transparently deal with the variations across kernel versions.- *
 - If the file system is mounted with -o grpid, then the group of a new file is made the same as that of the parent directory.
 
- *
 - If the file system is mounted with -o nogrpid and the set-group-ID bit is disabled on the parent directory, then the group of a new file is made the same as the process's file system GID.
 
- *
 - If the file system is mounted with -o nogrpid and the set-group-ID bit is enabled on the parent directory, then the group of a new file is made the same as that of the parent directory.
 
EXAMPLE¶
The following program changes the ownership of the file named in its second command-line argument to the value specified in its first command-line argument. The new owner can be specified either as a numeric user ID, or as a username (which is converted to a user ID by using getpwnam(3) to perform a lookup in the system password file).
#include <pwd.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
int
main(int argc, char *argv[])
{
    uid_t uid;
    struct passwd *pwd;
    char *endptr;
    if (argc != 3 || argv[1][0] == '\0') {
        fprintf(stderr, "%s <owner> <file>\n", argv[0]);
        exit(EXIT_FAILURE);
    }
    uid = strtol(argv[1], &endptr, 10);  /* Allow a numeric string */
    if (*endptr != '\0') {         /* Was not pure numeric string */
        pwd = getpwnam(argv[1]);   /* Try getting UID for username */
        if (pwd == NULL) {
            perror("getpwnam");
            exit(EXIT_FAILURE);
        }
        uid = pwd->pw_uid;
    }
    if (chown(argv[2], uid, -1) == -1) {
        perror("chown");
        exit(EXIT_FAILURE);
    }
    exit(EXIT_SUCCESS);
}
SEE ALSO¶
chmod(2), fchownat(2), flock(2), path_resolution(7), symlink(7)COLOPHON¶
This page is part of release 3.44 of the Linux man-pages project. A description of the project, and information about reporting bugs, can be found at http://www.kernel.org/doc/man-pages/.| 2010-11-22 | Linux |