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 in path, 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 descriptor fd. (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 on execve(2))
  • S_ISGID (02000) โ€” set-group-ID (set process effective group ID on execve(2); mandatory locking, as described in fcntl(2); take a new file's group from parent directory, as described in chown(2) and mkdir(2))
  • S_ISVTX (01000) โ€” sticky bit (restricted deletion flag, as described in unlink(2))
  • S_IRUSR (00400) โ€” read by owner
  • S_IWUSR (00200) โ€” write by owner
  • S_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 group
  • S_IWGRP (00020) โ€” write by group
  • S_IXGRP (00010) โ€” execute/search by group
  • S_IROTH (00004) โ€” read by others
  • S_IWOTH (00002) โ€” write by others
  • S_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) โ€” if path is an empty string, operate on the file referred to by dirfd (which may have been obtained using the open(2) O_PATH flag). in this case, dirfd can refer to any type of file, not just a directory. if dirfd is AT_FDCWD, the call operates on the current working directory. this flag is Linux-specific; define _GNU_SOURCE to obtain its definition.
  • AT_SYMLINK_NOFOLLOW โ€” if path is 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 also path_resolution(7).)
  • EBADF โ€” (fchmod()) the file descriptor fd is not valid.
  • EBADF โ€” (fchmodat()) path is relative but dirfd is neither AT_FDCWD nor a valid file descriptor.
  • EFAULT โ€” path points outside your accessible address space.
  • EINVAL โ€” (fchmodat()) invalid flag specified in flags.
  • EIO โ€” an I/O error occurred.
  • ELOOP โ€” too many symbolic links were encountered in resolving path.
  • ENAMETOOLONG โ€” path is 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()) path is relative and dirfd is a file descriptor referring to a file other than a directory.
  • ENOTSUP โ€” (fchmodat()) flags specified AT_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 the CAP_FOWNER capability).
  • EPERM โ€” the file is marked immutable or append-only. (see FS_IOC_SETFLAGS(2const).)
  • EROFS โ€” the named file resides on a read-only filesystem.
yes there are two EBADFs, two ENOTDIRs, and two EPERMs. 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
  • ๐Ÿ’– girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms