chmod(2)
๐ chmod(2) โ the system calls manual, bestie edition ๐
okay so. you're here for chmod, fchmod, and fchmodat โ the system calls that change permissions on a file. yes, THE chmod. the one everyone's shell-fu is obsessed with. but this is the system call page, the raw C-level version, so buckle up ๐
โจ NAME โจ
chmod, fchmod, fchmodat โ change permissions of a file ๐
so basically the whole vibe of this page is "make the file let people in (or not)" and there are three flavors of doing it.
๐ LIBRARY ๐
Standard C library (libc, -lc). so you link -lc like always, no surprise plot twist here ๐ซถ
๐ธ SYNOPSIS ๐ธ
here's the whole cast, no cap:
#include <sys/stat.h>
int chmod(const char *path, mode_t mode);
int fchmod(int fd, mode_t mode);
#include <fcntl.h> /* Definition of AT_* constants */
#include <sys/stat.h>
int fchmodat(int dirfd, const char *path, mode_t mode, int flags);
Feature Test Macro Requirements for glibc (there's a whole page for this, go peek at feature_test_macros(7)):
fchmod():
Since glibc 2.24:
_POSIX_C_SOURCE >= 199309L
glibc 2.19 to glibc 2.23
_POSIX_C_SOURCE
glibc 2.16 to glibc 2.19:
_BSD_SOURCE || _POSIX_C_SOURCE
glibc 2.12 to glibc 2.16:
_BSD_SOURCE || _XOPEN_SOURCE >= 500
|| _POSIX_C_SOURCE >= 200809L
glibc 2.11 and earlier:
_BSD_SOURCE || _XOPEN_SOURCE >= 500
fchmodat():
Since glibc 2.10:
_POSIX_C_SOURCE >= 200809L
Before glibc 2.10:
_ATFILE_SOURCE
like ok glibc, go off with the version matrix ๐ but yeah, if you're on an old glibc you gotta define the right macro or the function just won't exist for you. that's the rule.
๐ DESCRIPTION ๐
so chmod() and fchmod() system calls change a file's mode bits. and when we say "file mode" we mean the file permission bits plus the set-user-ID, set-group-ID, and sticky bits. the whole fit, head to toe โจ
these two calls are basically the same girl, different font. the only difference is how you point at the file:
chmod()changes the mode of the file specified whose pathname is given inpath, which is dereferenced if it is a symbolic link. (so it follows the symlink, got it.)fchmod()changes the mode of the file referred to by the open file descriptorfd. (fd girlies, this one's for you.)
the new file mode lives in mode, a bit mask you build by ORing together zero or more of these ๐
S_ISUID(04000) โ set-user-ID (set process effective user ID onexecve(2))S_ISGID(02000) โ set-group-ID (set process effective group ID onexecve(2); mandatory locking, as described infcntl(2); take a new file's group from parent directory, as described inchown(2)andmkdir(2))S_ISVTX(01000) โ sticky bit (restricted deletion flag, as described inunlink(2))S_IRUSR(00400) โ read by ownerS_IWUSR(00200) โ write by ownerS_IXUSR(00100) โ execute/search by owner ("search" applies for directories, and means that entries within the directory can be accessed)S_IRGRP(00040) โ read by groupS_IWGRP(00020) โ write by groupS_IXGRP(00010) โ execute/search by groupS_IROTH(00004) โ read by othersS_IWOTH(00002) โ write by othersS_IXOTH(00001) โ execute/search by others
note how the last three are the "others" bits and everybody forgets them. don't be that girl. ๐
ok now the rules, because permissions have rules:
the effective UID of the calling process must match the owner of the file, or the process must be privileged (on Linux: it must have the CAP_FOWNER capability). that's the vibe check for "are you allowed to do this at all."
if the calling process is not privileged (Linux: does not have the CAP_FSETID capability), and the group of the file does not match the effective group ID of the process or one of its supplementary group IDs, the S_ISGID bit will be turned off โ but this will not cause an error to be returned. so like, it silently yanks the bit. no drama, no errno, just gone. ๐
as a security measure, depending on the filesystem, the set-user-ID and set-group-ID execution bits may be turned off if a file is written. (On Linux, this occurs if the writing process does not have the CAP_FSETID capability.) on some filesystems, only the superuser can set the sticky bit, which may have a special meaning. for the sticky bit, and for set-user-ID and set-group-ID bits on directories, go look at inode(7).
on NFS filesystems, restricting the permissions will immediately influence already open files, because the access control is done on the server, but open files are maintained by the client. widening the permissions may be delayed for other clients if attribute caching is enabled on them. (NFS really said "eventually โจ".)
๐ฆ fchmodat() ๐ฆ
fchmodat() operates in exactly the same way as chmod(), except for the differences we're about to spill. it's the same girl with a dirfd.
if path is relative, then it is interpreted relative to the directory referred to by the file descriptor dirfd (rather than relative to the current working directory of the calling process, as is done by chmod() for a relative pathname).
if path is relative and dirfd is the special value AT_FDCWD, then path is interpreted relative to the current working directory of the calling process (like chmod()).
if path is absolute, then dirfd is ignored. (absolute path said "I don't need you" ๐)
flags can either be 0, or include the following flags:
AT_EMPTY_PATH(since Linux 6.6) โ ifpathis an empty string, operate on the file referred to bydirfd(which may have been obtained using theopen(2)O_PATHflag). in this case,dirfdcan refer to any type of file, not just a directory. ifdirfdisAT_FDCWD, the call operates on the current working directory. this flag is Linux-specific; define_GNU_SOURCEto obtain its definition.AT_SYMLINK_NOFOLLOWโ ifpathis a symbolic link, do not dereference it: instead operate on the link itself. (she said "I'm not following, I'm standing right here" ๐ง)
see openat(2) for an explanation of the need for fchmodat(). it's giving "why does this whole family exist".
โญ RETURN VALUE โญ
on success, zero is returned. on error, -1 is returned, and errno is set to indicate the error. classic. you know this dance. ๐ฉฐ
๐ฅ ERRORS ๐ฅ
depending on the filesystem, errors other than those listed below can be returned. so this list is the general one, not the whole alphabet.
the more general errors for chmod() are listed below:
EACCESโ search permission is denied on a component of the path prefix. (see alsopath_resolution(7).)EBADFโ (fchmod()) the file descriptorfdis not valid.EBADFโ (fchmodat())pathis relative butdirfdis neitherAT_FDCWDnor a valid file descriptor.EFAULTโpathpoints outside your accessible address space.EINVALโ (fchmodat()) invalid flag specified inflags.EIOโ an I/O error occurred.ELOOPโ too many symbolic links were encountered in resolvingpath.ENAMETOOLONGโpathis 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.ENOTDIRโ (fchmodat())pathis relative anddirfdis a file descriptor referring to a file other than a directory.ENOTSUPโ (fchmodat())flagsspecifiedAT_SYMLINK_NOFOLLOW, which is not supported.EPERMโ the effective UID does not match the owner of the file, and the process is not privileged (Linux: it does not have theCAP_FOWNERcapability).EPERMโ the file is marked immutable or append-only. (seeFS_IOC_SETFLAGS(2const).)EROFSโ the named file resides on a read-only filesystem.
yes there are twoEBADFs, twoENOTDIRs, and twoEPERMs. that's not a typo, that's the man page. handle it bestie ๐ซถ
๐ VERSIONS ๐
๐ C library/kernel differences
the GNU C library fchmodat() wrapper function implements the POSIX-specified interface described in this page. this interface differs from the underlying Linux system call, which does not have a flags argument. so glibc is out here doing extra work so you don't have to. icon behavior.
๐ glibc notes
on older kernels where fchmodat() is unavailable, the glibc wrapper function falls back to the use of chmod(). when path is a relative pathname, glibc constructs a pathname based on the symbolic link in /proc/self/fd that corresponds to the dirfd argument. (/proc girlies rise up ๐)
๐ STANDARDS ๐
POSIX.1-2024.
that's it. that's the standard. periodt.
๐ธ HISTORY ๐ธ
chmod()โ SVr4, POSIX.1-1988, 4.4BSD.fchmod()โ SVr4, 4.4BSD, SUSv1, POSIX.1-1996.fchmodat()โ POSIX.1-2008. Linux 2.6.16, glibc 2.4.AT_SYMLINK_NOFOLLOWโ POSIX.1-2008, glibc 2.32, Linux 6.5.
a whole family tree fr fr ๐ณ
๐ซถ SEE ALSO ๐ซถ
chmod(1), chown(2), execve(2), open(2), stat(2), inode(7), path_resolution(7), symlink(7)
and that's the page!! you ate, honestly. ๐
this whole thing lives in the System Calls Manual (that's the (2) in chmod(2), iykyk), and it's from Linux man-pages 6.20, dated 2026-02-08. go forth and fix your permissions bestie, and don't chmod 777 anything, I'm watching you ๐๐
โจ
๐ฆ where this page comes from
- arch rolling: man-pages 6.20-1 ยท BSD-2-Clause AND BSD-3-Clause AND BSD-4.3TAHOE AND BSD-4-Clause-UC AND GPL-1.0-or-later AND GPL-2.0-only AND GPL-2.0-or-later AND GPL-3.0-or-later AND LGPL-3.0-linking-exception AND LGPL-3.0-or-later AND LicenseRef-Public-Domain AND LicenseRef-UltraPermissive AND Linux-man-pages-1-para AND Linux-man-pages-copyleft AND Linux-man-pages-copyleft-2-para AND Linux-man-pages-copyleft-var AND MIT AND Spencer-94
- nixpkgs unstable: man-pages 6.20
- ๐ the page's license: Linux-man-pages-copyleft
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- Copyright 2006-2014, Michael Kerrisk <tmk.manpages@gmail.com>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms