RLP's Computing Blog

Giving a Server Behind NAT Its Own Public IP, With a Cheap VPS and WireGuard

Article Info

Recent Changes

About


Background

[LLM Disclosure: Claude wrote the first draft of this document. I edited it substantially. Claude helped with project management and investigation on the actual technical end for my own setup. The original idea and initial proof of concept were entirely mine, but by the time I came back to actually implement it, I’d forgotten a lot of detail, so I got Claude to scrape my tmux sessions and help me figure out what I’d been working on and do the polish and refinement. Very, very little actual code was Claude’s, but then this project didn’t have much.]

As a general comment, my personal setup is much more complex than this so it’s possible I missed something in the translation to a set of examples; feel free to email me at robinleepowell at gmail if it’s not working and you want a hand.

I have had static IPs on my home servers for a very long time, but that’s been getting more and more difficult to do without a really shitty support experience, so I decided to stop. However, I didn’t want to just like forward HTTPS to my home machines because I’ve had static IPs for almost 30 years and there’s a lot that I depend on that isn’t just web stuff. Also, for my sins, I still run my own email server which means the outbound IP is extremely important, and must be static and I must be able to set rDNS on it.

Grabbing a random VPS and redirecting packets on a port-by-port basis is well understood, but crazy person that I am I thought “is there any reason I can’t just redirect an entire IP at the nftables level?”.

Like, why can’t I just have an entire IP on a VPS act as if it’s on one of my home servers?

Turns out, you totally can. And you can do it bi-directionally, so that when the home server sends from a particular IP/interface, it comes out the other end with the VPS IP that the world sees.

I have not found a writeup of anything like this, hence this post.

I’m doing this by hand below, no automation, which is also how I actually built the proof of concept. If you’re going to run this for real you’ll want to put it under whatever configuration management you use, but the mechanism itself doesn’t need that to work, and understanding it without a config management layer in the way is worth doing first anyway.

Overview

We have the VPS, which has a static IP (we’ll call that 203.0.113.10) for SSHing and normal stuff, and another static IP for the IP mirroring (we’ll call that 203.0.113.20).

(Fun fact: some VPS providers will not provide multiple IPv4s. Finding that out was fun.)

And we have the home server, which has an internal 10.* or 192.168.* or whatever IP, we don’t really care for our purposes here.

We’re going to build a WireGuard tunnel between them. Then, on the VPS, you tell the firewall “anything that shows up addressed to the secondary IP, just hand to the home server through the tunnel instead,” and “anything the home server sends back through the tunnel, make it look like it came from the secondary IP”.

We’ll use 10.1.1.1/10.1.1.2 as the tunnel addresses (VPS and home server, respectively). Swap in your actual numbers as you go.

Setting Up The Tunnel

Generate a keypair on each box:

wg genkey | tee privatekey | wg pubkey > publickey

On the VPS, /etc/wireguard/wg0.conf:

[Interface]
PrivateKey = <the VPS's own private key>
Address = 10.1.1.1/24
ListenPort = 51820

[Peer]
PublicKey = <the home server's public key>
AllowedIPs = 10.1.1.2/32

And on the home server, also /etc/wireguard/wg0.conf:

[Interface]
PrivateKey = <the home server's own private key>
Address = 10.1.1.2/24
Table = off

[Peer]
PublicKey = <the VPS's public key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

PersistentKeepalive = 25 is only on the home server’s side, because it’s the one behind NAT. WireGuard is just UDP underneath, and your router’s NAT mapping for that UDP “connection” will time out and get reclaimed if nothing crosses it for a while. The VPS doesn’t need this, since it has a real address and nothing is going to forget about it.

The AllowedIPs Asymmetry

AllowedIPs looks like a simple access-list setting, but it’s actually WireGuard’s crypto-routing table: for a given peer, it’s both “what source addresses I’ll accept decrypted traffic from” and (depending on your routing setup) “what destinations I’ll send through this peer.” The VPS’s entry for the home server is a tight /32 - exactly, and only, the home server’s own tunnel address. The home server’s entry for the VPS is the wide-open 0.0.0.0/0. This is on purpose.

