Connecting two Debian servers via WireGuard

Русский · English · Español · Français · Deutsch

Instructions for Debian 12 and Debian 13: two servers receive permanent addresses in an encrypted tunnel. These addresses can be used to transfer backups, SSH, and connect to internal services.

Commands are executed from root. The basic example is designed for two servers with available public IPv4 addresses. The option with one server behind NAT is described below.

1. Addresses and parameters

Parameter Server 1 Server 2
External IP, example 203.0.113.10 198.51.100.20
Address inside the tunnel 10.10.0.1/32 10.10.0.2/32
Interface name wg-cluster wg-cluster
WireGuard Port 51821/UDP 51821/UDP
Configuration /etc/wireguard/wg-cluster.conf /etc/wireguard/wg-cluster.conf

External IPs in the table are addresses for documentation. Replace them with your addresses in all firewall configurations and rules. Check that the selected internal addresses, interface name and UDP port are free. If wg-cluster is already in use, choose a different name and replace it in all commands and file names.

Mask /32 is intentional here: each server is assigned one address, and a route to its neighbor creates wg-quick based AllowedIPs.

For exchange between the servers themselves, local chains are needed INPUT and OUTPUT. Enabling IP forwarding and MASQUERADE is required for other scenarios – for example, when traffic from a separate local network is routed through the server.

2. Installation on both servers

apt update
apt install wireguard iproute2 iputils-ping nano
install -d -m 700 /etc/wireguard

Checking existing interfaces, routes and occupied port:

wg show
ip -br address
ip route
ss -lunp '( sport = :51821 )'

Installation is supported by standard Debian repositories. In a container, the availability of WireGuard is further dependent on the kernel and host permissions.

3. Key generation

Each server needs its own key pair. The following block creates a private key only if the file does not already exist, and obtains a public key from it:

(
    set -eu
    umask 077
    if [ ! -e /etc/wireguard/wg-cluster.key ]; then
        wg genkey > /etc/wireguard/wg-cluster.key
    fi
    wg pubkey < /etc/wireguard/wg-cluster.key > /etc/wireguard/wg-cluster.pub
    chmod 600 /etc/wireguard/wg-cluster.key /etc/wireguard/wg-cluster.pub
)

Show public key:

cat /etc/wireguard/wg-cluster.pub

Pass the public key of the first server to the second, and the second to the first. The private key remains on its server. To fill out the configuration, you can view it locally:

cat /etc/wireguard/wg-cluster.key

In the field PrivateKey is the key itself, not the path to the file. Working keys are saved between restarts; There is no need to generate new ones every time you start.

4. Server configuration 1

Create a file with permissions only for root and open it with an editor. These commands are for the new configuration file:

umask 077
touch /etc/wireguard/wg-cluster.conf
chmod 600 /etc/wireguard/wg-cluster.conf
nano /etc/wireguard/wg-cluster.conf

Contents:

[Interface]
Address = 10.10.0.1/32
ListenPort = 51821
PrivateKey = PRIVATE_KEY_SERVER_1

[Peer]
PublicKey = PUBLIC_KEY_SERVER_2
Endpoint = 198.51.100.20:51821
AllowedIPs = 10.10.0.2/32

Replace PRIVATE_KEY_SERVER_1 with the private key of the first server, and PUBLIC_KEY_SERVER_2 – the public key of the second.

5. Server configuration 2

Create and open a file in the same way:

umask 077
touch /etc/wireguard/wg-cluster.conf
chmod 600 /etc/wireguard/wg-cluster.conf
nano /etc/wireguard/wg-cluster.conf

Contents:

[Interface]
Address = 10.10.0.2/32
ListenPort = 51821
PrivateKey = PRIVATE_KEY_SERVER_2

[Peer]
PublicKey = PUBLIC_KEY_SERVER_1
Endpoint = 203.0.113.10:51821
AllowedIPs = 10.10.0.1/32

Replace PRIVATE_KEY_SERVER_2 with the private key of the second server, and PUBLIC_KEY_SERVER_1 is the public key of the first.

