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 โ
0is returned. it's giving we did it ๐ซถ - error โ
-1is returned, anderrnois 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 alsopath_resolution(7).) so you're not allowed to peek in the corridor ๐EFAULTโpathpoints 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 resolvingpath. the symlinks are linking in circles, it's giving toxic situationship ๐ENAMETOOLONGโpathis 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 ofpathis 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. seeproc(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
- Copyright 1992, Drew Eckhardt <drew@cs.colorado.edu>
- ๐ girlified by deepseek-ai/DeepSeek-V4.1-Flash on : a language model's rewrite of the original, under its terms