As soon as you run more than one server — a web server in Hong Kong and a database in Tokyo, an app in Los Angeles and backups at the office — you need them to talk to each other securely. Exposing a database or Redis port to the internet is a common cause of breaches. WireGuard creates an encrypted private network on top of the internet with a few lines of configuration. It is built into the Linux kernel (5.6+), fast, and much simpler than IPsec or OpenVPN.
This guide sets up a private network between servers, so internal services listen only on private addresses.
Use WireGuard to connect infrastructure you control, and make sure your use complies with the laws of every location involved and with our Acceptable Use Policy.
How WireGuard works
Each machine (a peer) has a key pair. Each peer’s config lists the other peers’ public keys, which addresses they may use inside the tunnel (AllowedIPs), and where to reach them (Endpoint). There is no central server in the protocol; you choose the topology.
We will build this network:
| Server | Location | Public IP | WireGuard IP |
|---|---|---|---|
| web1 | Hong Kong | 203.0.113.10 | 10.10.0.1 |
| db1 | Tokyo | 198.51.100.20 | 10.10.0.2 |
| backup1 | Los Angeles | 192.0.2.30 | 10.10.0.3 |
Use a private range that does not clash with networks you already have.
Step 1: Install
sudo apt install wireguard # Ubuntu 20.04+ / Debian 11+
sudo dnf install wireguard-tools # AlmaLinux / Rocky 8+ (module in kernel)
Step 2: Generate keys on each server
umask 077
wg genkey | sudo tee /etc/wireguard/private.key | wg pubkey | sudo tee /etc/wireguard/public.key
Keep private.key on that server only. You will copy each server’s public key into the others’ configs.
Step 3: Configure each peer
On web1 (/etc/wireguard/wg0.conf):
[Interface]
Address = 10.10.0.1/24
ListenPort = 51820
PrivateKey = <web1 private key>
[Peer]
# db1 (Tokyo)
PublicKey = <db1 public key>
Endpoint = 198.51.100.20:51820
AllowedIPs = 10.10.0.2/32
PersistentKeepalive = 25
[Peer]
# backup1 (Los Angeles)
PublicKey = <backup1 public key>
Endpoint = 192.0.2.30:51820
AllowedIPs = 10.10.0.3/32
PersistentKeepalive = 25
On db1:
[Interface]
Address = 10.10.0.2/24
ListenPort = 51820
PrivateKey = <db1 private key>
[Peer]
PublicKey = <web1 public key>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.10.0.1/32
PersistentKeepalive = 25
[Peer]
PublicKey = <backup1 public key>
Endpoint = 192.0.2.30:51820
AllowedIPs = 10.10.0.3/32
PersistentKeepalive = 25
And similarly on backup1. This is a full mesh: every server connects directly to every other, which gives the lowest latency between locations.
Key points:
AllowedIPsworks as both a route and an access list: packets to 10.10.0.2 go to db1, and only packets from 10.10.0.2 are accepted from db1.PersistentKeepalive = 25keeps the tunnel open through NAT and stateful firewalls. On servers with public IPs talking to each other it is optional but harmless.- Lock down the files:
sudo chmod 600 /etc/wireguard/wg0.conf.
Step 4: Open the firewall
Allow UDP 51820 only from the other servers’ public IPs:
# UFW (Ubuntu / Debian)
sudo ufw allow from 198.51.100.20 to any port 51820 proto udp
sudo ufw allow from 192.0.2.30 to any port 51820 proto udp
# firewalld (AlmaLinux / Rocky)
sudo firewall-cmd --permanent --add-rich-rule='rule family=ipv4 source address=198.51.100.20 port port=51820 protocol=udp accept'
sudo firewall-cmd --reload
See UFW and firewalld guides for details.
Step 5: Start and enable
sudo systemctl enable --now wg-quick@wg0
sudo wg show
wg show lists each peer with its latest handshake and bytes transferred. A handshake within the last two minutes means the tunnel works. Test:
ping -c 3 10.10.0.2
Step 6: Move internal services onto the private network
Now bind databases and caches to the WireGuard address instead of all interfaces.
MariaDB/MySQL on db1 (/etc/mysql/mariadb.conf.d/50-server.cnf or my.cnf):
bind-address = 10.10.0.2
Create database users that may connect only from the web server’s tunnel address:
CREATE USER 'app'@'10.10.0.1' IDENTIFIED BY 'a-long-random-password';
GRANT ALL PRIVILEGES ON appdb.* TO 'app'@'10.10.0.1';
Redis: bind 10.10.0.2 and a password with requirepass.
Then check from outside that the public IP no longer exposes these ports:
sudo ss -tulpn | grep -E "3306|6379" # should show 10.10.0.2, not 0.0.0.0
More on database security in installing and securing MariaDB.
Performance across regions
WireGuard adds little overhead; the speed between locations is dominated by distance and route. Typical round trips: Hong Kong–Tokyo ~40–60 ms, Hong Kong–Los Angeles ~150 ms. Keep chatty connections (a web app querying its database dozens of times per page) within the same location, and use cross-region links for replication, backups and APIs.
If large transfers through the tunnel are slow, enable BBR on both ends, and consider lowering the MTU in [Interface] (MTU = 1380) if you see stalls with large packets.
Connecting an office or laptop
A laptop or office router can be added as another peer with its own key and tunnel IP. Because it is usually behind NAT without a fixed public IP, omit Endpoint for it on the servers, set Endpoint to one server on the laptop, and use PersistentKeepalive = 25 on the laptop side. Official WireGuard apps exist for Windows, macOS, iOS and Android.
Troubleshooting
| Symptom | Check |
|---|---|
| No handshake | Firewall on either side blocking UDP 51820; wrong Endpoint IP or port |
| Handshake OK, ping fails | AllowedIPs mismatch; tunnel IP typo; local firewall dropping traffic on wg0 |
| Works, then stops after idle | Add PersistentKeepalive = 25 |
| Slow or stalls on large transfers | Lower MTU; enable BBR |
| Changes not applied | sudo systemctl restart wg-quick@wg0 |
sudo wg show and sudo journalctl -u wg-quick@wg0 are your main diagnostic tools.