CHOWN(2)
π CHOWN(2) π
okay bestie, welcome to the System Calls Manual section, we're doing chown today and honestly it's giving β¨permissionsβ¨ in the best way. this is the change owner and group of a file page and it's lowkey iconic, no cap.
πΈ NAME πΈ
chown, fchown, lchown, fchownat β change owner and group of a file π«Ά
π LIBRARY π
Standard C Library (libc, -lc) β she lives in the standard C library, standard behaviour, we love that for her.
π SYNOPSIS π
#include <unistd.h>
int
chown(const char *path, uid_t owner, gid_t group);
int
fchown(int fd, uid_t owner, gid_t group);
int
lchown(const char *path, uid_t owner, gid_t group);
int
fchownat(int fd, const char *path, uid_t owner, gid_t group, int flag);
four functions, one mission, four very slightly different vibes π
β¨ DESCRIPTION β¨
so the whole point: the owner ID and group ID of the file named by path or referenced by fd gets changed, exactly as specified by the arguments owner and group. that's the tea. π
now the rules, bestie, listen up:
- the owner of a file may change the group to a group of which he or she is a member β that part's fine π
- but the change owner capability is restricted to the super-user. like, that's the whole thing. you need the big crown π
The chown() system call clears the set-user-id and set-group-id bits on the file β this is so nobody accidentally or mischievously creates set-user-id and set-group-id programs β but only if it's not executed by the super-user. so if root does it, the bits stay. noted. π«‘
Also chown() follows symbolic links β it operates on the target of the link, not the link itself. she's chasing the real one, not the middleman π
The fchown() system call is particularly useful when used in conjunction with the file locking primitives β see flock(2) if you wanna know more about that whole situation.
The lchown() system call is similar to chown() but does not follow symbolic links. she minds her business. she stays with the link itself π¦
The fchownat() system call is equivalent to chown() and lchown(), except in the case where path specifies a relative path. in that case, the file to be changed is determined relative to the directory associated with the file descriptor fd instead of the current working directory. so like, you're telling it where to start looking. main character energy π₯
π OPTIONS (well, flag values, technically) π
Values for flag are constructed by a bitwise-inclusive OR of flags from the following list, defined in <fcntl.h>:
AT_SYMLINK_NOFOLLOWβ ifpathnames a symbolic link, ownership of the symbolic link itself is changed. don't follow, just fix the link. π
Also important, and this is a big one:
- if
fchownat()is passed the special valueAT_FDCWDin thefdparameter, the current working directory is used, and the behavior is identical to a call tochown()orlchown()respectively β depending on whether or not theAT_SYMLINK_NOFOLLOWbit is set in theflagargument. iykyk π
And the classic move:
- One of the owner or group id's may be left unchanged by specifying it as
-1. so you can just change one and leave the other alone, slay π
π RETURN VALUES π
- on successful completion, the value
0is returned β¨ - otherwise, the value
-1is returned, and the global variableerrnois set to indicate the error π
that's it. that's the whole vibe check.
π ERRORS π
The chown() and lchown() failures
β οΈ heads up bestie:chown()andlchown()will fail and the file will be unchanged if any of these happen. unchanged β nothing got touched. π
[ENOTDIR]β a component of the path prefix is not a directory.[ENAMETOOLONG]β a component of a pathname exceeded 255 characters, or an entire path name exceeded 1023 characters.[ENOENT]β the named file does not exist. she's not there. ghosted π[EACCES]β search permission is denied for a component of the path prefix.[ELOOP]β too many symbolic links were encountered in translating the pathname.[EPERM]β the operation would change the ownership, but the effective user ID is not the super-user.[EPERM]β the named file has its immutable or append-only flag set. see thechflags(2)manual page for more information.[EROFS]β the named file resides on a read-only file system.[EFAULT]β thepathargument points outside the process's allocated address space.[EIO]β an I/O error occurred while reading from or writing to the file system.[EINTEGRITY]β corrupted data was detected while reading from the file system.
The fchown() failures
β οΈ the fchown() system call will fail if:
[EBADF]β thefdargument does not refer to a valid descriptor.[EINVAL]β thefdargument refers to a socket, not a file.[EPERM]β the effective user ID is not the super-user.[EROFS]β the named file resides on a read-only file system.[EIO]β an I/O error occurred while reading from or writing to the file system.[EINTEGRITY]β corrupted data was detected while reading from the file system.
The fchownat() extra failures
β οΈ in addition to the errors specified forchown()andlchown(), thefchownat()system call may fail if:
[EBADF]β thepathargument does not specify an absolute path and thefdargument is neitherAT_FDCWDnor a valid file descriptor open for searching.[EINVAL]β the value of theflagargument is not valid.[ENOTDIR]β thepathargument is not an absolute path andfdis neitherAT_FDCWDnor a file descriptor associated with a directory.
π¦ SEE ALSO π¦
go read those too bestie, they're all part of the same little family π«Ά
β STANDARDS β
- the
chown()system call is expected to conform toISO/IEC 9945-1:1990(βPOSIX.1β). - the
fchownat()system call follows The Open Group Extended API Set 2 specification.
certified, standardised, official β¨
π HISTORY π
- the
chown()function appeared in Version 1 AT&T UNIX. ancient. legendary. OG. π - the
fchown()system call appeared in 4.2BSD. - the
chown()system call was changed to follow symbolic links in 4.4BSD. - the
lchown()system call was added in FreeBSD 3.0 to compensate for the loss of functionality. they took something away and then gave uslchownback, iconic recovery arc π - the
fchownat()system call appeared in FreeBSD 8.0.
that's the whole page, bestie π this one shipped from Debian with a date of March 30, 2020 β so if you ever wonder when this exact writeup was blessed upon us, that's the timestamp. periodt. π β¨
π¦ where this page comes from
- debian bookworm: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- debian trixie: freebsd-manpages 12.2-2 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu jammy: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu noble: freebsd-manpages 12.2-1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu questing: freebsd-manpages 12.2-2
- ubuntu resolute: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- ubuntu stonking: freebsd-manpages 12.2-2build1 Β· BSD-2-Clause-FreeBSD AND BSD-4-Clause
- Copyright (c) 1980, 1991, 1993, 1994 The Regents of the University of California. All rights reserved
- π girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms