Understanding Linux Namespaces
Understanding Linux Namespaces
Feb 21
In this article we will explain what Linux namespaces are and how do we take
Let’s start!
What Is a Namespace?
resources for a set of processes. In some way, they are a sort of implementation of the
What are the main advantages in the use of namespaces? The main advantages are
two:
•Isolation of resources
One troublesome process won’t be taking down the whole host, it’ll only affect
•Security
The other advantage is that a security flaw in the process or processes running
under a given namespace, won’t give access to the attacked to the whole system.
Whatever he/she could do, will always be contained within the boundaries of that
namespace! This is why it’s also very important to avoid running our processes
processes and provide resource isolation for them. Some of the most popular and
kernel. You might be wondering, how is possible that we can run Docker or Podman in
Well, the answer is simple: Both Docker and Podman run a virtual machine in your
local machine (OSx or Windows) to be able to make use of namespaces and any other
For example, we are running Podman in our local machine, in order to make it work we
had to initialise and start a podman machine first by running the following:
$ podman machine init
This initialises and starts a virtual machine in your local machine, which runs a linux
operating system. How can we get the details about this machine to make sure that it’s
actually a virtual machine? We can get this information by running this command:
This will inspect the default machine we have currently running in podman. The output
which runs Fedora Core OS as the operating system. If you want to run a different
Linux version, our article “Run Ubuntu on Max Using QEMU” might be useful.
The way it works in Docker is very similar to what Podman does, although Docker uses
VirtualBox instead. This is the default virtual machine that comes with Docker Desktop,
Now that we know what a Linux namespace is, you could be thinking: How can I create
a new namespace?
this:
$ unshare --help
Usage:
Options:
You can see how most of the first few options allow us to specify different types of
namespaces, these are the different types of namespaces we have in Linux systems.
We will see cgroups in more detail in a future article, as it takes a very important role
Types of Namespaces
Each type of namespace is different and it provides isolation for different resources in
our system.
If we check the namespaces in the Linux manual pages, we can see a list of namespace
types:
POSIX message
queues
devices,
stacks, ports,
etc.
monotonic
CLONE_NEWUSER user_namespaces(7) User and group
IDs
NIS domain
name
clocks
IDs
NIS domain
name
We won’t get into too much detail, but you can clearly see how every namespace will
isolate a different resource in our system. Therefore, we can see briefly what the
Contains an independent set of user IDs and group IDs that can be assigned to
processes.
•PID namespace
Contains its own set of process IDs (PIDs). Every time we create a new namespace,
the process will get assigned PID 1. Every child process created in the new
•Mount namespace
They allow the management of mount points in our system. Doing unmount in our
new namespace won’t have any effect on the main host, as every new mount will
•Network namespace
Virtualises the network stack for the new namespace. This means that the new
namespace will have its own virtual interface/s, private IPs, IP route table, sockets,
etc.
namespace.
•Time namespace
This namespace was released quite recently in Linux (2020), it allows having
different system times within our system by specifying different time namespaces.
Introduced in 2016 as part of Linux release 4.6, limits the resource usage in our
system (cpu, memory, disk, etc) for a particular group of processes (under this
going to show a simple example to understand the power that namespaces bring to
us.
What exactly are we doing here? What are we telling the Kernel to do? Let’s go step by
If we check each of these options in the manual here, we can see each of these options
•— pid: Create a new pid namespace. (Will fail if — fork is not specified)
•— map-root-user: Wait to start the process until the current user (running
unshare command) gets mapped to the superuser in the new namespace. This
allows having root privileges within the namespace, but not outside of that scope.
•— mount-proc: Mount /proc filesystem in the new namespace and create a new
mount, this is to be able to have different processes with same process IDs in both
namespaces.
Let’s see what this implies. First of all, if we list the processes in the new namespace,
we only see the new processes running in the new namespace and not the processes
You will also notice that our processes are running as root within the new
namespace, this is what the –map—root-user does. The current user is root within
this namespace only, this use won't be able to do any harm to any of the external
Lastly, we can see how the IDs of the processes are 1 and 2, this is because we create a
It’s also worth mentioning that the unshare command we just ran is equivalent to the
This will be very familiar to those used to work with containers, as this is normally used
to gain access through a terminal inside the container. Now that we’ve mentioned
containers, let’s see what the namespaces role is inside of Linux containers.
Namespaces Role in Containers
Namespaces have been available in the Linux kernel since 2002, however, the use of
them was restricted to those with a very advanced knowledge of Linux systems. Linux
namespaces and containers were made “popular” by Docker, when it made possible a
wide adoption of containers across the sector for anyone with a minimum (or zero)
The use of namespaces is not transparent to the users, and that’s probably why most
There’s a considerable number of people in the sector that see containers as some sort
of virtual machine that does not contain the kernel, but shares it with the host instead.
This is a wrong understanding of what a container is, we’ll see why very soon.
Containers create an illusion that could make you think that you’re actually running a
virtual machine on your host, but this is far from the truth. When you run a virtual
machine on your host , you are actually booting a new OS distribution on your
machine, with the difference being that this OS will make calls to a middleware
provided by the virtualisation engine you use (Virtual Box, QEMU, etc).
This middleware translates every kernel call inside your virtual machine to a system call
that your host can understand. This is what mades possible, for example, to run
On the other hand, a Linux container makes use of Linux namespaces to provide this
illusion of running a different operating system. When we create an image based a any
known Linux distribution, we are actually mounting its filesystem, giving us the
impression of being on a new operating system. But actually we’re just inside of a new
namespace!
Conclusion
In this article we have seen what Linux namespaces are and the very important role
they play in distributed systems nowadays. We’ve also tried to clarify one of the biggest
misconceptions around Linux containers with regards to Linux namespaces and virtual
machines.
We really hope that every concept is now clearer to you. If you are still confused and
this is unclear, don’t worry, this can happen. Just re-read these concepts and eventually
That’s all from us today! We really hope you’ve enjoyed reading this article as much as