Skip to content

nsenter: enter another process namespaces

nsenter from iproute2 is a lightweight utility for entering any namespace of a running process without stopping it. It’s the go-to tool when you need to inspect or modify a process’s PID, network, mount, or IPC environment from the host, especially when docker exec isn’t an option.

1. What is nsenter and why it’s needed

nsenter lets you enter (enter) any of the namespaces (PID, NET, MNT, IPC, USER) of a target process identified by PID. Unlike screen or tmux, no process stop or restart is required—you simply overlay your execution environment on its existing runtime. A typical scenario: an admin checks routing or iptables rules inside a container’s network namespace without docker or kubectl access, but with the process PID available.

2. Basic syntax and flags

Main syntax:

nsenter [options] [command]

Key flags for namespace selection:

FlagShortDescription
--target-tPID of the process whose namespace to enter
--mount-mSwitch to mount namespace
--net-nSwitch to network namespace
--pid-pSwitch to pid namespace (default uses target’s namespace)
--user-uSwitch to user namespace
--ipc-iSwitch to ipc namespace

Example entry into the network namespace of process PID 3456:

sudo nsenter -t 3456 -n -i

If no command is specified, nsenter starts a shell inside the selected namespace. Flags can be combined: -t PID -m -n -i enters mount, network, and ipc namespaces of the target process simultaneously. Long forms (--target, --mount, etc.) are also supported, but short variants are preferred for speed in operational notes.

Tip

To avoid typing PID repeatedly, retrieve it with pgrep or ps aux | grep pattern, then pass the result to nsenter.

3. Practical usage examples

A common case is network diagnostics inside a running container when docker exec is blocked or the container was started with --network=container::

# Enter network namespace of a process by PID
sudo nsenter -t $(pgrep -f my-container) -n -i

# After entry, check interfaces, routing tables
ip addr show
ip route show

Another scenario—checking mount points of a process whose filesystem differs from the host:

sudo nsenter -t 3456 -m -- ls /proc/3456/root

It also happens that after a broken SSH session, the process stays alive but its session is gone. Entering its network namespace lets you recheck socket state or rules without restarting the process.

4. nsenter vs ip netns exec

ip netns exec from the same iproute2 package works with named network namespaces created via ip netns add. It suits pre‑prepared lab environments, virtual routers, or dedicated orchestrated networks via libvirt or systemd-networkd.

Characteristicnsenterip netns exec
Entry pointPID of target processName of network namespace
FlexibilityCan enter any namespace (MNT, IPC, USER, etc.)Only network namespace
Naming requiredNo—PID sufficientYes—namespace must be added beforehand
Typical caseOn‑the‑fly debugging of existing processes, containers without dockerManaging isolated networks, VPN gateways

In short: use nsenter when you need to jump into a namespace of a running process “on the fly.” Use ip netns exec when you have pre‑created named networks managed at the system level.

5. Common use cases

  • Debugging container network isolation: check iptables, nft rules or routes inside a process’s namespace without restarting the container.
  • Inspecting mount points: view /proc/<pid>/mountinfo or run commands inside mount namespace to locate broken references.
  • Recovery after SSH failure: enter the network namespace of a process whose connection dropped, collect logs or re‑bind sockets.
  • Audit and security: verify which namespaces a process sees and confirm compliance with isolation policies.
  • Migration and cloning: enter pid namespace to verify process identity when moving between hosts.

Final note on security: always confirm you have permission to enter the target process’s namespace. Running nsenter as root grants full access, so use sudo or appropriate cap_sys_admin capabilities following your infrastructure’s policy.