Skip to main content

Wireguard Ubuntu Deployment

Installation

Enabling IP Forwarding

This equivalent to commenting the following 2 lines below in /etc/sysctl.conf file and then running sudo sysctl -p

Starting Wireguard & Making it a System Service

This is done so Wireguard always starts on system reboot

Opening Ports

If you're using UFW for your firewall open up the necessary ports for Wireguard. 51820 is the standard Wireguard port but feel free to use a non-standard port.

Server Configuration

Create a configuration file in /etc/wireguard/wg0.conf. An example configuration is below. If you need a private public key pair you can generate one in tunsafe (windows wireguard client).

Server Config Explanation for [Interface]

Be aware that these iptables entries in PostUp & PostDown are for a given interface. Make sure that your VM’s interface is captured on here you can check with ip a. In this above config example if you scroll right, you can see that the VM’s interface is eth0. Additionally, and worth noting, also make sure that your wireguard interface also matches the reference on the iptables entry. In this above config example, the wireguard interface is wg0.

For Address = 10.xx.xx.xx/xx create and choose an arbitrary “Private IP address” different from other subnets on this VM’s network to avoid IP conflict. Also specify the IP range you’re going to use like /24 or /20 etc. You can use a program line tunsafe (Windows) to generate these keys or you can use line 14+15 here.

SaveConfig = true / false. This setting when set to "true" will automatically save the current live config in standard format into your wg0.conf file whenever wireguard service is turned off. Because it is in standard format any comments you made to the wg0.conf file while be gone. Set this to false if you don't want this to happen. Set this to true if you'd like to add clients while the server is live without turning it off.

Server Config Explanation for [Peer]

For peer just keep incrementing your arbitrary IP address by one & use /32 because it is one IP. Then enter in their public key.

Finally start your wireguard service with.

Adding Clients to Server

Use Method#1 if you're new. Method #2 and #3 are advanced.

Method #1: While Wireguard Is Off
Method #2: While Wireguard Is Live (Restarting Interface)

This method requires SaveConfig = true in your config.

Adding a peer (Changes not saved yet)

Check if new peer's public key and ip shows up with

Finally do a


Method #3: While Wireguard Is Live (wg-quick save wg0)

Also requires SaveConfig = true in your config.

The difference with using a wg-quick save is that you have to do the 4th command of route add which is easy to fat finger and screw things up.

Generating Client Configurations For Users

Example configuration. Please read the gotchas for each OS.

A couple of gotchas to note.

In Linux, the Address = line needs to end in /32.

In Mac OS & Windows the Address = lines needs to end in /24 or the subnet assigned.

Also in Linux the DNS = line cannot be there it has to be erased.

In Mac OS the DNS = line needs to be there otherwise client cannot browse Internet.

In Windows Tunsafe the DNS = line is optional. In Windows Wireguard the DNS = line is required.

Optional Configurations

Isolating Wireguard Clients From Each Other

This can be achieved with the following IP Tables command below assuming your wireguard interface is "wg0"

Command References