🐧Linux Namespaces Labs

🎯 Objective

Learn how Linux namespaces isolate system resources by creating isolated environments using the unshare and nsenter tools.

🧰 Prerequisites

  • Linux system (Ubuntu/Debian/CentOS)

  • Tools: util-linux (unshare, nsenter), procps, iproute2, coreutils

  • Root or sudo access recommended

Install required tools:

sudo apt update
sudo apt install util-linux procps iproute2 -y

Linux namespaces control what a process can see. CGroups (see other section Linux CGroupsarrow-up-right) control the resources that a process can use. Both mechanisms form the basis of Containers.

Linux currently provides the following namespaces:

  • Unix Timesharing System (UTS): This namespace is responsible for the hostname and domain names.

  • Process IDs

  • Mount points

  • Network

  • User and group IDs

  • Inter-process communications (IPC)

  • Control groups (cgroups)

You can see all namespaces on your machine using the lsns command. Try also to run this command using root sudo lsns, then you are able to see more details.

By using the tool unshare you may run a process with some namespaces unshared from the parent (i.e., simulating a linux container).


πŸ”Ή Lab 1: Isolating the hostname using a UTS namespace

So let's try to use the UTS (Unix Timesharing System) namespace to isolate the hostname:

This opens a root shell in a new UTS namespace.

Let's try to set an isolated hostname in this shell:

In the output you should see that it really has set a new hostname

So, in the current shell we have our own hostname isolated by the UTS namespace. Now open a new terminal and check the hostname.

You will notice that the host still has its original name.

βœ… Hostname change is isolated.

Finally, just exit the root shell by typing exit.


πŸ”Ή Lab 2: Isolating the process id using a PID namespace

You can also use the PID namespace to isolate the process id.

Only a few processes show now, starting from PID 1. This simulates what a container would see: A trimmed-down process view.

πŸ” Excursus: Breakdown of Flags

flags_breakdown

βœ… Simulates container-like PID isolation.

Finally, just exit the root shell by typing exit.


πŸ”Ή Lab 3: Isolating the network using a network namespace

First check the current network interfaces in the current shell:

You should see several interfaces here (i.e. lo, eth0 and docker)

Now open a new shell in an isolated network namespace:

This should show only the lo interface (loopback). Network is isolated. You can’t reach the outside world.

βœ… Network isolation achieved.

Finally, just exit the root shell by typing exit.


You’ll see symbolic links to namespace descriptors like mnt, uts, pid, etc. These are the namespaces of the current shell process id (echo $$ returns the PID of the current shell).

βœ… Observe mnt, uts, pid, net, and other namespace descriptors.


πŸ”Ή Lab 5: Isolating the mount namespace

Now, in another terminal, run:

Our new mount doesn’t appear outside the namespace.

βœ… Mount is isolated from the host view.

Finally, just exit the root shell by typing exit.


πŸ”Ή Lab 6: Combine multiple Namespaces

Now check in the same terminal:

All outputs reflect isolation from the host. You will see that the hostname is isolated, the process id starts at 1, the network is isolated and the mount namespace is also isolated.

βœ… Fully isolated namespace context.

Finally, just exit the root shell by typing exit.


πŸ”Ή Lab 7: Enter Another Process’s Namespace

Another option is the nsenter tool that basically is intended to run a program with namespaces of other processes.

In the new isolated shell set a new hostname

In another terminal run:

Now you’ve entered another process’s UTS namespace.

if you check for the hostname you should get the isolated hostname testhost.

nsenter opens up attack potential in containers as it is possible to enter a host namespace and perform a container escape. To mitigate this, nsenter command should not be installed inside containers and it should not be denied for attackers to install this in your container as well.

βœ… Join and inspect the other process's namespace.

Finally, just exit both sub shells by typing exit.


βœ… Wrap-Up

  • Namespaces are the foundation of container isolation.

  • Learned to use unshare and nsenter for namespace exploration

  • Explored UTS, PID, MNT, NET, and combined namespaces

  • Simulated container isolation mechanisms using native Linux features


Last updated