chown(2) System Calls Manual chown(2) chown, fchown, lchown, fchownat - LIBRARY Standard C library (libc, -lc) #include int chown(const char *pathname, uid_t owner, gid_t group); int fchown(int fd, uid_t owner, gid_t group); int lchown(const char *pathname, uid_t owner, gid_t group); #include /* of AT_* */ #include int fchownat(int dirfd, const char *pathname, uid_t owner, gid_t group, int flags); glibc (. feature_test_macros(7)): fchown(), lchown(): /* Since glibc 2.12: */ _POSIX_C_SOURCE >= 200809L || _XOPEN_SOURCE >= 500 || /* glibc <= 2.19: */ _BSD_SOURCE fchownat(): glibc 2.10: _POSIX_C_SOURCE >= 200809L glibc 2.10: _ATFILE_SOURCE . chown(), fchown() lchown() , : o chown() , pathname, , . o fchown() , fd. o lchown() chown() , . (Linux: CAP_CHOWN) . , . (Linux: CAP_CHOWN) . owner group -1, . , S_ISUID S_ISGID POSIX , chown() ; Linux Linux 2.2.13, root . (. ., S_IXGRP) S_ISGID , chown(). ( ), . fchownat() fchownat() chown(), , . pathname , , dirfd ( , chown()). pathname dirfd AT_FDCWD, pathname ( chown()). pathname , dirfd . flags , (OR) ; AT_EMPTY_PATH ( Linux 2.6.39) pathname , , dirfd ( open(2) O_PATH). , dirfd , . dirfd AT_FDCWD, . Linux; _GNU_SOURCE. AT_SYMLINK_NOFOLLOW pathname , , , lchown() ( , fchownat() chown()). openat(2) fchownat(). On success, zero is returned. On error, -1 is returned, and errno is set to indicate the error. , . chown() . EACCES - (. path_resolution(7).) EBADF (fchown()) fd . EBADF (fchownat()) pathname is relative but dirfd is neither AT_FDCWD nor a valid file descriptor. EFAULT pathname . EINVAL (fchownat()) flags. EIO (fchown()) (inode) /. ELOOP pathname . ENAMETOOLONG pathname. ENOENT . ENOMEM . ENOTDIR . ENOTDIR (fchownat()) pathname dirfd , , . EPERM (. ), / . EPERM (immutable) ( ioctl_iflags(2)). EROFS , . 4.4BSD ( , ). POSIX.1-2008. chown() fchown() lchown() 4.4BSD, SVr4, POSIX.1-2001. fchownat() POSIX.1-2008. Linux 2.6.16, glibc 2.4. (, open(2) mkdir(2)), ID . , , set-group-ID . mount(8) -o grpid ( -o bsdgroups) -o nogrpid ( -o sysvgroups), : o -o grpid, . o -o nogrpid set-group-ID, GID . o -o nogrpid set-group-ID, . Linux 4.12, -o grpid -o nogrpid ext2, ext3, ext4 XFS. , , -o nogrpid. glibc , fchownat() , glibc chown() lchown(). pathname , glibc /proc/self/fd, dirfd. NFS chown() NFS, UID. , , , chown() . , . chown(), fchown() lchown() Linux 16- . Linux 2.4 chown32(), fchown32() lchown32(), 32- . glibc chown(), fchown() lchown() . Before Linux 2.1.81 (except 2.1.46), chown() did not follow symbolic links. Since Linux 2.1.81, chown() does follow symbolic links, and there is a new system call lchown() that does not follow symbolic links. Since Linux 2.1.86, this new call (that has the same semantics as the old chown()) has got the same syscall number, and chown() got the newly introduced number. , , , . ID, ( ID getpwnam(3), ). #include #include #include #include int main(int argc, char *argv[]) { char *endptr; uid_t uid; struct passwd *pwd; if (argc != 3 || argv[1][0] == '\0') { fprintf(stderr, "%s \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); } . chgrp(1), chown(1), chmod(2), flock(2), path_resolution(7), symlink(7) Azamat Hackimov , Dmitriy S. Seregin , Dmitry Bolkhovskikh , Katrin Kutepova , Yuri Kozlov ; GNU 3 , . . , , . Linux man-pages 6.06 31 2023 . chown(2)