Introducing the “Sovereign Snippets” Series: Most of our time is spent in complex configurations, but the real power of a Linux system often hides in the short, punchy commands we use every day to maintain control over our hardware. I’m starting this series to document the specific “one-liners” I use to audit, secure, and manage my systems. No fluff – just functional tools for those who prefer the CLI over a GUI.

The “Works on the Host” Illusion

In Snippet #15, we built a network quarantine room using ip netns to trap a VPN and isolate specific applications. That is great when you are the architect building the walls. It is considerably less amusing when you are troubleshooting a broken container or a hardened systemd unit and you can’t figure out why its traffic is disappearing into the void.

You check the host. The host has a clean default route. The interface is up. The firewall looks fine. Meanwhile, the application inside the container is screaming about unreachable networks, empty routing tables, or mysteriously closed ports.

A healthy host namespace proves surprisingly little about the smaller universe occupied by a failing process.

When this happens, the usual instincts are terrible: either you try to exec into a stripped-down container image that contains little beyond an application binary and misplaced confidence (and then waste ten minutes trying to install iproute2 and ss inside a container that has no package manager), or you just restart the service over and over until causality becomes folklore.

There is a better way. Using nsenter, you can borrow the diagnostic tools already installed on your host filesystem and execute them directly inside the target process’s network namespace.

Step 1: Locate the Target Process

Before you can step into a process’s network stack, you need its PID. If you are investigating a systemd service (such as a sandboxed worker or a container runtime unit), grab its main process ID directly:

pid=$(systemctl show \
  --property=MainPID \
  --value thumbnail-worker.service)

printf 'Target PID: %s\n' "$pid"

(Watch out for 0. If the service is inactive, dead, or a oneshot unit without a persistent main process, MainPID will return 0, and there is nothing to inspect).

Step 2: Verify the Alternate Universe

Before running diagnostics, confirm that the process actually lives in a separate network namespace and isn’t just sharing the host’s stack.

First, ask lsns (from util-linux) to list the namespaces attached to that task:

sudo lsns --task "$pid"

(Note: Always run lsns with sudo during an investigation. Unprivileged lsns output is often incomplete because it gathers its data from /proc, where kernel permissions will quietly hide namespaces you don’t own).

If you want a quick, unambiguous confirmation, compare the network namespace symlink of your current shell against the target PID:

readlink /proc/self/ns/net
sudo readlink /proc/"$pid"/ns/net

The kernel tracks namespaces by inode number (e.g., net:[4026532281]). If the numbers don’t match, you have confirmed they are living in completely different network realities.

Step 3: Enter the Network Namespace (And Nothing Else)

Now we use nsenter to slip into the target’s network stack. We keep the scope deliberately narrow by passing --net explicitly. Because we are not entering the target’s mount namespace, the commands we run (ip, ss, nft) still come straight from the host’s /usr/sbin—meaning you get your full arsenal of host tools inside even the most barren distroless container.

Check the Interfaces

See what interfaces the process actually sees (and whether they are even UP):

sudo nsenter \
  --target "$pid" \
  --net \
  -- ip -brief address

Check the Routing Table

Verify whether it has a default gateway or if a missing route is stranding its packets:

sudo nsenter \
  --target "$pid" \
  --net \
  -- ip route show

Check Listening Sockets

See what is actually bound and listening from the namespace’s perspective (building on our ss habits from Snippet ):

sudo nsenter \
  --target "$pid" \
  --net \
  -- ss -lntup

Check Local Firewall Rules

Because nftables rulesets are namespace-specific, a clean firewall on the host doesn’t mean the container’s namespace isn’t dropping its own packets:

sudo nsenter \
  --target "$pid" \
  --net \
  -- nft list ruleset

The Caveats (The Tax on Precision)

nsenter is a surgical instrument. Like any surgical instrument, using it carelessly will either mislead you or cause bleeding.

  1. Keep --net Explicit: Never lazily pass --all when you only care about networking. --all drags you into the target’s mount, PID, user, IPC, and UTS namespaces. Suddenly your host binaries disappear, paths break, and you introduce a layer of complexity that this investigation does not need.
  2. The DNS Hybrid Trap: Joining only the network namespace does not reproduce the target’s filesystem, environment variables, credentials, cgroup, or security policy. This creates a classic debugging trap with DNS: container-specific DNS settings usually live in a separate mount namespace (/etc/resolv.conf). If you run the host’s curl using only nsenter --net, you will execute a request using the target’s network interfaces paired with the host’s /etc/resolv.conf. That produces a technically valid and diagnostically misleading hybrid. Stick to raw network state (ip, ss, nft) unless you explicitly account for the mount namespace.
  3. Read-Only Is a Choice, Not a Guardrail: All the commands above are read-only, which makes them safe. However, entering a namespace does not make subsequent commands harmless. Running ip link delete or nft flush ruleset inside nsenter will happily wreck that container’s networking in real time.
  4. The PID Reuse Race: Processes crash and restart. A PID you captured five minutes ago can exit and eventually be reused by an entirely different process. Always verify your target PID immediately before running diagnostics.
  5. Portability and Privileges: Both nsenter and lsns are standard utilities supplied by util-linux, meaning they are already sitting on almost every modern Linux host. Entering namespaces, however, requires root or appropriate Linux capabilities (CAP_SYS_ADMIN / CAP_SYS_PTRACE).

(For the full mechanics of how the kernel isolates these views, consult the upstream man pages: nsenter(1), lsns(8), and namespaces(7)).

Over to you: Have you ever caught yourself installing curl and iproute2 inside a production container just to check a routing table, or has nsenter already become your default microscope for containerized networking?