When a random packet hits 203.0.113.20, the VPS’s firewall rewrites the destination to 10.1.1.2 and forwards it into the tunnel. It does not rewrite the source. So what arrives at the home server, decrypted, is a packet that’s nominally “from” some address that has nothing to do with either tunnel endpoint. If the home server’s AllowedIPs for the VPS peer were narrowed to just the VPS’s own /32, it drops all internet traffic for this reason.

The VPS’s side doesn’t have this problem, because it’s never on the receiving end of relayed traffic - it only ever needs to accept things that are genuinely from the home server.

The Trap: wg-quick’s Automatic Routing

wg-quick - the thing that actually reads that config file and brings the interface up - has a Table setting that defaults to auto. When a peer’s AllowedIPs is 0.0.0.0/0, auto treats that as “this peer wants to be my default route,” and quietly sets up a whole scheme to make that happen. It is extremely unlikely that this is what you want, since this means that the route that takes normal packets (including the ones to your local home router, bound to the internet) will try to go over the WireGuard tunnel.

This actually would work, with the result of all internet traffic from the home server routing through the VPS, which isn’t at all what I wanted. It might also break if you have complex networking at home that relies on “my home router is the default route”, which is what happened to me.

Table = off tells wg-quick to keep its hands off routing altogether - no fwmark, no policy route, no generated firewall table. AllowedIPs still does its actual job of deciding what the tunnel will accept.

Routing Replies Back Through The Tunnel

The wg-quick automatic routing thing we just turned off was also the reason replies to relayed connections made it back through the tunnel at all, so, we need to put that part back.

When something on the home server answers an inbound connection that arrived through the tunnel, its reply is sourced as the address the connection was made to. So a reply to a connection on 10.1.1.2 is itself sourced from 10.1.1.2. Without a route telling the home server “anything sourced from my own tunnel address goes out through wg0,” that reply just takes the normal default gateway instead, and the original client never recognizes it.

The fix is a small policy route, added via PostUp/PostDown in the home server’s own config so it comes and goes with the interface automatically. Add this to the [Interface] section of wg0.conf on the home side:

PostUp = ip route replace default dev %i table wg_passthrough
PostUp = ip rule add from 10.1.1.2 table wg_passthrough priority 100
PostDown = ip rule del from 10.1.1.2 table wg_passthrough priority 100

wg_passthrough is just a name - the kernel needs to know what number it maps to, via a line in /etc/iproute2/rt_tables:

echo "200 wg_passthrough" >> /etc/iproute2/rt_tables

Note that that file may not exist and in that case you’ll need to create it.

The Firewall Rules

The nftables rules lives entirely on the VPS. Their job is to redirect traffic for the secondary IP into the tunnel, make the home server’s replies (and anything it sends on its own) look like they came from that IP.

It’s also important that the rules only forward traffic that’s actually part of this arrangement, rather than quietly becoming an open relay.

As one nftables table, in a file you’ll put somewhere permanent in the next section:

table inet static_ip {
    chain input {
        type filter hook input priority filter; policy drop;

        ct state established,related accept
        ct state invalid drop

        iifname "lo" accept
        meta l4proto { icmp, ipv6-icmp } accept

        udp dport 51820 accept
        tcp dport { 22 } accept
    }

    chain prerouting {
        type nat hook prerouting priority dstnat; policy accept;
        ip daddr 203.0.113.20 dnat to 10.1.1.2
    }

    chain postrouting {
        type nat hook postrouting priority srcnat; policy accept;
        iifname "wg0" ip saddr 10.1.1.2 snat to 203.0.113.20
    }

    chain forward {
        type filter hook forward priority filter; policy drop;

        ct state established,related accept
        ct state invalid drop

        iifname "wg0" oifname "eth0" ip saddr 10.1.1.2 accept
        iifname "eth0" oifname "wg0" ip daddr 10.1.1.2 accept
    }
}

