chroot(2)

โœจ chroot(2) โ€” change root directory โœจ

okay bestie, welcome to the chroot(2) page in the System Calls Manual ๐Ÿ’– this one is tea, this one is drama, this one gets people in trouble, so let's get into it ๐Ÿซถ


๐ŸŽ€ NAME ๐ŸŽ€

chroot โ€” change root directory. that's it, that's the whole name. short, iconic, dangerous, we love her โœจ


๐Ÿ“š LIBRARY ๐Ÿ“š

Standard C library, aka libc, aka link it with -lc ๐Ÿ’…


๐Ÿฆ‹ SYNOPSIS ๐Ÿฆ‹

#include <unistd.h>

int chroot(const char *path);

โœจ Feature Test Macro Requirements for glibc โœจ

(peep feature_test_macros(7) for the whole lore, bestie)

chroot():
    Since glibc 2.2.2:
        _XOPEN_SOURCE && ! (_POSIX_C_SOURCE >= 200112L)
            || /* Since glibc 2.20: */ _DEFAULT_SOURCE
            || /* glibc <= 2.19: */ _BSD_SOURCE
    Before glibc 2.2.2:
        none

like. the era of glibc you're living in literally changes your requirements. timeline queen behavior ๐Ÿ˜ญ


๐Ÿ’– DESCRIPTION ๐Ÿ’–

so chroot() changes the root directory of the calling process to whatever you put in path. that directory becomes the one used for pathnames that start with /. and here's the thing โ€” it's inherited by all the children of the calling process. the whole family gets the new root, no cap ๐Ÿ‘‘

only a privileged process may call chroot(). on Linux that means a process with the CAP_SYS_CHROOT capability in its user namespace. everyone else? denied. sorry babes ๐Ÿ’€

now here's the part that's giving reality check: this call changes one ingredient in the pathname resolution process and does nothing else. that's the entire plot. in particular, it is not intended to be used for any kind of security purpose โ€” neither to fully sandbox a process nor to restrict filesystem system calls. periodt.

in the past, chroot() has been used by daemons to restrict themselves before passing paths supplied by untrusted users to system calls like open(2). but if a folder is moved out of the chroot directory, an attacker can exploit that to get out of the chroot directory as well. the easiest way to do that is to chdir(2) to the to-be-moved directory, wait for it to be moved out, then open a path like ../../../etc/passwd. likeโ€ฆ that's it. that's the whole escape. so rude ๐Ÿ˜ญ

a slightly trickier variation also works under some circumstances if chdir(2) is not permitted. if a daemon allows a "chroot directory" to be specified, that usually means that if you want to prevent remote users from accessing files outside the chroot directory, you must ensure that folders are never moved out of it. that's the assignment, bestie ๐ŸŽ€

this call does not change the current working directory, so after the call '.' can be outside the tree rooted at '/'. in particular, the superuser can escape from a "chroot jail" by doing: c mkdir foo; chroot foo; cd ..; yeah. the root user just walked out. slay in the worst way ๐Ÿ™ˆ

also โ€” this call does not close open file descriptors, and such file descriptors may allow access to files outside the chroot tree. so if you left a door open before you locked up, the door stays open. highkey a plot hole ๐Ÿ”ฅ


โญ RETURN VALUE โญ

  • success โ€” 0 is returned. it's giving we did it ๐Ÿซถ
  • error โ€” -1 is returned, and errno is set to indicate the error. it's giving not us ๐Ÿ’€

๐ŸŒธ ERRORS ๐ŸŒธ

depending on the filesystem, other errors can be returned. the more general errors are listed below, queen ๐Ÿงš

  • EACCES โ€” search permission is denied on a component of the path prefix. (see also path_resolution(7).) so you're not allowed to peek in the corridor ๐Ÿ‘€
  • EFAULT โ€” path points outside your accessible address space. that memory is not yours, bestie, hands off ๐Ÿ’…
  • EIO โ€” an I/O error occurred. the disk said no ๐Ÿ˜ญ
  • ELOOP โ€” too many symbolic links were encountered in resolving path. the symlinks are linking in circles, it's giving toxic situationship ๐Ÿ”„
  • ENAMETOOLONG โ€” path is too long. bestie that path is unwell ๐Ÿ’€
  • ENOENT โ€” the file does not exist. she's not there, hun ๐Ÿ˜”
  • ENOMEM โ€” insufficient kernel memory was available. out of memory, out of vibes ๐Ÿ˜ฉ
  • ENOTDIR โ€” a component of path is not a directory. you brought a file to a directory party ๐ŸŽ€
  • EPERM โ€” the caller has insufficient privilege. you tried and the kernel said who do you think you are โœ‹๐Ÿ‘‘

๐Ÿ‘‘ STANDARDS ๐Ÿ‘‘

None. not standard. not standardized. free-spirited. no rules. a true main character ๐ŸŒˆ


๐Ÿ•ฐ๏ธ HISTORY ๐Ÿ•ฐ๏ธ

SVr4, 4.4BSD, SUSv1 (marked TO BE WITHDRAWN), SUSv2 (marked LEGACY). and heads up โ€” this function is not part of POSIX.1-2001. she's an elder but she's not posix-certified, iykyk ๐Ÿ’œ


๐Ÿ’… NOTES ๐Ÿ’…

  • a child process created via fork(2) inherits its parent's root directory. family tree, literally ๐ŸŒณ
  • the root directory is left unchanged by execve(2). new program, same root, she's consistent โœจ
  • the magic symbolic link, /proc/pid/root, can be used to discover a process's root directory. see proc(5) for details ๐Ÿ”
  • FreeBSD has a stronger jail() system call. good for them honestly ๐Ÿ–๏ธ

๐Ÿ”ฅ SEE ALSO ๐Ÿ”ฅ


and that's a wrap babes ๐Ÿ’– this page was pulled from Linux man-pages 6.20, dated 2026-02-08 โ€” go forth, root responsibly, and remember: chroot is not a security boundary no matter how many daemons pretend otherwise ๐Ÿซถโœจ stay safe, stay privileged, and don't cd .. on the way out ๐Ÿ‘‘๐ŸŽ€

๐Ÿ“ฆ 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