Build a private link between servers with WireGuard

You connect servers privately by installing WireGuard on each one, giving each a key pair and a 10.x address, and listing the other servers as peers in /etc/wireguard/wg0.conf. After wg-quick@wg0 is enabled, the servers reach each other on the 10.x addresses through an encrypted tunnel, and you can bind PostgreSQL or Redis to that address so the database never listens on the public internet.

Networking and security guide, updated 2026-12-03

What will you have at the end?

Two servers, called app and db here, joined by an encrypted tunnel, then a third server added to the same mesh. The plan in this guide:

ServerPublic IPv4Tunnel address
app203.0.113.1010.20.0.1
db198.51.100.2010.20.0.2
worker (added later)203.0.113.3010.20.0.3

Every command is for Ubuntu 24.04 or Debian 13, run as root. Rocky Linux and AlmaLinux differences are noted where they apply. The servers can be in the same city or in different ones; the steps are identical. The background is in Private networking explained.

How do you install WireGuard and create keys?

On every server:

apt update
apt install -y wireguard
cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
cat publickey

On Rocky Linux 9 or 10 and AlmaLinux, the kernel module is already included; install the tools with dnf install -y wireguard-tools.

umask 077 makes sure the private key file is readable by root only. The public key printed by the last command is safe to copy into a notes file; you will paste each server public key into the config of the other servers. Never copy a privatekey file between machines: each server keeps its own.

What goes into wg0.conf on each server?

On app, create /etc/wireguard/wg0.conf:

[Interface]
Address = 10.20.0.1/24
ListenPort = 51820
PrivateKey = (contents of /etc/wireguard/privatekey on app)

[Peer]
# db
PublicKey = (public key of db)
Endpoint = 198.51.100.20:51820
AllowedIPs = 10.20.0.2/32
PersistentKeepalive = 25

On db, the mirror image:

[Interface]
Address = 10.20.0.2/24
ListenPort = 51820
PrivateKey = (contents of /etc/wireguard/privatekey on db)

[Peer]
# app
PublicKey = (public key of app)
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.20.0.1/32
PersistentKeepalive = 25

AllowedIPs does two jobs: it is the list of inner addresses this peer may send from, and the list of addresses the kernel will route to this peer. Use a /32 per server in a mesh, never the whole /24 on more than one peer. PersistentKeepalive = 25 sends a tiny packet every 25 seconds so state in any firewall along the path stays fresh; between two servers with public addresses it is optional, but it makes the first packet after a quiet hour arrive without delay.

Lock the file down: chmod 600 /etc/wireguard/wg0.conf.

Which firewall ports must be open?

WireGuard uses one UDP port per interface, here 51820. Open it on every peer and allow the services you want on the tunnel:

ufw allow 51820/udp
ufw allow in on wg0 to any port 5432 proto tcp
ufw allow in on wg0 to any port 6379 proto tcp
ufw reload

To accept WireGuard only from your own servers, use ufw allow from 203.0.113.10 to any port 51820 proto udp per peer instead of the open rule. On Rocky and Alma with firewalld:

firewall-cmd --permanent --add-port=51820/udp
firewall-cmd --permanent --zone=trusted --add-interface=wg0
firewall-cmd --reload

Putting wg0 in the trusted zone allows all tunnel traffic; for tighter control, create a custom zone and add only the service ports.

How do you start the tunnel and keep it up after reboots?

On both servers:

systemctl enable --now wg-quick@wg0
wg show

wg show should list the interface, its listening port and the peer. Right after start, the peer section has no handshake yet. Send one packet and look again:

ping -c 3 10.20.0.2
wg show wg0 latest-handshakes

A handshake time a few seconds old means the keys match and packets flow both ways. ip addr show wg0 shows the 10.20.0.1/24 address and ip route shows 10.20.0.0/24 dev wg0. The unit starts on every boot; after editing the config later, apply it with systemctl restart wg-quick@wg0, or without dropping the interface with wg syncconf wg0 <(wg-quick strip wg0).

How do you test the link and spot the usual mistakes?

Check in this order:

  1. No handshake at all. UDP 51820 is blocked or the endpoint address is wrong. On the receiving side run tcpdump -ni eth0 udp port 51820 while you ping from the other end; if no packets arrive, it is the sender endpoint or a firewall. Interface names differ: check yours with ip -br link.
  2. Packets arrive, still no handshake. The public keys are swapped or mistyped. Compare wg show wg0 public-key on each server with the PublicKey line the other side has.
  3. Handshake works, ping fails. AllowedIPs does not include the address you are pinging, or ufw drops ICMP on wg0. ip route get 10.20.0.2 must say dev wg0.
  4. Ping works, the service does not. The daemon listens on the wrong address or the tunnel port rule is missing. ss -tlnp shows the truth.

