Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Select an option

  • Save samarpanda/3500dc8df4a599c689f034c13b38c028 to your computer and use it in GitHub Desktop.

Select an option

Save samarpanda/3500dc8df4a599c689f034c13b38c028 to your computer and use it in GitHub Desktop.
WireGuard multi server connection

You can connect WireGuard Server B to Server A as a site-to-site VPN, with Server B initiating the connection to Server A. To allow clients on both servers to communicate, you'll need to configure routing between the two WireGuard subnets.

Your existing networks are:

  • Server A: 10.10.0.0/24 — server IP 10.10.0.1
  • Server B: 10.11.0.0/24 — server IP 10.11.0.1

There are two ways to do this:

  1. Recommended: routed site-to-site VPN — preserve the original client IPs, allowing clients on either side to identify each other.
  2. NAT-based VPN — translate client addresses across the tunnel, which can simplify routing in some setups but hides the original source IPs.

Since you prefer NAT, I'll explain that approach, while also showing where routing is needed. NAT alone does not establish connectivity; WireGuard must still know which peer should receive traffic for the other subnet.

1. Server A configuration

Keep your existing interface and client peers. Add a peer for Server B and rules to NAT traffic between the two VPN subnets.

Assuming Server B's public key is SERVER_B_PUBLIC_KEY:

ini

[Interface]
PrivateKey = [SERVER_A_PRIVATE_KEY]
Address = 10.10.0.1/24
ListenPort = 51820

PreUp = sysctl -w net.ipv4.ip_forward=1

# Allow forwarding between VPN clients
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Existing internet NAT
PostUp = iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -o ens3 -j MASQUERADE

# NAT traffic from A's clients to B's clients
PostUp = iptables -t nat -A POSTROUTING -s 10.10.0.0/24 -d 10.11.0.0/24 -o wg0 -j MASQUERADE

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.10.0.0/24 -o ens3 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.10.0.0/24 -d 10.11.0.0/24 -o wg0 -j MASQUERADE

[Peer]
PublicKey = [EXISTING_CLIENT_1_PUBLIC_KEY]
AllowedIPs = 10.10.0.2/32

[Peer]
PublicKey = [EXISTING_CLIENT_2_PUBLIC_KEY]
AllowedIPs = 10.10.0.3/32

# Server B
[Peer]
PublicKey = [SERVER_B_PUBLIC_KEY]
AllowedIPs = 10.11.0.0/24

The important addition is:

ini

[Peer]
PublicKey = [SERVER_B_PUBLIC_KEY]
AllowedIPs = 10.11.0.0/24

This tells Server A to send traffic destined for any address in Server B's VPN subnet to Server B.

2. Server B configuration

Server B will initiate the connection to Server A. Replace SERVER_A_PUBLIC_IP with Server A's reachable public IP address or DNS hostname.

ini

[Interface]
PrivateKey = [SERVER_B_PRIVATE_KEY]
Address = 10.11.0.1/24
ListenPort = 51820

PreUp = sysctl -w net.ipv4.ip_forward=1

# Allow forwarding between VPN clients
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT
PostUp = iptables -A FORWARD -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT

# Existing internet NAT
PostUp = iptables -t nat -A POSTROUTING -s 10.11.0.0/24 -o ens3 -j MASQUERADE

# NAT traffic from B's clients to A's clients
PostUp = iptables -t nat -A POSTROUTING -s 10.11.0.0/24 -d 10.10.0.0/24 -o wg0 -j MASQUERADE

PostDown = iptables -D FORWARD -i wg0 -j ACCEPT
PostDown = iptables -D FORWARD -o wg0 -m conntrack --ctstate ESTABLISHED,RELATED -j ACCEPT
PostDown = iptables -t nat -D POSTROUTING -s 10.11.0.0/24 -o ens3 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.11.0.0/24 -d 10.10.0.0/24 -o wg0 -j MASQUERADE

# Existing local clients
[Peer]
PublicKey = [EXISTING_CLIENT_1_PUBLIC_KEY]
AllowedIPs = 10.11.0.2/32

[Peer]
PublicKey = [EXISTING_CLIENT_2_PUBLIC_KEY]
AllowedIPs = 10.11.0.3/32

# Server A
[Peer]
PublicKey = [SERVER_A_PUBLIC_KEY]
AllowedIPs = 10.10.0.0/24
Endpoint = SERVER_A_PUBLIC_IP:51820
PersistentKeepalive = 25

The important settings are:

  • AllowedIPs = 10.10.0.0/24 routes traffic for Server A's entire client subnet through the tunnel.
  • Endpoint tells Server B where to initiate the connection.
  • PersistentKeepalive = 25 helps maintain connectivity if Server B is behind NAT.

You do not need to configure an Endpoint for Server B on Server A if Server B initiates the connection and Server A can learn Server B's endpoint from its handshake.

3. Update the client configurations

This step is essential. The servers can be connected correctly while the clients still fail to communicate if their own routing configurations are missing the remote subnet.

Clients connected to Server A

In each client's existing peer entry for Server A, ensure AllowedIPs includes 10.11.0.0/24.

For example:

ini

[Peer]
PublicKey = [SERVER_A_PUBLIC_KEY]
Endpoint = SERVER_A_PUBLIC_IP:51820
AllowedIPs = 10.10.0.0/24, 10.11.0.0/24

Clients connected to Server B

In each client's existing peer entry for Server B, ensure AllowedIPs includes 10.10.0.0/24.

ini

[Peer]
PublicKey = [SERVER_B_PUBLIC_KEY]
Endpoint = SERVER_B_PUBLIC_IP:51820
AllowedIPs = 10.11.0.0/24, 10.10.0.0/24

Keep any other existing AllowedIPs entries, such as 0.0.0.0/0, if you already use them. The examples above illustrate subnet routing, not necessarily your complete client configuration.

4. How the NAT routing works

how-the-nat-routing-works

The two POSTROUTING rules translate traffic destined for the other VPN subnet to the local server's WireGuard address. Connection tracking reverses that translation for replies.

Trade-off: With this design, a client on Server A generally sees remote clients as 10.11.0.1, and clients on Server B see remote clients as 10.10.0.1. This works for initiating connections to individual remote client IPs, but the remote application won't see each client's original source IP. If preserving client identity matters, use routed site-to-site networking without NAT.

5. Apply and test

After backing up your configurations, restart WireGuard on both servers:

bash

sudo systemctl restart wg-quick@wg0

Check the tunnel on Server B:

bash

sudo wg show
ping 10.10.0.1

Then test actual client-to-client communication:

  • From Client A (10.10.0.2), try reaching Client B (10.11.0.2).
  • From Client B (10.11.0.2), try reaching Client A (10.10.0.2).

If the tunnel works but client communication fails, check host firewalls, IP forwarding, and the client AllowedIPs settings. Also ensure Server A's UDP port 51820 is reachable from Server B.

One final consideration: the rules above assume both servers use wg0 and that their firewall permits the forwarding shown. The ens3 NAT rule remains for internet access; it is separate from the inter-server NAT rules.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment