*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:
```
```
```
<span class="line"><span>pid</span><span>=</span><span>$(</span><span>systemctl</span><span> show </span><span></span></span><span class="line"><span> </span><span>--property=MainPID</span><span> </span><span></span></span><span class="line"><span> </span><span>--value</span><span> thumbnail-worker.service)</span></span><span class="line"></span><span class="line"><span>printf</span><span> </span><span>'Target PID: %sn'</span><span> </span><span>"</span><span>$pid</span><span>"</span></span>
```
*(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:
```
```
```
<span class="line"><span>sudo</span><span> </span><span>lsns</span><span> </span><span>--task</span><span> </span><span>"</span><span>$pid</span><span>"</span></span>
```
*(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:
```
```
```
<span class="line"><span>readlink</span><span> </span><span>/proc/self/ns/net</span></span><span class="line"><span>sudo</span><span> </span><span>readlink</span><span> </span><span>/proc/"</span><span>$pid</span><span>"/ns/net</span></span>
```
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`):
```
```
```
<span class="line"><span>sudo</span><span> </span><span>nsenter</span><span> </span><span></span></span><span class="line"><span> </span><span>--target</span><span> </span><span>"</span><span>$pid</span><span>"</span><span> </span><span></span></span><span class="line"><span> </span><span>--net</span><span> </span><span></span></span><span class="line"><span> </span><span>--</span><span> </span><span>ip</span><span> </span><span>-brief</span><span> </span><span>address</span></span>
```
### Check the Routing Table
Verify whether it has a default gateway or if a missing route is stranding its packets:
```
```
```
<span class="line"><span>sudo</span><span> </span><span>nsenter</span><span> </span><span></span></span><span class="line"><span> </span><span>--target</span><span> </span><span>"</span><span>$pid</span><span>"</span><span> </span><span></span></span><span class="line"><span> </span><span>--net</span><span> </span><span></span></span><span class="line"><span> </span><span>--</span><span> </span><span>ip</span><span> </span><span>route</span><span> </span><span>show</span></span>
```
### Check Listening Sockets
See what is actually bound and listening from the namespace’s perspective (building on our `ss` habits from Snippet [#1](https://schulz.dk/tag/1/)):
```
```
```
<span class="line"><span>sudo</span><span> </span><span>nsenter</span><span> </span><span></span></span><span class="line"><span> </span><span>--target</span><span> </span><span>"</span><span>$pid</span><span>"</span><span> </span><span></span></span><span class="line"><span> </span><span>--net</span><span> </span><span></span></span><span class="line"><span> </span><span>--</span><span> </span><span>ss</span><span> </span><span>-lntup</span></span>
```
### 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:
```
```
```
<span class="line"><span>sudo</span><span> </span><span>nsenter</span><span> </span><span></span></span><span class="line"><span> </span><span>--target</span><span> </span><span>"</span><span>$pid</span><span>"</span><span> </span><span></span></span><span class="line"><span> </span><span>--net</span><span> </span><span></span></span><span class="line"><span> </span><span>--</span><span> </span><span>nft</span><span> </span><span>list</span><span> </span><span>ruleset</span></span>
```
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?
Linux Snippets #16: See the Network the Process Sees
Submitted 23 hours ago by
blog@schulz.dk to random@mbin.linuxnation.social