chroot

The following are the building blocks of a container

  • chroot
  • namespaces
  • cgroups

Containers are lightweight compared to virtual machines, they make use of existing resources on the server, just the process is isolated, rather than emulating whole operating system.

chroot is used to isolate the directories required for the container. It separates the directories from the host root user.

chroot /new-root bash, this command fails if you try right away, that’s because new-root doesn’t have access to the bash yet.

To move bash to the new root, you would also need to move all the libraries that bash is relying on.

ldd /bin/bash command can be used to display all the libraries that bash is relying on, you need to repeat the steps for the other utility commands

Once you run chroot it changes the root directory to the specified directory. So your root user now thinks, the root directory is the new directory that you just navigated.

Namespaces

The following command can create a contained environment, so user cannot kill processes on the host machine itself, each root user on the system uses an isolated environment so they cannot interfere with each other.

unshare --mount --uts --ipc --net --pid --fork --user --map-root-user

mount -t sysfs none /sys mount -t tmpfs none /tmp mount -t proc none /proc