|
Packit |
7cfc04 |
.\" Copyright (c) 1992 Drew Eckhardt (drew@cs.colorado.edu), March 28, 1992
|
|
Packit |
7cfc04 |
.\" and Copyright (c) 1998 Andries Brouwer (aeb@cwi.nl)
|
|
Packit |
7cfc04 |
.\" and Copyright (c) 2006, 2007, 2008, 2014 Michael Kerrisk <mtk.manpages@gmail.com>
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_START(VERBATIM)
|
|
Packit |
7cfc04 |
.\" Permission is granted to make and distribute verbatim copies of this
|
|
Packit |
7cfc04 |
.\" manual provided the copyright notice and this permission notice are
|
|
Packit |
7cfc04 |
.\" preserved on all copies.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Permission is granted to copy and distribute modified versions of this
|
|
Packit |
7cfc04 |
.\" manual under the conditions for verbatim copying, provided that the
|
|
Packit |
7cfc04 |
.\" entire resulting derived work is distributed under the terms of a
|
|
Packit |
7cfc04 |
.\" permission notice identical to this one.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Since the Linux kernel and libraries are constantly changing, this
|
|
Packit |
7cfc04 |
.\" manual page may be incorrect or out-of-date. The author(s) assume no
|
|
Packit |
7cfc04 |
.\" responsibility for errors or omissions, or for damages resulting from
|
|
Packit |
7cfc04 |
.\" the use of the information contained herein. The author(s) may not
|
|
Packit |
7cfc04 |
.\" have taken the same level of care in the production of this manual,
|
|
Packit |
7cfc04 |
.\" which is licensed free of charge, as they might when working
|
|
Packit |
7cfc04 |
.\" professionally.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Formatted or processed versions of this manual, if unaccompanied by
|
|
Packit |
7cfc04 |
.\" the source, must acknowledge the copyright and authors of this work.
|
|
Packit |
7cfc04 |
.\" %%%LICENSE_END
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.\" Modified by Michael Haardt <michael@moria.de>
|
|
Packit |
7cfc04 |
.\" Modified 1993-07-21 by Rik Faith <faith@cs.unc.edu>
|
|
Packit |
7cfc04 |
.\" Modified 1996-07-09 by Andries Brouwer <aeb@cwi.nl>
|
|
Packit |
7cfc04 |
.\" Modified 1996-11-06 by Eric S. Raymond <esr@thyrsus.com>
|
|
Packit |
7cfc04 |
.\" Modified 1997-05-18 by Michael Haardt <michael@cantor.informatik.rwth-aachen.de>
|
|
Packit |
7cfc04 |
.\" Modified 2004-06-23 by Michael Kerrisk <mtk.manpages@gmail.com>
|
|
Packit |
7cfc04 |
.\" 2007-07-08, mtk, added an example program; updated SYNOPSIS
|
|
Packit |
7cfc04 |
.\" 2008-05-08, mtk, Describe rules governing ownership of new files
|
|
Packit |
7cfc04 |
.\" (bsdgroups versus sysvgroups, and the effect of the parent
|
|
Packit |
7cfc04 |
.\" directory's set-group-ID mode bit).
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.TH CHOWN 2 2017-09-15 "Linux" "Linux Programmer's Manual"
|
|
Packit |
7cfc04 |
.SH NAME
|
|
Packit |
7cfc04 |
chown, fchown, lchown, fchownat \- change ownership of a file
|
|
Packit |
7cfc04 |
.SH SYNOPSIS
|
|
Packit |
7cfc04 |
.nf
|
|
Packit |
7cfc04 |
.B #include <unistd.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int chown(const char *" pathname ", uid_t " owner ", gid_t " group );
|
|
Packit |
7cfc04 |
.BI "int fchown(int " fd ", uid_t " owner ", gid_t " group );
|
|
Packit |
7cfc04 |
.BI "int lchown(const char *" pathname ", uid_t " owner ", gid_t " group );
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
.BR "#include <fcntl.h> " "/* Definition of AT_* constants */"
|
|
Packit |
7cfc04 |
.B #include <unistd.h>
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BI "int fchownat(int " dirfd ", const char *" pathname ,
|
|
Packit |
7cfc04 |
.BI " uid_t " owner ", gid_t " group ", int " flags );
|
|
Packit |
7cfc04 |
.fi
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.in -4n
|
|
Packit |
7cfc04 |
Feature Test Macro Requirements for glibc (see
|
|
Packit |
7cfc04 |
.BR feature_test_macros (7)):
|
|
Packit |
7cfc04 |
.in
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR fchown (),
|
|
Packit |
7cfc04 |
.BR lchown ():
|
|
Packit |
7cfc04 |
.PD 0
|
|
Packit |
7cfc04 |
.ad l
|
|
Packit |
7cfc04 |
.RS 4
|
|
Packit |
7cfc04 |
/* Since glibc 2.12: */ _POSIX_C_SOURCE\ >=\ 200809L
|
|
Packit |
7cfc04 |
|| _XOPEN_SOURCE\ >=\ 500
|
|
Packit |
7cfc04 |
.\" || _XOPEN_SOURCE\ &&\ _XOPEN_SOURCE_EXTENDED
|
|
Packit |
7cfc04 |
|| /* Glibc versions <= 2.19: */ _BSD_SOURCE
|
|
Packit |
7cfc04 |
.RE
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR fchownat ():
|
|
Packit |
7cfc04 |
.PD 0
|
|
Packit |
7cfc04 |
.ad l
|
|
Packit |
7cfc04 |
.RS 4
|
|
Packit |
7cfc04 |
.TP 4
|
|
Packit |
7cfc04 |
Since glibc 2.10:
|
|
Packit |
7cfc04 |
_POSIX_C_SOURCE\ >=\ 200809L
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
Before glibc 2.10:
|
|
Packit |
7cfc04 |
_ATFILE_SOURCE
|
|
Packit |
7cfc04 |
.RE
|
|
Packit |
7cfc04 |
.ad
|
|
Packit |
7cfc04 |
.PD
|
|
Packit |
7cfc04 |
.SH DESCRIPTION
|
|
Packit |
7cfc04 |
These system calls change the owner and group of a file.
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
.BR fchown (),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR lchown ()
|
|
Packit |
7cfc04 |
system calls differ only in how the file is specified:
|
|
Packit |
7cfc04 |
.IP * 2
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
changes the ownership of the file specified by
|
|
Packit |
7cfc04 |
.IR pathname ,
|
|
Packit |
7cfc04 |
which is dereferenced if it is a symbolic link.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
.BR fchown ()
|
|
Packit |
7cfc04 |
changes the ownership of the file referred to by the open file descriptor
|
|
Packit |
7cfc04 |
.IR fd .
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
.BR lchown ()
|
|
Packit |
7cfc04 |
is like
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
but does not dereference symbolic links.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
Only a privileged process (Linux: one with the
|
|
Packit |
7cfc04 |
.B CAP_CHOWN
|
|
Packit |
7cfc04 |
capability) may change the owner of a file.
|
|
Packit |
7cfc04 |
The owner of a file may change the group of the file
|
|
Packit |
7cfc04 |
to any group of which that owner is a member.
|
|
Packit |
7cfc04 |
A privileged process (Linux: with
|
|
Packit |
7cfc04 |
.BR CAP_CHOWN )
|
|
Packit |
7cfc04 |
may change the group arbitrarily.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If the
|
|
Packit |
7cfc04 |
.I owner
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
.I group
|
|
Packit |
7cfc04 |
is specified as \-1, then that ID is not changed.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
When the owner or group of an executable file is
|
|
Packit |
7cfc04 |
changed by an unprivileged user, the
|
|
Packit |
7cfc04 |
.B S_ISUID
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.B S_ISGID
|
|
Packit |
7cfc04 |
mode bits are cleared.
|
|
Packit |
7cfc04 |
POSIX does not specify whether
|
|
Packit |
7cfc04 |
this also should happen when root does the
|
|
Packit |
7cfc04 |
.BR chown ();
|
|
Packit |
7cfc04 |
the Linux behavior depends on the kernel version,
|
|
Packit |
7cfc04 |
and since Linux 2.2.13, root is treated like other users.
|
|
Packit |
7cfc04 |
.\" In Linux 2.0 kernels, superuser was like everyone else
|
|
Packit |
7cfc04 |
.\" In 2.2, up to 2.2.12, these bits were not cleared for superuser.
|
|
Packit |
7cfc04 |
.\" Since 2.2.13, superuser is once more like everyone else.
|
|
Packit |
7cfc04 |
In case of a non-group-executable file (i.e., one for which the
|
|
Packit |
7cfc04 |
.B S_IXGRP
|
|
Packit |
7cfc04 |
bit is not set) the
|
|
Packit |
7cfc04 |
.B S_ISGID
|
|
Packit |
7cfc04 |
bit indicates mandatory locking, and is not cleared by a
|
|
Packit |
7cfc04 |
.BR chown ().
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
When the owner or group of an executable file is changed (by any user),
|
|
Packit |
7cfc04 |
all capability sets for the file are cleared.
|
|
Packit |
7cfc04 |
.\"
|
|
Packit |
7cfc04 |
.SS fchownat()
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR fchownat ()
|
|
Packit |
7cfc04 |
system call operates in exactly the same way as
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
except for the differences described here.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If the pathname given in
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is relative, then it is interpreted relative to the directory
|
|
Packit |
7cfc04 |
referred to by the file descriptor
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
(rather than relative to the current working directory of
|
|
Packit |
7cfc04 |
the calling process, as is done by
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
for a relative pathname).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is relative and
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
is the special value
|
|
Packit |
7cfc04 |
.BR AT_FDCWD ,
|
|
Packit |
7cfc04 |
then
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is interpreted relative to the current working
|
|
Packit |
7cfc04 |
directory of the calling process (like
|
|
Packit |
7cfc04 |
.BR chown ()).
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is absolute, then
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
is ignored.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.I flags
|
|
Packit |
7cfc04 |
argument is a bit mask created by ORing together
|
|
Packit |
7cfc04 |
0 or more of the following values;
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.BR AT_EMPTY_PATH " (since Linux 2.6.39)"
|
|
Packit |
7cfc04 |
.\" commit 65cfc6722361570bfe255698d9cd4dccaf47570d
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is an empty string, operate on the file referred to by
|
|
Packit |
7cfc04 |
.IR dirfd
|
|
Packit |
7cfc04 |
(which may have been obtained using the
|
|
Packit |
7cfc04 |
.BR open (2)
|
|
Packit |
7cfc04 |
.B O_PATH
|
|
Packit |
7cfc04 |
flag).
|
|
Packit |
7cfc04 |
In this case,
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
can refer to any type of file, not just a directory.
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
is
|
|
Packit |
7cfc04 |
.BR AT_FDCWD ,
|
|
Packit |
7cfc04 |
the call operates on the current working directory.
|
|
Packit |
7cfc04 |
This flag is Linux-specific; define
|
|
Packit |
7cfc04 |
.B _GNU_SOURCE
|
|
Packit |
7cfc04 |
.\" Before glibc 2.16, defining _ATFILE_SOURCE sufficed
|
|
Packit |
7cfc04 |
to obtain its definition.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B AT_SYMLINK_NOFOLLOW
|
|
Packit |
7cfc04 |
If
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is a symbolic link, do not dereference it:
|
|
Packit |
7cfc04 |
instead operate on the link itself, like
|
|
Packit |
7cfc04 |
.BR lchown ().
|
|
Packit |
7cfc04 |
(By default,
|
|
Packit |
7cfc04 |
.BR fchownat ()
|
|
Packit |
7cfc04 |
dereferences symbolic links, like
|
|
Packit |
7cfc04 |
.BR chown ().)
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
See
|
|
Packit |
7cfc04 |
.BR openat (2)
|
|
Packit |
7cfc04 |
for an explanation of the need for
|
|
Packit |
7cfc04 |
.BR fchownat ().
|
|
Packit |
7cfc04 |
.SH RETURN VALUE
|
|
Packit |
7cfc04 |
On success, zero is returned.
|
|
Packit |
7cfc04 |
On error, \-1 is returned, and
|
|
Packit |
7cfc04 |
.I errno
|
|
Packit |
7cfc04 |
is set appropriately.
|
|
Packit |
7cfc04 |
.SH ERRORS
|
|
Packit |
7cfc04 |
Depending on the filesystem,
|
|
Packit |
7cfc04 |
errors other than those listed below can be returned.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The more general errors for
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
are listed below.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EACCES
|
|
Packit |
7cfc04 |
Search permission is denied on a component of the path prefix.
|
|
Packit |
7cfc04 |
(See also
|
|
Packit |
7cfc04 |
.BR path_resolution (7).)
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EFAULT
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
points outside your accessible address space.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ELOOP
|
|
Packit |
7cfc04 |
Too many symbolic links were encountered in resolving
|
|
Packit |
7cfc04 |
.IR pathname .
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENAMETOOLONG
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is too long.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOENT
|
|
Packit |
7cfc04 |
The file does not exist.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOMEM
|
|
Packit |
7cfc04 |
Insufficient kernel memory was available.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOTDIR
|
|
Packit |
7cfc04 |
A component of the path prefix is not a directory.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EPERM
|
|
Packit |
7cfc04 |
The calling process did not have the required permissions
|
|
Packit |
7cfc04 |
(see above) to change owner and/or group.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EPERM
|
|
Packit |
7cfc04 |
The file is marked immutable or append-only.
|
|
Packit |
7cfc04 |
(See
|
|
Packit |
7cfc04 |
.BR ioctl_iflags (2).)
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EROFS
|
|
Packit |
7cfc04 |
The named file resides on a read-only filesystem.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The general errors for
|
|
Packit |
7cfc04 |
.BR fchown ()
|
|
Packit |
7cfc04 |
are listed below:
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EBADF
|
|
Packit |
7cfc04 |
.I fd
|
|
Packit |
7cfc04 |
is not a valid open file descriptor.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EIO
|
|
Packit |
7cfc04 |
A low-level I/O error occurred while modifying the inode.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOENT
|
|
Packit |
7cfc04 |
See above.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EPERM
|
|
Packit |
7cfc04 |
See above.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EROFS
|
|
Packit |
7cfc04 |
See above.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The same errors that occur for
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
can also occur for
|
|
Packit |
7cfc04 |
.BR fchownat ().
|
|
Packit |
7cfc04 |
The following additional errors can occur for
|
|
Packit |
7cfc04 |
.BR fchownat ():
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EBADF
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
is not a valid file descriptor.
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B EINVAL
|
|
Packit |
7cfc04 |
Invalid flag specified in
|
|
Packit |
7cfc04 |
.IR flags .
|
|
Packit |
7cfc04 |
.TP
|
|
Packit |
7cfc04 |
.B ENOTDIR
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is relative and
|
|
Packit |
7cfc04 |
.I dirfd
|
|
Packit |
7cfc04 |
is a file descriptor referring to a file other than a directory.
|
|
Packit |
7cfc04 |
.SH VERSIONS
|
|
Packit |
7cfc04 |
.BR fchownat ()
|
|
Packit |
7cfc04 |
was added to Linux in kernel 2.6.16;
|
|
Packit |
7cfc04 |
library support was added to glibc in version 2.4.
|
|
Packit |
7cfc04 |
.SH CONFORMING TO
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
.BR fchown (),
|
|
Packit |
7cfc04 |
.BR lchown ():
|
|
Packit |
7cfc04 |
4.4BSD, SVr4, POSIX.1-2001, POSIX.1-2008.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The 4.4BSD version can be
|
|
Packit |
7cfc04 |
used only by the superuser (that is, ordinary users cannot give away files).
|
|
Packit |
7cfc04 |
.\" chown():
|
|
Packit |
7cfc04 |
.\" SVr4 documents EINVAL, EINTR, ENOLINK and EMULTIHOP returns, but no
|
|
Packit |
7cfc04 |
.\" ENOMEM. POSIX.1 does not document ENOMEM or ELOOP error conditions.
|
|
Packit |
7cfc04 |
.\" fchown():
|
|
Packit |
7cfc04 |
.\" SVr4 documents additional EINVAL, EIO, EINTR, and ENOLINK
|
|
Packit |
7cfc04 |
.\" error conditions.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
.BR fchownat ():
|
|
Packit |
7cfc04 |
POSIX.1-2008.
|
|
Packit |
7cfc04 |
.SH NOTES
|
|
Packit |
7cfc04 |
.SS Ownership of new files
|
|
Packit |
7cfc04 |
When a new file is created (by, for example,
|
|
Packit |
7cfc04 |
.BR open (2)
|
|
Packit |
7cfc04 |
or
|
|
Packit |
7cfc04 |
.BR mkdir (2)),
|
|
Packit |
7cfc04 |
its owner is made the same as the filesystem user ID of the
|
|
Packit |
7cfc04 |
creating process.
|
|
Packit |
7cfc04 |
The group of the file depends on a range of factors,
|
|
Packit |
7cfc04 |
including the type of filesystem,
|
|
Packit |
7cfc04 |
the options used to mount the filesystem,
|
|
Packit |
7cfc04 |
and whether or not the set-group-ID mode bit is enabled
|
|
Packit |
7cfc04 |
on the parent directory.
|
|
Packit |
7cfc04 |
If the filesystem supports the
|
|
Packit |
7cfc04 |
.B "\-o\ grpid"
|
|
Packit |
7cfc04 |
(or, synonymously
|
|
Packit |
7cfc04 |
.BR "\-o\ bsdgroups" )
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.B "\-o\ nogrpid"
|
|
Packit |
7cfc04 |
(or, synonymously
|
|
Packit |
7cfc04 |
.BR "\-o\ sysvgroups" )
|
|
Packit |
7cfc04 |
.BR mount (8)
|
|
Packit |
7cfc04 |
options, then the rules are as follows:
|
|
Packit |
7cfc04 |
.IP * 2
|
|
Packit |
7cfc04 |
If the filesystem is mounted with
|
|
Packit |
7cfc04 |
.BR "\-o\ grpid" ,
|
|
Packit |
7cfc04 |
then the group of a new file is made
|
|
Packit |
7cfc04 |
the same as that of the parent directory.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
If the filesystem is mounted with
|
|
Packit |
7cfc04 |
.BR "\-o\ nogrpid"
|
|
Packit |
7cfc04 |
and the set-group-ID bit is disabled on the parent directory,
|
|
Packit |
7cfc04 |
then the group of a new file is made the same as the
|
|
Packit |
7cfc04 |
process's filesystem GID.
|
|
Packit |
7cfc04 |
.IP *
|
|
Packit |
7cfc04 |
If the filesystem is mounted with
|
|
Packit |
7cfc04 |
.BR "\-o\ nogrpid"
|
|
Packit |
7cfc04 |
and the set-group-ID bit is enabled on the parent directory,
|
|
Packit |
7cfc04 |
then the group of a new file is made
|
|
Packit |
7cfc04 |
the same as that of the parent directory.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
As at Linux 4.12,
|
|
Packit |
7cfc04 |
the
|
|
Packit |
7cfc04 |
.BR "\-o\ grpid"
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR "\-o\ nogrpid"
|
|
Packit |
7cfc04 |
mount options are supported by ext2, ext3, ext4, and XFS.
|
|
Packit |
7cfc04 |
Filesystems that don't support these mount options follow the
|
|
Packit |
7cfc04 |
.BR "\-o\ nogrpid"
|
|
Packit |
7cfc04 |
rules.
|
|
Packit |
7cfc04 |
.SS Glibc notes
|
|
Packit |
7cfc04 |
On older kernels where
|
|
Packit |
7cfc04 |
.BR fchownat ()
|
|
Packit |
7cfc04 |
is unavailable, the glibc wrapper function falls back to the use of
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR lchown ().
|
|
Packit |
7cfc04 |
When
|
|
Packit |
7cfc04 |
.I pathname
|
|
Packit |
7cfc04 |
is a relative pathname,
|
|
Packit |
7cfc04 |
glibc constructs a pathname based on the symbolic link in
|
|
Packit |
7cfc04 |
.IR /proc/self/fd
|
|
Packit |
7cfc04 |
that corresponds to the
|
|
Packit |
7cfc04 |
.IR dirfd
|
|
Packit |
7cfc04 |
argument.
|
|
Packit |
7cfc04 |
.SS NFS
|
|
Packit |
7cfc04 |
The
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
semantics are deliberately violated on NFS filesystems
|
|
Packit |
7cfc04 |
which have UID mapping enabled.
|
|
Packit |
7cfc04 |
Additionally, the semantics of all system
|
|
Packit |
7cfc04 |
calls which access the file contents are violated, because
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
may cause immediate access revocation on already open files.
|
|
Packit |
7cfc04 |
Client side
|
|
Packit |
7cfc04 |
caching may lead to a delay between the time where ownership have
|
|
Packit |
7cfc04 |
been changed to allow access for a user and the time where the file can
|
|
Packit |
7cfc04 |
actually be accessed by the user on other clients.
|
|
Packit |
7cfc04 |
.SS Historical details
|
|
Packit |
7cfc04 |
The original Linux
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
.BR fchown (),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR lchown ()
|
|
Packit |
7cfc04 |
system calls supported only 16-bit user and group IDs.
|
|
Packit |
7cfc04 |
Subsequently, Linux 2.4 added
|
|
Packit |
7cfc04 |
.BR chown32 (),
|
|
Packit |
7cfc04 |
.BR fchown32 (),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR lchown32 (),
|
|
Packit |
7cfc04 |
supporting 32-bit IDs.
|
|
Packit |
7cfc04 |
The glibc
|
|
Packit |
7cfc04 |
.BR chown (),
|
|
Packit |
7cfc04 |
.BR fchown (),
|
|
Packit |
7cfc04 |
and
|
|
Packit |
7cfc04 |
.BR lchown ()
|
|
Packit |
7cfc04 |
wrapper functions transparently deal with the variations across kernel versions.
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
In versions of Linux prior to 2.1.81 (and distinct from 2.1.46),
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
did not follow symbolic links.
|
|
Packit |
7cfc04 |
Since Linux 2.1.81,
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
does follow symbolic links, and there is a new system call
|
|
Packit |
7cfc04 |
.BR lchown ()
|
|
Packit |
7cfc04 |
that does not follow symbolic links.
|
|
Packit |
7cfc04 |
Since Linux 2.1.86, this new call (that has the same semantics
|
|
Packit |
7cfc04 |
as the old
|
|
Packit |
7cfc04 |
.BR chown ())
|
|
Packit |
7cfc04 |
has got the same syscall number, and
|
|
Packit |
7cfc04 |
.BR chown ()
|
|
Packit |
7cfc04 |
got the newly introduced number.
|
|
Packit |
7cfc04 |
.SH EXAMPLE
|
|
Packit |
7cfc04 |
.PP
|
|
Packit |
7cfc04 |
The following program changes the ownership of the file named in
|
|
Packit |
7cfc04 |
its second command-line argument to the value specified in its
|
|
Packit |
7cfc04 |
first command-line argument.
|
|
Packit |
7cfc04 |
The new owner can be specified either as a numeric user ID,
|
|
Packit |
7cfc04 |
or as a username (which is converted to a user ID by using
|
|
Packit |
7cfc04 |
.BR getpwnam (3)
|
|
Packit |
7cfc04 |
to perform a lookup in the system password file).
|
|
Packit |
7cfc04 |
.SS Program source
|
|
Packit |
7cfc04 |
.EX
|
|
Packit |
7cfc04 |
#include <pwd.h>
|
|
Packit |
7cfc04 |
#include <stdio.h>
|
|
Packit |
7cfc04 |
#include <stdlib.h>
|
|
Packit |
7cfc04 |
#include <unistd.h>
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
int
|
|
Packit |
7cfc04 |
main(int argc, char *argv[])
|
|
Packit |
7cfc04 |
{
|
|
Packit |
7cfc04 |
uid_t uid;
|
|
Packit |
7cfc04 |
struct passwd *pwd;
|
|
Packit |
7cfc04 |
char *endptr;
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (argc != 3 || argv[1][0] == \(aq\\0\(aq) {
|
|
Packit |
7cfc04 |
fprintf(stderr, "%s <owner> <file>\\n", argv[0]);
|
|
Packit |
7cfc04 |
exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
uid = strtol(argv[1], &endptr, 10); /* Allow a numeric string */
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (*endptr != \(aq\\0\(aq) { /* Was not pure numeric string */
|
|
Packit |
7cfc04 |
pwd = getpwnam(argv[1]); /* Try getting UID for username */
|
|
Packit |
7cfc04 |
if (pwd == NULL) {
|
|
Packit |
7cfc04 |
perror("getpwnam");
|
|
Packit |
7cfc04 |
exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
uid = pwd\->pw_uid;
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
if (chown(argv[2], uid, \-1) == \-1) {
|
|
Packit |
7cfc04 |
perror("chown");
|
|
Packit |
7cfc04 |
exit(EXIT_FAILURE);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
|
|
Packit |
7cfc04 |
exit(EXIT_SUCCESS);
|
|
Packit |
7cfc04 |
}
|
|
Packit |
7cfc04 |
.EE
|
|
Packit |
7cfc04 |
.SH SEE ALSO
|
|
Packit |
7cfc04 |
.BR chgrp (1),
|
|
Packit |
7cfc04 |
.BR chown (1),
|
|
Packit |
7cfc04 |
.BR chmod (2),
|
|
Packit |
7cfc04 |
.BR flock (2),
|
|
Packit |
7cfc04 |
.BR path_resolution (7),
|
|
Packit |
7cfc04 |
.BR symlink (7)
|
|
Packit |
7cfc04 |
.SH COLOPHON
|
|
Packit |
7cfc04 |
This page is part of release 4.15 of the Linux
|
|
Packit |
7cfc04 |
.I man-pages
|
|
Packit |
7cfc04 |
project.
|
|
Packit |
7cfc04 |
A description of the project,
|
|
Packit |
7cfc04 |
information about reporting bugs,
|
|
Packit |
7cfc04 |
and the latest version of this page,
|
|
Packit |
7cfc04 |
can be found at
|
|
Packit |
7cfc04 |
\%https://www.kernel.org/doc/man\-pages/.
|