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.
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.
awk '{print $5}' /proc/self/mountinfo | sort -upivot_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.
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.