Introduction
WireGuard is a modern, fast, and secure VPN that is easy to configure. In this guide, we will walk through the process of setting up a site-to-site VPN using WireGuard. This will allow you to securely connect two networks, for example, your office network and your home network.
Prerequisites
- Two Linux servers, each on a different network. We will call them
site-aandsite-b. - Root access to both servers.
- A public IP address for each server.
Step 1: Install WireGuard
First, we need to install WireGuard on both servers.
On Debian/Ubuntu:
|
|
On CentOS/RHEL:
|
|
Step 2: Generate Keys
WireGuard works by encrypting traffic with public-key cryptography. Each server needs a private key and a public key.
On both site-a and site-b:
-
Go to the WireGuard configuration directory:
1cd /etc/wireguard -
Generate the private and public keys:
1wg genkey | tee privatekey | wg pubkey > publickeyThis will create two files:
privatekeyandpublickey. -
Secure the private key:
1chmod 600 privatekey
Step 3: Configure WireGuard
Now we need to create a configuration file for each server.
On site-a
-
Create a new configuration file:
1sudo nano /etc/wireguard/wg0.conf -
Add the following content to the file. Replace the placeholders with your actual values.
1 2 3 4 5 6 7 8 9[Interface] Address = 10.0.1.1/24 PrivateKey = <site-a-private-key> ListenPort = 51820 [Peer] PublicKey = <site-b-public-key> Endpoint = <site-b-public-ip>:51820 AllowedIPs = 10.0.2.0/24Address: The private IP address forsite-a’s WireGuard interface.PrivateKey: The content ofsite-a’sprivatekeyfile.ListenPort: The port WireGuard will listen on.PublicKey: The content ofsite-b’spublickeyfile.Endpoint: The public IP address and port ofsite-b.AllowedIPs: The IP address range of the network atsite-b.
On site-b
-
Create a new configuration file:
1sudo nano /etc/wireguard/wg0.conf -
Add the following content to the file. Replace the placeholders with your actual values.
1 2 3 4 5 6 7 8 9[Interface] Address = 10.0.2.1/24 PrivateKey = <site-b-private-key> ListenPort = 51820 [Peer] PublicKey = <site-a-public-key> Endpoint = <site-a-public-ip>:51820 AllowedIPs = 10.0.1.0/24Address: The private IP address forsite-b’s WireGuard interface.PrivateKey: The content ofsite-b’sprivatekeyfile.ListenPort: The port WireGuard will listen on.PublicKey: The content ofsite-a’spublickeyfile.Endpoint: The public IP address and port ofsite-a.AllowedIPs: The IP address range of the network atsite-a.
Step 4: Enable IP Forwarding
For the two sites to be able to communicate, we need to enable IP forwarding on both servers.
-
Edit the
sysctl.conffile:1sudo nano /etc/sysctl.conf -
Uncomment the following line:
1net.ipv4.ip_forward=1 -
Apply the changes:
1sudo sysctl -p
Step 5: Start WireGuard
Now we can start the WireGuard service on both servers.
-
Start the WireGuard interface:
1sudo wg-quick up wg0 -
Enable the WireGuard service to start on boot:
1sudo systemctl enable wg-quick@wg0
Step 6: Configure Firewall
You may need to configure your firewall to allow WireGuard traffic.
On both servers (using ufw):
|
|
Conclusion
You should now have a working site-to-site VPN with WireGuard. You can test the connection by pinging one server from the other using their WireGuard IP addresses.
|
|