chroot vs pivot_root: Why Containers Use pivot_root
chroot only changes how a process resolves paths, while pivot_root swaps the mount namespace's root and lets you unmount the old one. Why containers need that.

Both calls make a directory look like /, and for a long time that was enough for a jail. Containers use pivot_root(2) instead, and the reason is a single sentence: chroot(2) changes where a process starts resolving absolute paths, while pivot_root changes what the mount namespace's root is, and lets you throw the old root away. This post compares the two on what they do, what escapes them and when each is still the right tool. It describes Linux 6.12 behaviour as documented in the chroot(2) and pivot_root(2) man pages.
What chroot actually does
chroot(path) sets a per-process attribute: the root directory used to resolve absolute pathnames. After chroot("/srv/jail"), a lookup of /etc/passwd by that process reads /srv/jail/etc/passwd. Nothing else changes. The mount table is the same, and the old root filesystem is still mounted and reachable through any file descriptor that was open before, and through the process's current working directory if it stayed outside.
That last point is the classic escape. The chroot(2) man page says the call is not intended as a security mechanism and that a privileged process can break out. The old trick for a root process inside the jail is to create a subdirectory, chroot into it, and then walk .. upward: because the process's current directory is now above its root, the kernel never stops it at the boundary, and the walk reaches the real root. The call requires CAP_SYS_CHROOT, and a process that holds it can usually do this.
What pivot_root does
pivot_root(new_root, put_old) is a mount operation. It moves the current root mount of the calling process's mount namespace to the directory put_old and makes new_root the new root mount. The kernel updates the root and current directory of every process in the namespace that pointed at the old root, so it is not a per-process setting.
The man page lists the constraints, and they are why the call needs preparation:
new_rootandput_oldmust be directories.new_rootmust be a mount point, and so must not be on the same mount as the current root.put_oldmust be at or underneathnew_root.The propagation of the parent mount of
new_rootand of the current root must not beMS_SHARED.
After the call, the old root is mounted at put_old, and the standard last step is to unmount it. Once umount2("/put_old", MNT_DETACH) succeeds, no path and no inherited reference leads back to the host filesystem. That is the property chroot cannot offer.
Side by side
chroot | pivot_root | |
|---|---|---|
| Scope | One process and its children | The whole mount namespace |
| Changes | The process's root directory pointer | The root mount |
| Old root | Stays mounted and reachable | Moved to put_old, then unmountable |
| Needs a mount namespace | No | Effectively yes |
| Requires | CAP_SYS_CHROOT | CAP_SYS_ADMIN |
| Break-out by a privileged process | Well known | Not through the filesystem, once the old root is unmounted |
Needs new_root to be a mount point | No | Yes |
The "needs a mount namespace" row deserves a note. pivot_root works on the namespace the caller is in, so calling it without first unsharing would swap the root for every process on the host, including init. Runtimes always create a new mount namespace first with CLONE_NEWNS.
The fix for the shared-mount rule
The most common failure is EINVAL, and the usual cause is the last bullet in the list above. On a systemd host, / is a shared mount. A new mount namespace inherits those propagation settings, so pivot_root refuses to work until you make the tree private. The two steps, in C, go in this order.
#define _GNU_SOURCE
#include <sched.h>
#include <stdio.h>
#include <sys/mount.h>
#include <sys/stat.h>
#include <sys/syscall.h>
#include <unistd.h>
static int pivot_root(const char *new_root, const char *put_old) {
return syscall(SYS_pivot_root, new_root, put_old);
}
int main(void) {
if (unshare(CLONE_NEWNS) != 0) { perror("unshare"); return 1; }
/* make every mount private so the next calls cannot leak back */
if (mount(NULL, "/", NULL, MS_REC | MS_PRIVATE, NULL) != 0) {
perror("mount private"); return 1;
}
/* new_root must be a mount point: bind-mount the directory onto itself */
if (mount("/srv/jail", "/srv/jail", NULL, MS_BIND | MS_REC, NULL) != 0) {
perror("bind mount"); return 1;
}
mkdir("/srv/jail/old_root", 0700);
if (pivot_root("/srv/jail", "/srv/jail/old_root") != 0) {
perror("pivot_root"); return 1;
}
if (chdir("/") != 0) { perror("chdir"); return 1; }
if (umount2("/old_root", MNT_DETACH) != 0) { perror("umount2"); return 1; }
rmdir("/old_root");
execl("/bin/sh", "sh", "-c", "ls /; echo done", (char *)NULL);
perror("execl");
return 1;
}The glibc wrapper for pivot_root does not exist, so the program calls it through syscall(2), as the man page says to. Assume /srv/jail is a prepared directory holding a minimal root with /bin/sh and ls built statically, and that you run the program as root. Compile with gcc -O2 pivot.c -o pivot.
bin
dev
proc
doneThe listing is the jail's contents, and nothing from the host appears, because /old_root was detached and removed. Compare with a chroot version of the same program: after chroot("/srv/jail"), the host's / is still mounted, so a shell that kept an open directory descriptor on it could fchdir out. The pivot_root version leaves nothing to fchdir into.
Why runtimes still carry a chroot fallback
runc has a code path that falls back to chroot when pivot_root cannot be used. The documented case is a root filesystem that sits on a ramfs, such as an initramfs: pivot_root fails with EINVAL there because the root mount cannot be moved, and the runtime has a --no-pivot flag for it. The cost of that fallback is the weaker isolation described above, so it is a last resort. The OCI runtime specification does not mandate one call or the other; it asks for the container's root to be the one in root.path.
When chroot is still right
Use chroot when you want to run a build or an installer against a directory tree and you control everything inside it: chroot /mnt/target grub-install, a package builder's clean environment, a rescue shell from a live USB. The jail is for convenience, not containment, and chroot needs no mount namespace and no preparation. Use pivot_root whenever the thing inside is code you do not trust, or when you are building a container runtime, which is the situation this series is about.
Seeing the difference from /proc
The mount table shows what each approach leaves behind. Inside a chroot, the process still lives in the host's mount namespace, so /proc/self/mountinfo lists every host mount, including the ones outside its apparent root, and a process with CAP_SYS_CHROOT can still reach them. After pivot_root and a detach of the old root, the same file lists only what the new root contains.
sudo chroot /srv/jail /bin/sh -c 'wc -l < /proc/self/mountinfo'41The count is the size of the host's table, because the host's /proc was visible to the shell through a mount you prepared in the jail, and the number will differ on your machine. Run the same command inside the pivot_root program from earlier, with /proc mounted in the new namespace, and the count drops to the handful of mounts you created. That drop is the practical meaning of "the old root is gone": the kernel has no mount that leads back to it, so no path walk can reach it, with or without privileges inside the jail.


