Advanced
Searching inHow-to guides
How to search
  • Type the task in your own words (find port, open firewall, disk full) or paste an error message in quotes.
  • Choose "Quick answer" to see only the commands, or an area to stay in one field.
  • "two words" finds the exact phrase.
  • Plain words find names, texts and examples.
  • ↑ ↓ to move · Enter to open · Esc to close · / to search
Try:

How-to 110Network · VPN

How to connect two servers with a WireGuard VPN

Two servers in different places — a cloud VM and an office server, or two data centers — must talk privately, without opening database ports to the internet. WireGuard builds an encrypted tunnel with a few lines of config: each side has a key pair, and each side knows the other's public key. This how-to connects two servers, makes it start at boot, and fixes the usual "handshake but no traffic".

  • 5 Lessons
  • 10 Commands

$ run as a normal user# run as root (or with sudo)

110.1

The quick answer

On both servers:

dnf -y install wireguard-tools
umask 077; wg genkey | tee /etc/wireguard/private.key | wg pubkey > /etc/wireguard/public.key
cat /etc/wireguard/public.key

Then write wg0.conf on each side (step 2), and:

systemctl enable --now wg-quick@wg0
wg show
ping -c 3 10.8.0.2

On Ubuntu: apt-get install -y wireguard.

110.2

Step by step

  1. Plan the tunnel: a small private network only for the tunnel, 10.8.0.0/24 — server A (public IP 203.0.113.10) gets 10.8.0.1, server B gets 10.8.0.2. Only A needs an open UDP port (51820); B connects out to it.
  2. The configs — each file has its own private key and the other side's public key:
/etc/wireguard/wg0.conf (server A)
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <A private key>
 
[Peer]
PublicKey = <B public key>
AllowedIPs = 10.8.0.2/32
/etc/wireguard/wg0.conf (server B)
[Interface]
Address = 10.8.0.2/24
PrivateKey = <B private key>
 
[Peer]
PublicKey = <A public key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.8.0.1/32
PersistentKeepalive = 25
chmod 600 /etc/wireguard/wg0.conf

AllowedIPs is both the routing and the access list: only these source addresses are accepted from that peer. PersistentKeepalive keeps the tunnel open through NAT.

  1. Open the port on A (and in the cloud security group):
firewall-cmd --permanent --add-port=51820/udp && firewall-cmd --reload
  1. Start at boot on both:
systemctl enable --now wg-quick@wg0
  1. Reach a whole network behind the other side (for example the office LAN 192.168.10.0/24 behind B): add it to B's entry on A (AllowedIPs = 10.8.0.2/32, 192.168.10.0/24), turn on forwarding on B (net.ipv4.ip_forward = 1), and route/NAT there.
  2. Use the tunnel: let the database listen on, or allow only, the tunnel IP — for example PostgreSQL on A allows 10.8.0.2/32 in pg_hba.conf, and the firewall opens 5432 only on wg0.

110.3

Check that it worked

wg show
interface: wg0
  listening port: 51820
peer: Xy3…=
  endpoint: 198.51.100.7:43120
  allowed ips: 10.8.0.2/32
  latest handshake: 12 seconds ago
  transfer: 18.42 KiB received, 21.10 KiB sent
ping -c 3 10.8.0.2

A recent handshake, bytes in both directions, and ping works both ways.

110.4

Common problems

ProblemCauseFix
No latest handshakeUDP 51820 blocked, wrong endpoint, or wrong public key.Open the port (host + cloud); check Endpoint and keys.
Handshake OK, no pingAllowedIPs does not include the address, or a firewall on wg0.Fix AllowedIPs on both sides; allow traffic on the wg0 zone/interface.
Tunnel drops after idle timeNAT forgets the mapping.PersistentKeepalive = 25 on the side behind NAT.
Network behind B not reachableNo forwarding or return route.ip_forward=1 on B; route back to 10.8.0.0/24 in the office network.
RTNETLINK answers: Operation not supportedKernel module missing (old kernel / container).Update the kernel; WireGuard is built in since Linux 5.6.
Config readable by othersPrivate key leaked risk.chmod 600; rotate keys if it leaked.

Commands in this lesson

110.5

Go further

1What does AllowedIPs do in WireGuard?

It is the routing for that peer and its access list: only these source addresses are accepted from it.

2The handshake works but ping fails. What do you check?

AllowedIPs on both sides and the firewall rules on the wg0 interface.

All commands in this chapter