AllowedIPs determines the addresses forwarded to the neighbor and the valid source addresses of decrypted packets from it. For this task, the address of a neighboring server with /32. Meaning 0.0.0.0/0 means another task – directing all IPv4 traffic through the tunnel.

6. Firewall

Two types of permissions are needed: incoming UDP WireGuard on an external address and traffic between internal addresses via wg-cluster. In the provider panel, also allow incoming 51821/UDP: on server 1 from external IP server 2, on server 2 – from external IP server 1.

Below are several options. Use the option for the firewall manager that is already used on the server. The tunnel rules allow the neighboring server to access all local services through the WireGuard address; If necessary, narrow them down to the required ports.

If the rules are managed by iptables

On server 1:

iptables -C INPUT -p udp -s 198.51.100.20/32 --dport 51821 -j ACCEPT || iptables -I INPUT 1 -p udp -s 198.51.100.20/32 --dport 51821 -j ACCEPT
iptables -C INPUT -i wg-cluster -s 10.10.0.2/32 -d 10.10.0.1/32 -j ACCEPT || iptables -I INPUT 1 -i wg-cluster -s 10.10.0.2/32 -d 10.10.0.1/32 -j ACCEPT

On server 2:

iptables -C INPUT -p udp -s 203.0.113.10/32 --dport 51821 -j ACCEPT || iptables -I INPUT 1 -p udp -s 203.0.113.10/32 --dport 51821 -j ACCEPT
iptables -C INPUT -i wg-cluster -s 10.10.0.1/32 -d 10.10.0.2/32 -j ACCEPT || iptables -I INPUT 1 -i wg-cluster -s 10.10.0.1/32 -d 10.10.0.2/32 -j ACCEPT

-C checks for the presence of a rule, and -I INPUT 1 adds the missing rule to the beginning of the chain. These commands do not create duplicates when executed repeatedly. A rule not found message from the first check is acceptable if the next command successfully added it. Check the existing rule for its location before the blocking rules.

Check:

iptables -nvL INPUT --line-numbers
iptables -S OUTPUT

If outgoing traffic is restricted, also allow outgoing UDP to the neighbor’s external address on the port 51821 and outgoing traffic via wg-cluster to its internal address.

To save, use the current firewall control mechanism. If already installed iptables-persistent:

netfilter-persistent save
systemctl enable netfilter-persistent

On a server with independent iptables, where there is no saving mechanism yet, you can install it and save the rules:

apt install iptables-persistent
netfilter-persistent save
systemctl enable netfilter-persistent

Saving saves the entire current set of rules, including the rules of other programs. If the rules are managed by Webmin, enter and save these permissions in the firewall module you are using. If there are other nftables chains with blocking, iptables permissions alone may not be enough: permissions must pass all the filtering in place.

If you already use UFW

On server 1:

ufw insert 1 allow proto udp from 198.51.100.20 to any port 51821
ufw insert 1 allow in on wg-cluster from 10.10.0.2 to 10.10.0.1

On server 2:

ufw insert 1 allow proto udp from 203.0.113.10 to any port 51821
ufw insert 1 allow in on wg-cluster from 10.10.0.1 to 10.10.0.2

Check: ufw status numbered. The UFW maintains these rules. Enabling UFW on a server where it has not been used before requires separate configuration of access, including SSH.

If you use firewalld

Check which zone handles the external interface:

firewall-cmd --get-active-zones

In the following commands public is an example external zone name. Replace it with the active zone on your server.

On server 1:

firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="198.51.100.20/32" port port="51821" protocol="udp" accept'
firewall-cmd --permanent --zone=trusted --add-interface=wg-cluster
firewall-cmd --reload

On server 2:

firewall-cmd --permanent --zone=public --add-rich-rule='rule family="ipv4" source address="203.0.113.10/32" port port="51821" protocol="udp" accept'
firewall-cmd --permanent --zone=trusted --add-interface=wg-cluster
firewall-cmd --reload

