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

Build: A Mini Container with unshare and chroot

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

The plan

The mini container has three parts. The first is a root filesystem, a directory that contains everything the program needs, such as a shell and its libraries. The second is a set of namespaces that hide the host's process list, host name, and mounts from the program. The third is a launch command that enters the root with chroot and starts the shell. The lab builds the first part and writes the second and third as a script, and it checks the syntax without requiring the namespaces to be created.

Build the root filesystem

A root filesystem needs directories with standard names: bin for programs, etc for configuration, and proc as the mount point for the process filesystem. Create them under a directory called rootfs, and write a host name file so that the box identifies itself.

BASH2 lines
mkdir -p rootfs/bin rootfs/proc rootfs/etc
echo mini > rootfs/etc/hostname

A real root filesystem also needs programs. A common choice is a static build of BusyBox, which packs a shell and the usual utilities into one binary that has no shared library dependencies. A static binary is the simplest choice for chroot, because the new root does not need copies of the libraries. The lab does not copy a binary, but the same layout works with one placed in rootfs/bin.

Ask for namespaces

The launcher calls unshare with the flags that create the isolation. --uts gives the box its own host name, --mount keeps mounts private to it, and --pid --fork gives it its own process numbering. The --fork flag matters, because the first process in a new PID namespace must be the one that becomes PID 1 inside it.

A hostname change inside the box should not affect the host. That is the simplest test of the UTS namespace, and it is cheap to run on a machine where you have the privilege.

Enter the root

After unshare, the launcher calls chroot to set the new root, and then runs the shell. The path /workspace/rootfs is the directory the shell sees as /. Inside, the shell can list bin and etc, and it cannot reach the host files above the root through an absolute path. Remember that chroot is not a full sandbox, so the namespaces and the directory layout do the isolation work together.

The script

A short script is easier to repeat than a long command. The script sets the strict options, calls unshare with the flags, and passes the chroot and shell at the end. Each line is one step, so a failure points at one step.

BASH3 lines
#!/bin/bash
set -eu
unshare --uts --mount --pid --fork chroot /workspace/rootfs /bin/sh

In the lab, running this script is optional. An unprivileged user usually cannot create these namespaces, and the error is a useful result. The step that matters is the syntax check, which bash -n performs without running any command.

Check before you run

bash -n reads a script, parses it, and reports syntax errors. It does not expand variables or run commands, so it is safe to use on a script that would fail or change the system. A clean result means the script is at least well formed, which catches missing quotes and unbalanced if statements early.

Note

A syntax check does not prove the script works. It does not check that the binaries exist, that the paths are right, or that you have privilege. Those are runtime checks, and the step that follows the build is to run the script on a machine where the privilege exists.

What you built

The directory is the filesystem, the flags are the isolation, and the command is the process. Real container runtimes add cgroup limits, a pivot_root instead of chroot, dropped capabilities, and a seccomp filter. Each addition is one more kernel setting, and you now know where to look for each one.

Testing the result

Once the script runs on a machine that allows namespaces, test each feature separately. Inside the box, run hostname and compare it with the host. Run ps and check that the process list is short. List the proc directory and check that it is mounted. Each test looks at one kernel setting, so a failure points at one flag.

Common failures

An error that says operation not permitted means the namespace or the chroot needs privilege or a kernel setting. An error that says no such file or directory names a missing path, often a shell or a library inside the root. A shell that starts but prints nothing usually lacks the terminal device, because /dev was not populated. Naming the failure before you change the script saves time, and the list above covers most first attempts.

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

Lay out a mini container

Create a root filesystem skeleton, write a launcher script with unshare and chroot, and check its syntax.

01
Create the root filesystem skeleton
A container root needs a few directories. Create them under rootfs and give the box a hostname file: ``bash mkdir -p rootfs/bin rootfs/proc rootfs/etc echo mini > rootfs/etc/hostname `` Run ls -R rootfs to confirm the directories exist.
Show hint for step 1
mkdir -p creates the directories and their parents without complaint if they already exist.
02
Write the launcher script
Write run-box.sh. It asks for new namespaces with unshare and then runs a shell inside rootfs with chroot. Running it is optional here, because an unprivileged user usually gets a permission error: ``bash cat > run-box.sh <<'EOF' #!/bin/bash set -eu unshare --uts --mount --fork chroot /workspace/rootfs /bin/sh EOF `` The file must mention both unshare and chroot. Keep the script readable, one command per line.
Show hint for step 2
The file must contain the words unshare and chroot. The heredoc delimiter is quoted so nothing inside expands.
03
Check the script without running it
bash -n reads a script and reports syntax errors without running any command. Run it and save a note when it passes: ``bash bash -n run-box.sh && echo "syntax ok" > syntax.txt `` Open syntax.txt. It should say syntax ok.
Show hint for step 3
The && runs echo only when bash -n succeeds, so the file is written only for a clean script.
Check yourself Question 1 of 4
Concept Check Host name flag recall

Which unshare flag gives the box its own host name?

Recap & Key Takeaways

A mini container is a root directory, a set of namespaces, and a launcher that calls chroot inside them.
unshare --pid needs --fork, so that the command becomes PID 1 in the new namespace.
bash -n checks a script's syntax without running it, which is a safe first check for a launcher.
Real containers add cgroup limits, pivot_root, dropped capabilities, and seccomp on top of this basic layout.
Was this lesson helpful? Spot an issue or have a suggestion? Let us know on GitHub.
Report an issue