[{"content":"Or: how I almost locked myself out of my own EC2 box, and the guard that fixed it.\nI needed one process on a box in us-east-1 to egress through a WireGuard peer in a different geographic location. Everything else on the host had to keep behaving normally: SSH from my laptop, SSM from the AWS console, the package manager, the metrics agent. The host was fine where it was, but a specific workload needed a different exit.\nThe obvious move is wg-quick up on the host with your provider\u0026rsquo;s config. Don\u0026rsquo;t do that. WireGuard\u0026rsquo;s default AllowedIPs = 0.0.0.0/0 rewrites the host\u0026rsquo;s main routing table, which means every outbound packet now goes through the tunnel, including the SSH session you\u0026rsquo;re typing into. If the tunnel doesn\u0026rsquo;t fully come up, or if the peer can\u0026rsquo;t reach you back on the new path, you\u0026rsquo;ve just dropped yourself off the network. On a VM with no serial console enabled, you\u0026rsquo;re calling support.\nSo the actual question is: how do you scope a tunnel to a single process and guarantee the host\u0026rsquo;s networking is untouched?\nThe wrong answer: policy routing If you\u0026rsquo;ve spent any time in Linux networking, your first instinct is ip rule: mark packets from a specific user or cgroup with iptables -j MARK, then ip rule add fwmark X lookup vpn-table, then put a WireGuard default route in that table.\nIt works. It\u0026rsquo;s also fragile in ways you only discover later. You\u0026rsquo;re modifying the kernel\u0026rsquo;s routing logic for everyone, then trusting that your mark-and-route rules will only catch the traffic you meant to catch. The first time NetworkManager restarts, or someone flushes iptables, or a future-you adds a rule that conflicts at a higher priority, you\u0026rsquo;ve got a leak. You probably won\u0026rsquo;t notice until production. Debugging it means staring at ip rule show, ip route show table all, and conntrack output, swearing.\nThere\u0026rsquo;s a cleaner primitive.\nThe right answer: network namespaces A network namespace is its own independent copy of the kernel\u0026rsquo;s networking stack: its own routing table, its own iptables, its own interface list. Processes inside see only that stack. Processes outside don\u0026rsquo;t see it at all.\nSo the model is:\nThe host\u0026rsquo;s root namespace doesn\u0026rsquo;t change. Default route via eth0, SSH on its usual path, no ip rule shenanigans. A separate namespace, egress, has its own default route through a WireGuard tunnel, and only processes I explicitly drop into that namespace use it. A veth pair bridges the two, with one end in each namespace, on a small underlay subnet (10.200.0.0/24). The host masquerades that subnet out its normal interface.\nRoot namespace (control plane) egress namespace (egress plane) eth0 -\u0026gt; Internet (untouched) wg-egress -\u0026gt; WireGuard peer -\u0026gt; Internet \\ / veth underlay (10.200.0.0/24) WireGuard lives inside the namespace, full-tunnel, AllowedIPs = 0.0.0.0/0. From inside the namespace, everything goes through the tunnel. From outside, nothing changed. Verification is two lines:\nip netns exec egress curl -4 ifconfig.me # the peer\u0026#39;s IP curl -4 ifconfig.me # the AWS Elastic IP That\u0026rsquo;s the whole pattern. The control plane stays on its native AWS path; the egress plane lives somewhere else entirely. Logical presence wherever the peer is, physical stability in us-east-1.\nOne detail that bites people: once WireGuard installs its 0.0.0.0/0 route inside the namespace, that route also tries to catch traffic going to the WireGuard endpoint itself, which is a routing loop. The fix is a /32 route for the endpoint\u0026rsquo;s public IP via the underlay gateway, installed after the tunnel comes up. Three lines of ip route and easy to forget, and when you forget you spend forty minutes wondering why your handshakes silently fail.\nThen I almost bricked the server This is where it gets uncomfortable.\nwg-quick is almost namespace-native. You run it under ip netns exec and most of the time it does the right thing: interface comes up inside the namespace, default route lands in the namespace\u0026rsquo;s routing table, everyone goes home.\nMost of the time.\nFirst time I built this, on the EC2 box, over SSH from my laptop: something in my wireguard.conf (I never fully chased it down, probably a PostUp hook interacting badly with how Ubuntu patches wg-quick) caused the WireGuard interface to come up in the root namespace instead of the egress one. The 0.0.0.0/0 route landed on the host. My SSH session was suddenly trying to exit through a tunnel that had no return path back to my laptop. Frozen terminal. No response to anything.\nI got lucky. SSM Session Manager was enabled on that instance, I got in through the AWS console, ran ip link delete wg-egress, watched the host\u0026rsquo;s default route restore itself, and my heart rate came back down.\nThe fix is now the most important code in the repo: a host-integrity guard wrapped around wg-quick up. It does three things.\nBefore the tunnel comes up, if the WireGuard interface already exists in the root namespace, refuse to do anything. Something already went wrong on a previous run; don\u0026rsquo;t make it worse. Then snapshot the host\u0026rsquo;s IPv4 default route as a string, immediately before invoking wg-quick. After wg-quick returns, check two invariants: the WireGuard interface is not in the root namespace, and the host\u0026rsquo;s default route is byte-identical to the snapshot. If either check fails, tear the tunnel back down and exit non-zero.\nHOST_DEFAULT_BEFORE=\u0026#34;$(ip -4 route show default | head -1)\u0026#34; ip netns exec \u0026#34;$NS_NAME\u0026#34; wg-quick up \u0026#34;$WG_CONF\u0026#34; if ip link show \u0026#34;$WG_IFACE\u0026#34; \u0026gt;/dev/null 2\u0026gt;\u0026amp;1; then abort_rollback \u0026#34;ABORT: $WG_IFACE appeared in ROOT namespace\u0026#34; fi HOST_DEFAULT_AFTER=\u0026#34;$(ip -4 route show default | head -1)\u0026#34; if [[ \u0026#34;$HOST_DEFAULT_BEFORE\u0026#34; != \u0026#34;$HOST_DEFAULT_AFTER\u0026#34; ]]; then abort_rollback \u0026#34;ABORT: host default route changed during wg-quick up\u0026#34; fi The guard doesn\u0026rsquo;t prevent the bug. wg-quick does what wg-quick does, and the configuration that triggers the leak lives in user files I can\u0026rsquo;t audit. What the guard does is detect the leak fast enough that the SSH session running the script doesn\u0026rsquo;t die. The window between wg-quick returning and the next outbound SSH packet trying to use the new (broken) route is small but not zero, somewhere around a hundred milliseconds in my testing. Long enough to check, roll back, and exit with a loud error instead of leaving you locked out.\nThis is what turns the project from a weekend experiment into something I\u0026rsquo;d actually run on a server I care about. WireGuard-in-a-namespace as a concept has been written up plenty of times. The guard is the part that makes it safe to run on a remote host where the cost of a bad five seconds is calling support to mount your root volume on a rescue instance.\nWhat this doesn\u0026rsquo;t try to do Worth being honest about scope.\nIPv4 only. If you don\u0026rsquo;t disable IPv6 in the namespace you\u0026rsquo;ll leak the host\u0026rsquo;s address through it; the repo does the disable for you but it\u0026rsquo;s a real footgun if you fork without reading. It controls IP-layer routing. Applications that learn the host\u0026rsquo;s identity through other channels (WebRTC STUN, browser fingerprinting, OS telemetry) will still leak it. You trust the WireGuard peer: encrypted to them, cleartext after. And only networking is isolated. A process in the namespace can still read your home directory and talk to your SSH agent. If you need more, layer firejail, bwrap, or a container on top.\nThat\u0026rsquo;s the trade. Per-process egress, host networking untouched, with a guard that makes the failure mode loud instead of lethal.\nThe repo Setup, scripts, docs, the guard: see ambifore-org/remote-egress. MIT licensed, a few hundred lines of bash.\nIf you\u0026rsquo;ve solved this differently (or, more useful, if you know of a wg-quick replacement that\u0026rsquo;s strictly namespace-native), I\u0026rsquo;d genuinely like to hear about it.\n","permalink":"https://ambifore.com/posts/per-process-vpn-linux-network-namespaces/","summary":"How I almost locked myself out of my own EC2 box, and the guard that fixed it.","title":"Network namespaces are the right answer to per-process VPN on Linux"}]