Zone trusted is intended here only for the new WireGuard interface. It gives the neighbor full access to services through the tunnel. To restrict access, use a separate zone with the necessary services. If the interface is already assigned to another zone, check the existing setting first. Rebooting firewalld applies the permanent configuration; temporary rules that must be preserved must first be formalized as permanent.

7. Startup and autostart

On both servers:

systemctl enable --now wg-quick@wg-cluster
systemctl status wg-quick@wg-cluster --no-pager

For this systemd service, the status is active (exited) is normal: the utility created an interface and then exited. The interface itself continues to work.

If the interface was previously launched manually via wg-quick up, first bring its control to one method. Trying to create an already existing interface again will result in an error.

8. Communication check

On server 1:

ping -c 4 10.10.0.2
ip route get 10.10.0.2
wg show wg-cluster

On server 2:

ping -c 4 10.10.0.1
ip route get 10.10.0.1
wg show wg-cluster

Successful check: there are answers ping, route to neighbor uses dev wg-cluster, and wg show after traffic exchange shows latest handshake and counters transfer in both directions.

Without traffic and without keepalive, the interface may not yet perform a handshake. So first send a ping, then check the status. Systemd status alone is not enough to ping a neighbor.

To access the service, use an internal address, for example from server 1:

ssh admin@10.10.0.2

Here admin – example: specify an existing account and your SSH port using -p. For backup please specify 10.10.0.2 in the destination settings on the first server, and 10.10.0.1 is in second place.

The service must listen to the WireGuard address or a suitable public address. Service that listens only 127.0.0.1, not accessible through the tunnel. You can check listening sockets with the command ss -lntup. If the service is bound exclusively to the WireGuard address, consider the startup order: the interface should appear before the service.

9. Tunnel control

After changing the configuration:

systemctl restart wg-quick@wg-cluster

Restarting briefly interrupts the tunnel. Perform it through an independent connection to the server if you manage the server via the same tunnel.

Stop:

systemctl stop wg-quick@wg-cluster

Stop and remove from autorun:

systemctl disable --now wg-quick@wg-cluster

Latest launch messages:

journalctl -u wg-quick@wg-cluster -b -n 80 --no-pager

10. If server 2 is behind NAT

Let server 1 have an accessible public address, and server 2 initiate communication from behind NAT.

On server 1, remove the line Endpoint from section [Peer] server 2. WireGuard learns the external address and port of the neighbor from correctly authenticated packets.

Leave on server 2 Endpoint with server address 1 and add to the same section [Peer]:

PersistentKeepalive = 25

This maintains the NAT state when there is no useful traffic. For two directly reachable servers, keepalive is usually not needed.

On server side 1, the UDP source address restriction must match the actual external NAT address. If this address changes, the fixed restriction on the previous IP will no longer work; it will require updating the rule or allowing incoming UDP to the selected port without such a restriction. An authenticated WireGuard-peer is still used to access internal services.

On the NAT side, outgoing UDP packets and responses to them must pass. For this scheme, forwarding the incoming port to server 2 is not required. If both servers are behind a NAT with no incoming UDP available, this instruction alone does not provide a direct connection: an accessible intermediary node is required, or at least one side must be configured to be reachable.

11. If it doesn’t work

Symptom What to check
Systemd startup error Log, key values, syntax, free port, no existing interface with the same name
No handshake after ping Public IP and UDP port in Endpoint, keys of opposite nodes, firewall of server and provider, reachability via NAT
Handshake is available, ping does not respond Interface addresses, AllowedIPs, routes, ICMP resolution and incoming traffic to wg-cluster
Ping works, the required service does not open Listening address, port, service access rules and firewall
Small packets pass, large transmission freezes MTU along the path and ICMP filtering; as a diagnostic step you can try MTU = 1380 in [Interface] on both servers and repeat the check

To view external UDP packets during ping:

apt install tcpdump
tcpdump -ni any udp port 51821

If the exchange is correct, there should be packets in both directions. The presence of UDP packets in itself does not yet prove successful authentication – check it through wg show.

Documentation