WireGuard VPN site-to-site | Ubuntu (2024)

Another usual VPN configuration where one could deploy WireGuard is to connect two distinct networks over the internet. Here is a simplified diagram:

 ┌─────── WireGuard tunnel ──────┐ │ 10.10.9.0/31 │ │ │ 10.10.9.0 wgA│ xx │wgB 10.10.9.1 ┌─┴─┐ xxx xxxx ┌─┴─┐ alpha site │ │ext xx xx ext│ │ beta site │ ├─── x x ───┤ │ 10.10.10.0/24 │ │ xx xx │ │ 10.10.11.0/24 │ │ x x │ │ └─┬─┘ x x └─┬─┘ 10.10.10.1│ xx x │10.10.11.1 ...┌─────────┬────┘ xx xxx xx └───┬─────────┐... │ │ xx xxxxx │ │ │ │ │ │ ┌─┴─┐ ┌─┴─┐ public internet ┌─┴─┐ ┌─┴─┐ │ │ │ │ │ │ │ │ └───┘ └───┘ └───┘ └───┘

The goal here is to seamlessly integrate network alpha with network beta, so that systems on the alpha site can transparently access systems on the beta site, and vice-versa.

Such a setup has a few particular details:

  • Both peers are likely to be always up and running.
  • We can’t assume one side will always be the initiator, like the laptop in a coffee shop scenario.
  • Because of the above, both peers should have a static endpoint, like a fixed IP address, or valid domain name.
  • Since we are not assigning VPN IPs to all systems on each side, the VPN network here will be very small (a /31, which allows for two IPs) and only used for routing. The only systems with an IP in the VPN network are the gateways themselves.
  • There will be no NAT applied to traffic going over the WireGuard network. Therefore, the networks of both sites must be different and not overlap.

This is what an MTR (My Traceroute) report from a system in the beta network to an alpha system will look like:

ubuntu@b1:~$ mtr -n -r 10.10.10.230Start: 2022-09-02T18:56:51+0000HOST: b1 Loss% Snt Last Avg Best Wrst StDev 1.|-- 10.10.11.1 0.0% 10 0.1 0.1 0.1 0.2 0.0 2.|-- 10.10.9.0 0.0% 10 299.6 299.3 298.3 300.0 0.6 3.|-- 10.10.10.230 0.0% 10 299.1 299.1 298.0 300.2 0.6

Note:
Technically, a /31 Classless Inter-Domain Routing (CIDR) network has no usable IP addresses, since the first one is the network address, and the second (and last) one is the broadcast address. RFC 3021 allows for it, but if you encounter routing or other networking issues, switch to a /30 CIDR and its two valid host IPs.

Configure WireGuard

On the system that is the gateway for each site (that has internet connectivity), we start by installing WireGuard and generating the keys. For the alpha site:

$ sudo apt install wireguard$ wg genkey | sudo tee /etc/wireguard/wgA.key$ sudo cat /etc/wireguard/wgA.key | wg pubkey | sudo tee /etc/wireguard/wgA.pub

And the configuration on alpha will be:

[Interface]PostUp = wg set %i private-key /etc/wireguard/%i.keyAddress = 10.10.9.0/31ListenPort = 51000[Peer]# beta sitePublicKey = <contents of /etc/wireguard/wgB.pub>AllowedIPs = 10.10.11.0/24,10.10.9.0/31Endpoint = <beta-gw-ip>:51000

On the gateway for the beta site we take similar steps:

$ sudo apt install wireguard$ wg genkey | sudo tee /etc/wireguard/wgB.key$ sudo cat /etc/wireguard/wgB.key | wg pubkey | sudo tee /etc/wireguard/wgB.pub

And create the corresponding configuration file for beta:

[Interface]Address = 10.10.9.1/31PostUp = wg set %i private-key /etc/wireguard/%i.keyListenPort = 51000[Peer]# alpha sitePublicKey = <contents of /etc/wireguard/wgA.pub>AllowedIPs = 10.10.10.0/24,10.10.9.0/31Endpoint = <alpha-gw-ip>:51000

Important:
WireGuard is being set up on the gateways for these two networks. As such, there are no changes needed on individual hosts of each network, but keep in mind that the WireGuard tunneling and encryption is only happening between the alpha and beta gateways, and NOT between the hosts of each network.

Bring the interfaces up

Since this VPN is permanent between static sites, it’s best to use the systemd unit file for wg-quick to bring the interfaces up and control them in general. In particular, we want them to be brought up automatically on reboot events.

On alpha:

$ sudo systemctl enable --now wg-quick@wgA

And similarly on beta:

$ sudo systemctl enable --now wg-quick@wgB

This both enables the interface on reboot, and starts it right away.

Firewall and routing

Both gateways probably already have some routing and firewall rules. These might need changes depending on how they are set up.

The individual hosts on each network won’t need any changes regarding the remote alpha or beta networks, because they will just send that traffic to the default gateway (as any other non-local traffic), which knows how to route it because of the routes that wg-quick added.

In the configuration we did so far, there have been no restrictions in place, so traffic between both sites flows without impediments.

In general, what needs to be done or checked is:

  • Make sure both gateways can contact each other on the specified endpoint addresses and UDP port. In the case of this example, that is port 51000. For extra security, create a firewall rule that only allows each peer to contact this port, instead of the Internet at large.

  • Do NOT masquerade or NAT the traffic coming from the internal network and going out via the WireGuard interface towards the other site. This is purely routed traffic.

  • There shouldn’t be any routing changes needed on the gateways, since wg-quick takes care of adding the route for the remote site, but do check the routing table to see if it makes sense (ip route and ip route | grep wg are a good start).

  • You may have to create new firewall rules if you need to restrict traffic between the alpha and beta networks.

    For example, if you want to prevent SSH between the sites, you could add a firewall rule like this one to alpha:

    $ sudo iptables -A FORWARD -i wgA -p tcp --dport 22 -j REJECT

    And similarly on beta:

    $ sudo iptables -A FORWARD -i wgB -p tcp --dport 22 -j REJECT

    You can add these as PostUp actions in the WireGuard interface config. Just don’t forget to remove them in the corresponding PreDown hook, or you will end up with multiple rules.

