How To Create a Point-To-Point VPN with WireGuard on Ubuntu 16.04

Introduction

WireGuard is a modern, high-performance VPN designed to be easy to use while providing robust security. WireGuard focuses only on providing a secure connection between parties over a network interface encrypted with public key authentication. This means that, unlike most VPNs, no topology is enforced so different configurations can be achieved by manipulating the surrounding networking configuration. This model offers great power and flexibility that can be applied according to your individual needs.

One of the simplest topologies that WireGuard can use is a point-to-point connection. This establishes a secure link between two machines without mediation by a central server. This type of connection can also be used between more than two members to establish a mesh VPN topology, where each individual server can talk to its peers directly. Because each host is on equal footing, these two topologies are best suited for establishing secure messaging between servers as opposed to using a single server as a gateway to route traffic through.

In this guide, we will demonstrate how to establish a point-to-point VPN connection with WireGuard using two Ubuntu 16.04 servers. We will start by installing the software and then generating cryptographic key pairs for each host. Afterwards, we will create a short configuration file to define the peer's connection information. Once we start up the interface, we will be able to send secure messages between the servers over the WireGuard interface.

Prerequisites

To follow along with this guide, you will need access to two Ubuntu 16.04 servers. On each server, you will need to create a non-root user with sudo privileges to perform administrative actions. You will also need a basic firewall configured on each system. You can fulfill these requirements by completing the following tutorial:

When you are ready to continue, log into each server with your sudo user.

Installing the Software

The WireGuard project provides a PPA with up-to-date packages for Ubuntu systems. We will need to install WireGuard on both of our servers before we can continue. On each server, perform the following actions.

First, add the WireGuard PPA to the system to configure access to the project's packages:

sudo add-apt-repository ppa:wireguard/wireguard

Press ENTER when prompted to add the new package source to your apt configuration. Once the PPA has been added, update the local package index to pull down information about the newly available packages and then install the WireGuard kernel module and userland components:

sudo apt-get update

sudo apt-get install wireguard-dkms wireguard-tools

Next, we can begin configuring WireGuard on each of our servers.

Creating a Private Key

Each participant in a WireGuard VPN authenticates to its peers using public keys cryptography. Connections between new peers can be established by exchanging public keys and performing minimal configuration.

To generate a private key and write it directly to a WireGuard configuration file, type the following on each server:

The first command writes the initial contents of a configuration file to /etc/wireguard/wg0.conf. The umask value in a sub-shell so that we create the file with restricted permissions without affecting our regular environment.

The second command generates a private key using WireGuard's wg command and writes it directly to our restricted configuration file. We also pipe the key back into the wg pubkey command to derive the associated public key, which we write to a file called /etc/wireguard/publickey for easy reference. We will need to exchange the key in this file with the second server as we define our configuration.

Creating an Initial Configuration File

Next, we will open the configuration file in an editor to set up a few other details:

sudo nano /etc/wireguard/wg0.conf

Inside, you should see your generated private key defined in a section called [Interface]. This section contains the configuration for the local side of the connection.

Configuring the Interface Section

We need to define the VPN IP address this node will use and the port that it will listen on for connections from peers. Begin by adding ListenPort and SaveConfig lines so that your file looks like this:

This sets the port that WireGuard will listen on. This can be any free, bindable port, but in this guide we will set up our VPN on port 5555 for both servers. Set the ListenPort on each host to the port you've selected:

We also set SaveConfig to true. This will tell the wg-quick service to automatically save its active configuration to this file at shutdown.

Note: When SaveConfig is enabled, the wg-quick service will overwrite the contents of the /etc/wireguard/wg0.conf file whenever the service shuts down. If you need to modify the WireGuard configuration, either shut down the wg-quick service prior to editing the /etc/wireguard/wg0.conf file or make the changes to the running service using the wg command (these will be be saved in the file when the service shuts down). Any changes made to the configuration file while the service is running will be overwritten when wg-quick stores its active configuration.

Next, add a unique Address definition to each server so that the wg-quick service can set the network information when it brings up the WireGuard interface. We will use the 10.0.0.0/24 subnet as the address space for our VPN. For each computer, you will need to pick a unique address within this range (10.0.0.1 to 10.0.0.254) and specify the address and subnet using CIDR notation.

We will give our first server an address of 10.0.0.1, which is represented as 10.0.0.1/24 in CIDR notation:

We can enter the information about the server's peers either within the configuration file or manually using the wg command later on. As mentioned above, the wg-quick service with the SaveConfig option set to true will mean that the peer information will eventually be written to the file with either method.

To demonstrate both ways of defining peer identities, we will create a [Peer] section in the second server's configuration file but not the first. You can save and close the configuration file for the first server (the one defining the 10.0.0.1 address) now.

Defining the Peer Section

In the configuration file that's still open, create a section called [Peer] below the entries in the [Interface] section.

Begin by setting the PublicKey to the value of the first server's public key. You can find this value by typing cat /etc/wireguard/publickey on the opposite server. We will also set AllowedIPs to the IP addresses that are valid inside the tunnel. Since we know the specific IP address that the first server is using, we can input that directly, ending with /32 to indicate a range that contains single IP value:

Finally, we can set the Endpoint to the first server's public IP address and the WireGuard listening port (we used port 5555 in this example). WireGuard will update this value if it receives legitimate traffic from this peer on another address, allowing the VPN to adapt to roaming conditions. We set the initial value so that this server can initiate contact:

To complete the connection, we now need to add the second server's peering information to the first server using the wg command.

Adding the Missing Peer Information on the Command Line

On the first server (the one that doesn't display peer information), enter the peering information manually using the following format. The second server's public key can be found in the output of sudo wg from the second server:

If everything is working correctly, you can save the configuration on the first server back to the /etc/wireguard/wg0.conf file by restarting the service:

sudo systemctl restart wg-quick@wg0

If you want to start the tunnel at boot, you can enable the service on each machine by typing:

sudo systemctl enable wg-quick@wg0

The VPN tunnel should now be automatically started whenever the machine boots.

Conclusion

WireGuard is a great option for many use cases due to its flexibility, light-weight implementation, and modern cryptography. In this guide, we installed WireGuard on two Ubuntu 16.04 servers and configured each host as a server with a point-to-point connection to its peer. This topology is ideal for establishing server-to-server communication with peers where each side is an equal participant or where hosts might have to establish ad-hoc connections to other servers.