<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Ambitious Foreman</title>
    <description>The latest articles on DEV Community by Ambitious Foreman (@ambifore).</description>
    <link>https://dev.to/ambifore</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F3991273%2Ff473509a-d632-4746-9262-0c0e1643c20c.png</url>
      <title>DEV Community: Ambitious Foreman</title>
      <link>https://dev.to/ambifore</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/ambifore"/>
    <language>en</language>
    <item>
      <title>Network namespaces are the right answer to per-process VPN on Linux</title>
      <dc:creator>Ambitious Foreman</dc:creator>
      <pubDate>Thu, 18 Jun 2026 18:41:31 +0000</pubDate>
      <link>https://dev.to/ambifore/network-namespaces-are-the-right-answer-to-per-process-vpn-on-linux-4lpa</link>
      <guid>https://dev.to/ambifore/network-namespaces-are-the-right-answer-to-per-process-vpn-on-linux-4lpa</guid>
      <description>&lt;p&gt;&lt;em&gt;Or: how I almost locked myself out of my own EC2 box, and the guard that fixed it.&lt;/em&gt;&lt;/p&gt;

&lt;p&gt;I needed one process on a box in &lt;code&gt;us-east-1&lt;/code&gt; 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. Only this one workload needed a different exit.&lt;/p&gt;

&lt;p&gt;The obvious move is &lt;code&gt;wg-quick up&lt;/code&gt; on the host with your provider's config. Don't do that. WireGuard's default &lt;code&gt;AllowedIPs = 0.0.0.0/0&lt;/code&gt; rewrites the host's main routing table, which means &lt;em&gt;every&lt;/em&gt; outbound packet now goes through the tunnel, including the SSH session you're typing into. If the tunnel doesn't fully come up, or if the peer can't reach you back on the new path, you've just dropped yourself off the network. On a VM with no serial console enabled, you're calling support.&lt;/p&gt;

&lt;p&gt;So the actual question is: how do you scope a tunnel to a single process and guarantee the host's networking is untouched?&lt;/p&gt;

&lt;h2&gt;
  
  
  The wrong answer: policy routing
&lt;/h2&gt;

&lt;p&gt;If you've spent any time in Linux networking, your first instinct is &lt;code&gt;ip rule&lt;/code&gt;: mark packets from a specific user or cgroup with &lt;code&gt;iptables -j MARK&lt;/code&gt;, then &lt;code&gt;ip rule add fwmark X lookup vpn-table&lt;/code&gt;, then put a WireGuard default route in that table.&lt;/p&gt;

&lt;p&gt;It works. It's also fragile in ways you only discover later. You're modifying the kernel's routing logic &lt;em&gt;for everyone&lt;/em&gt;, 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've got a leak. You probably won't notice until production. Debugging it means staring at &lt;code&gt;ip rule show&lt;/code&gt;, &lt;code&gt;ip route show table all&lt;/code&gt;, and conntrack output, swearing.&lt;/p&gt;

&lt;p&gt;There's a cleaner primitive.&lt;/p&gt;

&lt;h2&gt;
  
  
  The right answer: network namespaces
&lt;/h2&gt;

&lt;p&gt;A network namespace is its own independent copy of the kernel's networking stack: its own routing table, its own iptables, its own interface list. Processes inside see only that stack. Processes outside don't see it at all.&lt;/p&gt;

&lt;p&gt;So the model is:&lt;/p&gt;

