Linux Commands

Working With the nsenter Linux Command

One way of encapsulating the host system when running different processes is by using containers. For DevOps, containers have become the way forward for application deployment as they guarantee the security and isolation of the entire system and resources. The isolation of the system resources works via the Linux namespaces. A container works independently, and it can’t access the host resources.

The kernel can allocate and restrict the resources that the processes running can access through the namespaces. The namespaces create isolation, and containers only view processes. To easily work with the containers and namespaces, the nsenter Linux command is required, and we will see how to use nsenter to inspect and execute programs in the namespaces.

How to Inspect Linux namespaces using nsenter

Before we dive into using nsenter, we need to understand Linux containers and namespaces.

What are Linux Containers?

With growing technology, the demand to develop more scalable and secure applications has stretched the need to use containers. Linux containers can best be said to be a technology that allows packaging and isolating applications and resources in their runtime environment, away from the system. Besides, you can also create containers containing namespaces for different processes on your Linux system using various tools, such as podman or docker.

For this example, we will create a new container using docker, as shown in the image below.

Exit the container and list the available containers from the host system. You will note that the container we created gets listed, including its container ID.

$ sudo docker ps -l

Alternatively, if you are using podman Red Hat offers a container in its catalog that you can access using the command below.

$ podman run --name namespace-demo -it /bin/bash

Once you have the container created, list the process id of the container created using the command below.

$ runc list

In the output, you should note the process id. In our case, the ID is 39782.

Using the process id above, you can get the namespaces associated with it using lsns or nsenter.

To use lsns, use the syntax below.

$ lsns -p <process-id>

Unlike lsns, the nsenter offers more options, giving you more control.

Using nsenter with Linux namespaces

Some of the common options that you can use with nsenter include:

1. -t: this flag specifies the target process id.

2. -u: it is used to enter the process’s namespace, and if no namespace is entered, it will use that of the target process.

For instance, to get the hostname of the namespace with the id 39782, the command would be:

$ nsenter -t 39782 -u hostname

3. -a: used to enter all the available namespaces. In our case, we only have one. If we run the command below, you will note we are inside the container.

You can log out by typing exit.

4. -n: the flag is used to enter the network namespace. Only the network information for the given namespace gets viewed.

If you were to view the same network details from the complete system, you would notice that there is isolation.

The isolation extends to the IP route, and we can also get the IP route for the namespace using the command below.

$ nsenter -t 39782 -n ip route

In the image above, you can notice how the first output is for the namespace of the given target process id, but the second output is the ip route for the complete system.

5. -p: using nsenter, you can also enter the PID namespace using the -p flag. For this to work, you should have the top or ps commands installed inside the container.

If you created the podman container, use the command below to install the procps-ng package, which installs top and ps to help view the currently running processes using the ps -ef Linux command.

$ yum install procps-ng

You can now enter the PID namespace using the command below.

$ nsenter -t 39782 -p -r ps -ef

The -r sets the root directory, and if none is specified, like in the command below, it uses that of the target process ID.

The output gives the currently running process in the container.


As a developer, you can’t evade using Linux containers. The best approach is to equip yourself with tools that enable you to interact with the different namespaces for processes in separate containers. Thanks to containers, the isolation of processes running on Linux is possible. We’ve covered how to use the nsenter Linux command to enter the namespaces of different processes in any container. Keep practising and trying other tactics to understand the tool better.

About the author

Denis Kariuki

Denis is a Computer Scientist with a passion for Networking and Cyber Security. I love the terminal, and using Linux is a hobby. I am passionate about sharing tips and ideas about Linux and computing.