π§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,coreutilsRoot or
sudoaccess recommended
Install required tools:
sudo apt update
sudo apt install util-linux procps iproute2 -yLinux namespaces control what a process can see. CGroups (see other section Linux CGroups) 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

β 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.
πΉ Lab 4: Inspect Namespace Links in /proc
/procYouβ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
unshareandnsenterfor namespace explorationExplored UTS, PID, MNT, NET, and combined namespaces
Simulated container isolation mechanisms using native Linux features
Last updated