&lt;p&gt;The host's root namespace doesn't change. Default route via &lt;code&gt;eth0&lt;/code&gt;, SSH on its usual path, no &lt;code&gt;ip rule&lt;/code&gt; shenanigans. A separate namespace, &lt;code&gt;egress&lt;/code&gt;, has its own default route through a WireGuard tunnel, and only processes I explicitly drop into that namespace use it. A &lt;code&gt;veth&lt;/code&gt; pair bridges the two, with one end in each namespace, on a small underlay subnet (&lt;code&gt;10.200.0.0/24&lt;/code&gt;). The host masquerades that subnet out its normal interface.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;Root namespace (control plane)          egress namespace (egress plane)
  eth0 -&amp;gt; Internet (untouched)            wg-egress -&amp;gt; WireGuard peer -&amp;gt; Internet
                       \                /
                        veth underlay (10.200.0.0/24)
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;WireGuard lives &lt;em&gt;inside&lt;/em&gt; the namespace, full-tunnel, &lt;code&gt;AllowedIPs = 0.0.0.0/0&lt;/code&gt;. From inside the namespace, everything goes through the tunnel. From outside, nothing changed. Verification is two lines:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;ip netns &lt;span class="nb"&gt;exec &lt;/span&gt;egress curl &lt;span class="nt"&gt;-4&lt;/span&gt; ifconfig.me   &lt;span class="c"&gt;# the peer's IP&lt;/span&gt;
curl &lt;span class="nt"&gt;-4&lt;/span&gt; ifconfig.me                        &lt;span class="c"&gt;# the AWS Elastic IP&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;That'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 &lt;code&gt;us-east-1&lt;/code&gt;.&lt;/p&gt;

&lt;p&gt;One detail that bites people: once WireGuard installs its &lt;code&gt;0.0.0.0/0&lt;/code&gt; route inside the namespace, that route also tries to catch traffic going to the WireGuard &lt;em&gt;endpoint itself&lt;/em&gt;, which is a routing loop. The fix is a &lt;code&gt;/32&lt;/code&gt; route for the endpoint's public IP via the underlay gateway, installed &lt;em&gt;after&lt;/em&gt; the tunnel comes up. Three lines of &lt;code&gt;ip route&lt;/code&gt; and easy to forget, and when you forget you spend forty minutes wondering why your handshakes silently fail.&lt;/p&gt;

&lt;h2&gt;
  
  
  Then I almost bricked the server
&lt;/h2&gt;

&lt;p&gt;This is where it gets uncomfortable.&lt;/p&gt;

&lt;p&gt;&lt;code&gt;wg-quick&lt;/code&gt; is &lt;em&gt;almost&lt;/em&gt; namespace-native. You run it under &lt;code&gt;ip netns exec&lt;/code&gt; and most of the time it does the right thing: interface comes up inside the namespace, default route lands in the namespace's routing table, everyone goes home.&lt;/p&gt;

&lt;p&gt;Most of the time.&lt;/p&gt;

&lt;p&gt;First time I built this, on the EC2 box, over SSH from my laptop: something in my &lt;code&gt;wireguard.conf&lt;/code&gt; (I never fully chased it down, probably a &lt;code&gt;PostUp&lt;/code&gt; hook interacting badly with how Ubuntu patches &lt;code&gt;wg-quick&lt;/code&gt;) caused the WireGuard interface to come up in the &lt;em&gt;root&lt;/em&gt; namespace instead of the &lt;code&gt;egress&lt;/code&gt; one. The &lt;code&gt;0.0.0.0/0&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;I got lucky. SSM Session Manager was enabled on that instance, I got in through the AWS console, ran &lt;code&gt;ip link delete wg-egress&lt;/code&gt;, watched the host's default route restore itself, and my heart rate came back down.&lt;/p&gt;

&lt;p&gt;The fix is now the most important code in the repo: a host-integrity guard wrapped around &lt;code&gt;wg-quick up&lt;/code&gt;. It does three things.&lt;/p&gt;

&lt;p&gt;Before 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't make it worse. Then snapshot the host's IPv4 default route as a string, immediately before invoking &lt;code&gt;wg-quick&lt;/code&gt;. After &lt;code&gt;wg-quick&lt;/code&gt; returns, check two invariants: the WireGuard interface is &lt;em&gt;not&lt;/em&gt; in the root namespace, and the host's default route is byte-identical to the snapshot. If either check fails, tear the tunnel back down and exit non-zero.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight shell"&gt;&lt;code&gt;&lt;span class="nv"&gt;HOST_DEFAULT_BEFORE&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ip &lt;span class="nt"&gt;-4&lt;/span&gt; route show default | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
ip netns &lt;span class="nb"&gt;exec&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$NS_NAME&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; wg-quick up &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$WG_CONF&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;

&lt;span class="k"&gt;if &lt;/span&gt;ip &lt;span class="nb"&gt;link &lt;/span&gt;show &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$WG_IFACE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;&amp;gt;&lt;/span&gt;/dev/null 2&amp;gt;&amp;amp;1&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;abort_rollback &lt;span class="s2"&gt;"ABORT: &lt;/span&gt;&lt;span class="nv"&gt;$WG_IFACE&lt;/span&gt;&lt;span class="s2"&gt; appeared in ROOT namespace"&lt;/span&gt;
&lt;span class="k"&gt;fi

&lt;/span&gt;&lt;span class="nv"&gt;HOST_DEFAULT_AFTER&lt;/span&gt;&lt;span class="o"&gt;=&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="si"&gt;$(&lt;/span&gt;ip &lt;span class="nt"&gt;-4&lt;/span&gt; route show default | &lt;span class="nb"&gt;head&lt;/span&gt; &lt;span class="nt"&gt;-1&lt;/span&gt;&lt;span class="si"&gt;)&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt;
&lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="o"&gt;[[&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOST_DEFAULT_BEFORE&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;!=&lt;/span&gt; &lt;span class="s2"&gt;"&lt;/span&gt;&lt;span class="nv"&gt;$HOST_DEFAULT_AFTER&lt;/span&gt;&lt;span class="s2"&gt;"&lt;/span&gt; &lt;span class="o"&gt;]]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="k"&gt;then
  &lt;/span&gt;abort_rollback &lt;span class="s2"&gt;"ABORT: host default route changed during wg-quick up"&lt;/span&gt;
&lt;span class="k"&gt;fi&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;The guard doesn't &lt;em&gt;prevent&lt;/em&gt; the bug. &lt;code&gt;wg-quick&lt;/code&gt; does what &lt;code&gt;wg-quick&lt;/code&gt; does, and the configuration that triggers the leak lives in user files I can't audit. What the guard does is detect the leak fast enough that the SSH session running the script doesn't die. The window between &lt;code&gt;wg-quick&lt;/code&gt; 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.&lt;/p&gt;

&lt;p&gt;This is what turns the project from a weekend experiment into something I'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.&lt;/p&gt;

&lt;h2&gt;
  
  
  What this doesn't try to do
&lt;/h2&gt;

&lt;p&gt;Worth being honest about scope.&lt;/p&gt;

&lt;p&gt;IPv4 only. If you don't disable IPv6 in the namespace you'll leak the host's address through it; the repo does the disable for you but it's a real footgun if you fork without reading. It controls IP-layer routing. Applications that learn the host'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 &lt;code&gt;firejail&lt;/code&gt;, &lt;code&gt;bwrap&lt;/code&gt;, or a container on top.&lt;/p&gt;

&lt;p&gt;That's the trade. Per-process egress, host networking untouched, with a guard that makes the failure mode loud instead of lethal.&lt;/p&gt;

&lt;h2&gt;
  
  
  The repo
&lt;/h2&gt;

&lt;p&gt;Setup, scripts, docs, the guard: see &lt;a href="https://github.com/ambifore-org/remote-egress" rel="noopener noreferrer"&gt;ambifore-org/remote-egress&lt;/a&gt;. MIT licensed, a few hundred lines of bash.&lt;/p&gt;

&lt;p&gt;If you've solved this differently (or, more useful, if you know of a &lt;code&gt;wg-quick&lt;/code&gt; replacement that's strictly namespace-native), I'd genuinely like to hear about it.&lt;/p&gt;

</description>
      <category>linux</category>
      <category>networking</category>
      <category>wireguard</category>
      <category>security</category>
    </item>
  </channel>
</rss>
