$_ CLI In Depth
Learning progress 0%
lesson syscalls namespaces cgroups

chroot & pivot_root

5 min read Module 17: Syscalls, Namespaces & cgroups Updated Dec 5, 2026
Cheat Sheet
0:00 / 3:12

What the root directory is

Every process has a root directory. When a process opens /etc/passwd, the kernel starts at the process root and follows the path. Normally the process root is the real root of the filesystem, so the path means what it says. Change the process root, and the same path means a different file. A process that is inside a different root cannot name a file outside it using an absolute path.

The current directory is a separate setting. A relative path starts from the working directory, and that does not change with the root. This distinction explains several behaviors covered below.

chroot: change the root for one tree

The chroot system call sets the root directory of the calling process. Children inherit the root, so a command run under chroot sees the new root for everything it opens. The chroot program does the same thing and then runs a command. For example, chroot /some/dir /bin/sh starts a shell whose / is /some/dir.

To run chroot, the kernel requires the CAP_SYS_CHROOT capability, which normally means root. The lab user is unprivileged, so chroot fails with a permission error. The exercise records that status instead of requiring success, because the failure is informative: it shows which operations are reserved for privileged users.

BASH1 lines
chroot /workspace true 2> chroot-err.txt; echo "status=$?"

A failing chroot leaves the error text in the file. The status is what you should read first. On a system where the call works, the command still needs a true program inside the new root, so an empty directory gives its own error.

chroot is not a sandbox

A chroot does not isolate a process from the kernel, the network, or other processes. A process with root privilege inside a chroot can, with well-known techniques, leave it, because the process root is not stored with any extra restriction. Chroot is useful for building software against an alternative system tree, or for a service that starts as root, sets up its files, and then drops privileges. It is not a security mechanism by itself, and you should not rely on it for untrusted code.

Mounts and the root

chroot only changes the path lookup. A chroot directory that contains a bind mount of /proc still shows the host processes, and a directory that is the same as the host root shows everything. Real isolation comes from combining chroot with mount namespaces, so that the new root is a separate mount table. The next step is pivot_root.

The file /proc/self/mountinfo lists the mounts visible to your process, with the mount point in the fifth field. The lab reads that field, which is the root of your view when you run as an ordinary user.

BASH1 lines
awk '{print $5}' /proc/self/mountinfo | sort -u

pivot_root: change the root of a namespace

pivot_root swaps the root filesystem of the calling mount namespace. It takes a new root directory, which must itself be a mount point, and a directory under it where the old root is moved. After the call, the old root is still mounted, and it can be unmounted so the process has no path back to the host filesystem. Container runtimes use this sequence for that reason.

Because pivot_root changes the mount table, it only makes sense inside a private mount namespace. In the host namespace, moving the root would affect everything on the machine, so the kernel requires privilege and a suitable mount layout. The steps are: make the new root a bind mount, call pivot_root, change into the new root, and unmount the old one.

Tip

A chroot changes where paths start. pivot_root changes which mounts exist. Container runtimes use the second because it also removes the old root, which chroot leaves in place.

Reading the reference

The chroot(2) and pivot_root(2) pages on man7.org describe the exact requirements and the errors that each call can return. In the lab, use the --help text for the chroot command, and read the pages online for the rest. The key habit is the same as before: check the status and the file list, not the story.

What a new root needs

A chroot target must contain every file the program opens by an absolute path. A shell needs its own binary, the libraries it loads, and the configuration files that its startup reads. A missing piece is the most common reason a chroot seems broken: the program starts, prints an error about a missing file, and exits. The fix is to copy or bind mount the missing files into the root, not to change the program.

Why bind mounts are common

A bind mount makes a directory appear at a second place without copying it. Binding the host /usr into a new root shares the files, but it also shares any changes, so careful setups bind them read-only. A private mount namespace keeps the mounts made inside a container from reaching the host, which is one place where chroot becomes useful only together with a mount namespace.

Checking the root you are in

A quick way to see whether a process is inside a chroot is to compare the root directory of the process with the real root. On Linux, the directory /proc/self/root is a link to the process root, so reading it from inside the process shows where the process believes the root is. Compare the inode of that directory with the inode of the real root, and a difference means a changed root. Checks like this help when a service reports a missing file that exists on the host, because the answer is often that the service sees a different root than the one you are looking at.

What a container really is: namespaces, cgroups and a root

Visualizer

A container is not a special kind of object. It is ordinary processes inside namespaces that limit what they can see, cgroups that limit what they can use, and a root directory of their own.

1 / 4
ls -l /proc/PID/ns, one link per namespace (inode numbers are examples)
cgroupcgroup:[4026531835]
ipcipc:[4026531839]
mntmnt:[4026531841]
netnet:[4026531840]
pidpid:[4026531836]
useruser:[4026531837]
utsuts:[4026531838]

Each process points at one namespace of each kind. Two processes with the same inode number share that namespace. This shell uses the default, host-wide ones.

ls -l /proc/$$/ns
Terminal lab · 3 steps · real Linux in your browser

Check the root filesystem view

Try chroot as an unprivileged user, check your uid, and read the mount table.

01
Record a refused chroot
chroot changes the root directory of a process. Only root may run it, so an unprivileged shell usually gets an error. Record the result: ``bash chroot /workspace true 2> chroot-err.txt echo "status=$?" > chroot-try.txt `` Open chroot-try.txt. Any status line is a valid result for this step.
Show hint for step 1
The status line is what the check reads. The error text goes to chroot-err.txt.
02
Confirm you are not root
The chroot step fails for a regular user. Save your numeric user ID to prove it: ``bash id -u > uid.txt `` Open uid.txt. It should hold a number other than 0.
Show hint for step 2
id -u prints only the numeric user ID. Root always has ID 0.
03
List the mount points
Each line of /proc/self/mountinfo describes one mount. The fifth field is the mount point, relative to the root of your view. Save the unique mount points: ``bash awk '{print $5}' /proc/self/mountinfo | sort -u > mount-points.txt `` Open mount-points.txt. It should contain a line with a single slash, which is the root of your view.
Show hint for step 3
awk counts fields from 1. The fifth field holds the mount point, and the root directory appears as a single slash.
Check yourself Question 1 of 4
Concept Check Capability for chroot recall

Which capability does the chroot system call require?

Recap & Key Takeaways

A process resolves absolute paths from its root directory, and chroot changes that root for a process and its children.
chroot needs privilege and is not a sandbox, because a privileged process inside it can often leave.
pivot_root swaps the root of a mount namespace, and it removes the old root when the old root is unmounted.
Use /proc/self/mountinfo to see the mounts visible to a process, with the mount point in the fifth field.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue