1.\" $OpenBSD: chown.2,v 1.14 2005/08/12 18:56:40 jmc Exp $ 2.\" $NetBSD: chown.2,v 1.10 1995/10/12 15:40:47 jtc Exp $ 3.\" 4.\" Copyright (c) 1980, 1991, 1993, 1994 5.\" The Regents of the University of California. All rights reserved. 6.\" 7.\" Redistribution and use in source and binary forms, with or without 8.\" modification, are permitted provided that the following conditions 9.\" are met: 10.\" 1. Redistributions of source code must retain the above copyright 11.\" notice, this list of conditions and the following disclaimer. 12.\" 2. Redistributions in binary form must reproduce the above copyright 13.\" notice, this list of conditions and the following disclaimer in the 14.\" documentation and/or other materials provided with the distribution. 15.\" 3. Neither the name of the University nor the names of its contributors 16.\" may be used to endorse or promote products derived from this software 17.\" without specific prior written permission. 18.\" 19.\" THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND 20.\" ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 21.\" IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 22.\" ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE 23.\" FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 24.\" DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS 25.\" OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) 26.\" HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT 27.\" LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY 28.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 29.\" SUCH DAMAGE. 30.\" 31.\" @(#)chown.2 8.4 (Berkeley) 4/19/94 32.\" 33.Dd January 25, 1997 34.Dt CHOWN 2 35.Os 36.Sh NAME 37.Nm chown , 38.Nm lchown , 39.Nm fchown 40.Nd change owner and group of a file or link 41.Sh SYNOPSIS 42.Fd #include <sys/types.h> 43.Fd #include <unistd.h> 44.Ft int 45.Fn chown "const char *path" "uid_t owner" "gid_t group" 46.Ft int 47.Fn lchown "const char *path" "uid_t owner" "gid_t group" 48.Ft int 49.Fn fchown "int fd" "uid_t owner" "gid_t group" 50.Sh DESCRIPTION 51The owner ID and group ID of the file (or link) named by 52.Fa path 53or referenced by 54.Fa fd 55is changed as specified by the arguments 56.Fa owner 57and 58.Fa group . 59The owner of a file may change the 60.Fa group 61to a group of which he or she is a member, but the change 62.Fa owner 63capability is restricted to the superuser. 64.Pp 65By default, 66.Fn chown 67clears the set-user-ID and set-group-ID bits on the file 68to prevent accidental or mischievous creation of 69set-user-ID and set-group-ID programs. 70This behaviour can be overridden by setting the 71.Xr sysctl 8 72variable 73.Va fs.posix.setuid 74to zero. 75.Pp 76.Fn lchown 77operates similarly to how 78.Fn chown 79operated on older systems, and does not follow symbolic links. 80It allows the owner and group of a symbolic link to be set. 81.Pp 82.Fn fchown 83is particularly useful when used in conjunction 84with the file locking primitives (see 85.Xr flock 2 ) . 86.Pp 87One of the owner or group IDs 88may be left unchanged by specifying it as \-1. 89.Sh RETURN VALUES 90Zero is returned if the operation was successful; 91\-1 is returned if an error occurs, with a more specific 92error code being placed in the global variable 93.Va errno . 94.Sh ERRORS 95.Fn chown 96or 97.Fn lchown 98will fail and the file or link will be unchanged if: 99.Bl -tag -width Er 100.It Bq Er ENOTDIR 101A component of the path prefix is not a directory. 102.It Bq Er ENAMETOOLONG 103A component of a pathname exceeded 104.Dv {NAME_MAX} 105characters, or an entire path name exceeded 106.Dv {PATH_MAX} 107characters. 108.It Bq Er ENOENT 109The named file does not exist. 110.It Bq Er EACCES 111Search permission is denied for a component of the path prefix. 112.It Bq Er ELOOP 113Too many symbolic links were encountered in translating the pathname. 114.It Bq Er EPERM 115The effective user ID is not the superuser. 116.It Bq Er EROFS 117The named file resides on a read-only file system. 118.It Bq Er EFAULT 119.Fa path 120points outside the process's allocated address space. 121.It Bq Er EIO 122An I/O error occurred while reading from or writing to the file system. 123.El 124.Pp 125.Fn fchown 126will fail if: 127.Bl -tag -width Er 128.It Bq Er EBADF 129.Fa fd 130does not refer to a valid descriptor. 131.It Bq Er EINVAL 132.Fa fd 133refers to a socket, not a file. 134.It Bq Er EPERM 135The effective user ID is not the superuser. 136.It Bq Er EROFS 137The named file resides on a read-only file system. 138.It Bq Er EIO 139An I/O error occurred while reading from or writing to the file system. 140.El 141.Sh SEE ALSO 142.Xr chgrp 1 , 143.Xr chmod 2 , 144.Xr flock 2 , 145.Xr chown 8 146.Sh STANDARDS 147The 148.Fn chown 149function is expected to conform to 150.St -p1003.1-88 . 151.Sh HISTORY 152The 153.Fn fchown 154function call appeared in 155.Bx 4.2 . 156.Pp 157The 158.Fn chown 159and 160.Fn fchown 161functions were changed to follow symbolic links in 162.Bx 4.4 . 163.Pp 164The 165.Fn lchown 166function was added to 167.Ox 168due to the above. 169