Previous Peer-to-site (inside device) Next Default gateway

This page was last modified 5 days ago. Help improve this document in the forum.

WireGuard VPN site-to-site | Ubuntu (2024)

FAQs

Can WireGuard do site to site? ›

If you're required to share information or resources between intranets from different locations, such as offices, chain stores, using site to site VPN with WireGuard® can quickly help you build up your private network to connect all these places.

Is WireGuard P2P? ›

Technically every WireGuard tunnel is a peer to peer connection, but there are three main ways a WireGuard tunnel can be configured depending on whether or not a peer endpoint is known or defined: Site-to-Site (peer endpoint filled in on both sides) Remote Access “Server” (endpoint only filled in on remote peers)

Does WireGuard have a GUI? ›

Wireguard GUI is a Debian/Linux application that allow you to manage your Wireguard VPN configuration.

How do I use WireGuard as a VPN? ›

How to get started with WireGuard VPN
  1. Sign up with UpCloud. ...
  2. Deploy a new cloud server. ...
  3. Installing WireGuard. ...
  4. IP forwarding. ...
  5. Configuring firewall rules. ...
  6. Generating private and public keys. ...
  7. Generate server config. ...
  8. Starting WireGuard and enabling it at boot.

Which is better site to site IPsec or WireGuard? ›

Compared to IPsec, the WireGuard connection has a 20% lower latency and a 15% higher throughput. When it comes to performance, WireGuard usually performs better than IPSec and even quicker than other VPN protocols like OpenVPN.

Which is better site to site VPN WireGuard or OpenVPN? ›

The biggest notable differences between WireGuard and OpenVPN are speed and security. While WireGuard is generally faster, OpenVPN provides heavier security. The differences between these two protocols are also what make up their defining features.

How do I connect to another peer in WireGuard? ›

You can configure multiple peers within a single WireGuard interface, and then enable or disable them as needed. Here's how you can do it: Navigate to "Network" > "Interfaces" and click on the WireGuard interface you want to modify. In the "WireGuard" tab, click on "Add Peer" to add a new peer.

Is P2P better than VPN? ›

A P2P network, on its own, is not as secure as a regular VPN. Although it allows direct connection between users, a P2P network may not have the encryption capabilities a VPN has.

Is anything better than WireGuard? ›

OpenVPN is supported by more routers than WireGuard, and it also can operate with TCP, which offers more stable connections than UDP, and is generally better for remote connections as well.

How to access WireGuard UI? ›

Accessing the Wireguard-UI and configuration

Your docker-compose file will start the WireGuard-UI application on port 80 of your EC2 instance. You can now access the application at “http://<your-ec2-instance-ip>”.

Is WireGuard a full tunnel? ›

WireGuard supports Full Tunnel VPN routing. This means that when the client connects, all Internet traffic routes over the tunnel. This is useful to ensure that the device is fully protected by all security layers of NG Firewall.

Does WireGuard hide IP? ›

While OpenVPN doesn't keep any personal information, such as IP addresses are stored by WireGuard on the VPN server until it reboots. This poses a privacy concern since if the server is hacked in any way, users' IP addresses will be disclosed, revealing their online activities.

How much does WireGuard cost? ›

Since WireGuard and OpenVPN are free software, there is no expense associated with using them. Though there are some free solutions, you'll still need to pay for a VPN subscription. Since WireGuard and OpenVPN are free software, there is no expense associated with using them.

Is WireGuard the best VPN? ›

It works very quickly, provides a high level of security, and is written with relatively few lines of code. The lightweight nature of the protocol code is important, because it makes deployment and debugging easier. In short, WireGuard is a faster, more effective way to protect and transfer data across a VPN.

Why not to use WireGuard? ›

It is extensible that new cryptographic primitives can be added. WireGuard does not have that. That means WireGuard will break at some point, because one of the cryptographic primitives will weaken or entirely break at some point.

What can you do with WireGuard? ›

WireGuard can establish a new connection very quickly, allowing you to switch between networks and routers without waiting for your VPN to slowly reconnect. With other protocols, a network switch could result in a slow VPN reconnection. Open source software.

Is WireGuard as safe as OpenVPN? ›

Conclusion. Both OpenVPN and WireGuard are really secure open-source VPN protocols, if properly implemented. However, WireGuard is newer and faster than OpenVPN, because it was designed with modern devices and processors in mind. It is also easier to maintain.

Does WireGuard route all traffic? ›

WireGuard can be set up to route all traffic through the VPN, and not just specific remote networks. There could be many reasons to do this, but mostly they are related to privacy.

References

Top Articles
Latest Posts
Article information

Author: Arline Emard IV

Last Updated:

Views: 5894

Rating: 4.1 / 5 (52 voted)

Reviews: 83% of readers found this page helpful

Author information

Name: Arline Emard IV

Birthday: 1996-07-10

Address: 8912 Hintz Shore, West Louie, AZ 69363-0747

Phone: +13454700762376

Job: Administration Technician

Hobby: Paintball, Horseback riding, Cycling, Running, Macrame, Playing musical instruments, Soapmaking

Introduction: My name is Arline Emard IV, I am a cheerful, gorgeous, colorful, joyous, excited, super, inquisitive person who loves writing and wants to share my knowledge and understanding with you.