The input chain is just “let me SSH into my own VPS, and let WireGuard’s own handshake traffic through”.

The prerouting and postrouting chains are the actual trick, exactly as described above, and notice that literally everything is forwarded forwarded unconditionally. If the home server isn’t listening on something, it sends back the connection refused, same as it would for any other address it owns.

The forward chain is technically not required if you don’t mind becoming an open relay to the internet. But you do mind that, so keep it.

Swap eth0 for whatever your VPS actually calls its internet-facing interface.

Making It Permanent

IP forwarding:

echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/99-forwarding.conf
sysctl --system

The WireGuard interface:

systemctl enable --now wg-quick@wg0

Do this on both ends.

The nftables rules:

Put the table inet static_ip { ... } block from above into its own file, say /etc/nftables/static_ip.nft, and get it included from wherever your distro’s nftables service actually loads its rules from - on anything RHEL-flavored, that’s an include line added to /etc/sysconfig/nftables.conf:

echo 'include "/etc/nftables/static_ip.nft"' >> /etc/sysconfig/nftables.conf
systemctl enable --now nftables

If firewalld is sitting there enabled from whatever the VPS image shipped with, turn it off first (systemctl disable --now firewalld) - it and a hand-rolled nftables ruleset will fight each other, and firewalld tends to win the boot-order race.

If You Want IPv6 Too

Everything above generalizes directly - WireGuard, the firewall rules, the policy route, all of it - just add the IPv6 forms alongside the IPv4 ones, not instead of them.

Two gotchas:

ip, ip route, and ip rule default to IPv4 whenever the target doesn’t make the family obvious - ip route replace default dev wg0 table wg_passthrough is quietly IPv4-only, even though nothing about it looks that way. Anywhere you see one of these commands above, the v6 version needs its own explicit -6, run as its own extra command, not folded into the existing one.

Also, net.ipv6.conf.all.forwarding is a completely separate switch from net.ipv4.ip_forward - turning one on says nothing about the other, and nothing warns you that the other one’s still off. If inbound v4 works and v6 doesn’t (or the reverse), this is the first thing to check.

Spoke-to-Spoke: The Hairpin

Everything above assumes a home server only ever needs to talk to the wider internet. The moment you’ve got a second home server behind the same VPS, and the two of them talk to each other under their own public identities (two mail servers exchanging mail, say), the connections silently fail.

Here’s the shape of it. Say your second home server is 10.1.1.3 on the tunnel, forwarded from 203.0.113.21. One home server connects to the other’s public address, not its tunnel address - that’s the whole point of giving each one a real identity. The VPS DNATs it, same as it would for a stranger on the internet, and tries to hand it back out through the tunnel - except this time both ends of that hop are wg0, and our forward chain doesn’t match those packets.

Fix is two more rules, scoped deliberately rather than just opening wg0 to itself wholesale:

set peer_tunnel_ips {
    type ipv4_addr
    elements = { 10.1.1.2, 10.1.1.3 }
}

# inside the existing forward chain:
iifname "wg0" oifname "wg0" ip saddr @peer_tunnel_ips ip daddr @peer_tunnel_ips accept

Add similar ipv6 rules if desired.

The other direction is handled by the usual ct state established,related accept stuff.

This is deliberately limited to only the explicit servers named, no reason to open up more than we need.

fail2ban

Make sure you update any abuse prevention rules you have in fail2ban or similar systems on your home servers to include the external IPs of any servers you have that might talk to each other, or it is likely you will have sadness. Honestly it’s better to include every IP that an internal host has here, but you need to take extra care to include the external ones since those aren’t visible to the servers themselves.

What This Doesn’t Cover

This gets you full inbound passthrough and that’s it - the home server answers on that IP, for anything, as if it owned it directly. Getting something specific on the home server (an Exim transport, say) to actually bind its outbound connections to the tunnel address in the first place is still a separate, per-application detail not covered here, but telling an app to source from a particular interface is generally quite doable in most apps these days.