Measure throughput with iperf3 -s on one end and iperf3 -c 10.20.0.2 on the other once everything answers.

How do you add a third server to the mesh?

Install WireGuard on worker, generate its keys and write its config with Address = 10.20.0.3/24 and two peer blocks, one for app and one for db. Then add one new peer block to each existing server:

[Peer]
# worker
PublicKey = (public key of worker)
Endpoint = 203.0.113.30:51820
AllowedIPs = 10.20.0.3/32
PersistentKeepalive = 25

Apply on app and db without breaking current connections:

wg syncconf wg0 <(wg-quick strip wg0)

Start the tunnel on worker with systemctl enable --now wg-quick@wg0 and ping both 10.20.0.1 and 10.20.0.2. Each new peer adds one block to every existing config, so with many servers it pays to generate the files from a short script or an Ansible template.

When should you change the MTU?

WireGuard adds its own header to each packet, so the tunnel MTU must be smaller than the MTU of the public interface. wg-quick sets 1420 by default, which leaves room for the overhead over IPv6 on a normal 1500 byte link. You rarely need to touch it.

The sign of an MTU problem is very specific: ping and SSH work, small queries work, but large responses hang or time out, for example a big SELECT or a file copy that stalls. Test with packets that cannot be fragmented:

ping -c 3 -M do -s 1392 10.20.0.2

1392 bytes of payload plus 28 bytes of headers is 1420. If that fails but smaller sizes work, set MTU = 1380 in the [Interface] section on every peer and restart the tunnel. The details of path MTU are in MTU and MSS.

How do you point PostgreSQL at the tunnel?

Ubuntu 24.04 ships PostgreSQL 16, Debian 13 ships 17; adjust the version in the paths. On db, edit /etc/postgresql/16/main/postgresql.conf:

listen_addresses = 'localhost,10.20.0.2'

Then allow the tunnel range in /etc/postgresql/16/main/pg_hba.conf:

host    appdb    appuser    10.20.0.0/24    scram-sha-256

PostgreSQL refuses to start if 10.20.0.2 does not exist yet, which happens when it boots before the tunnel. Add an ordering drop-in:

mkdir -p /etc/systemd/system/postgresql@16-main.service.d
printf '[Unit]\nAfter=wg-quick@wg0.service\nWants=wg-quick@wg0.service\n' > /etc/systemd/system/postgresql@16-main.service.d/wg.conf
systemctl daemon-reload
systemctl restart postgresql

From app, test with psql -h 10.20.0.2 -U appuser appdb and set your application connection string to the same host. ss -tlnp | grep 5432 on db must show only 127.0.0.1 and 10.20.0.2.

How do you point Redis at the tunnel?

In /etc/redis/redis.conf on db:

bind 127.0.0.1 10.20.0.2
protected-mode yes
requirepass use-a-long-random-string-here

Add the same kind of drop-in for redis-server.service with After=wg-quick@wg0.service, reload systemd and restart Redis. Test from app with redis-cli -h 10.20.0.2 -a ... ping; the answer is PONG. Keep the password even on the tunnel: it limits the damage if another peer is ever compromised. For more on placing databases, see databases on a VPS.

Questions about this guide

Does tunnel traffic count as outgoing transfer?

Yes. The encrypted packets travel between public addresses, so each server counts what it sends. Incoming traffic is free. Keep heavy replication within budget by checking vnstat -i wg0 over a few days.

Can I use a different port than 51820?

Yes. Set any free UDP port in ListenPort, use it in the Endpoint lines of the other peers, and open it in the firewall instead of 51820.

Do I need IP forwarding?

Not for a mesh where every server talks to every other server directly. Forwarding is only needed when one server routes traffic for others, as in a hub and spoke layout or a VPN gateway.

What happens when a server public IP changes?

Only the Endpoint lines that point at it change. Tunnel addresses stay the same, so database connection strings and allow lists keep working. Moving to a new server means ordering it and migrating, as in moving to a bigger plan.

Can I connect my laptop to the same mesh?

Yes. Give it a tunnel address such as 10.20.0.50, list the servers as peers, and keep PersistentKeepalive = 25 because a laptop sits behind NAT. Servers then list the laptop without an Endpoint line.

How do I rotate a key?

Generate a new pair on that server, replace its PrivateKey, put the new public key into every other peer config, and run wg syncconf everywhere. The old key stops working as soon as no peer lists it.