Skip to main content

Wireguard Ubuntu Deployment

Installation

sudo add-apt-repository ppa:wireguard/wireguard
sudo apt install wireguard
Enabling IPv4 Forwarding
sudo echo "net.ipv4.ip_forward = 1" >> /etc/sysctl.conf
sudo echo "net.ipv4.conf.all.proxy_arp = 1" >> /etc/sysctl.conf
sudo sysctl -p /etc/sysctl.conf

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

net.ipv4.ip_forward = 1
net.ipv4.conf.all.proxy_arp = 1
Making Wireguard a system service

The command below enables Wireguard as a system service so it always starts on system reboot.

sudo systemctl enable wg-quick@wg0.service

Opening UDP Ports

Before starting, you need to open a UDP port on your firewall pointing to the server. You can use any UDP port. I use 51820/udp because it is the standard Wireguard port.

If you have ufw (uncomplicated firewall) enabled, on the server you must also allow the port thru.

The commands below will allow ports 22/tcp and 51820/udp thru and enable UFW (a second firewall on the server). If you don't want to use UFW you can disable it with sudo ufw disable.

sudo ufw status # To check if ufw is enabled.
sudo ufw allow 22/tcp comment 'OpenSSH'
sudo ufw allow 51820/udp comment 'Wireguard'
sudo ufw enable
sudo ufw status numbered # Repeat to check if the new UFW rules were added.

Server Configuration

CreateFirst generate a private public key pair. You can do this in either…

  • Tunsafe, a Windows Wireguard client
  • or Ubuntu with the commands below

umask 077
wg genkey | tee privatekey | wg pubkey > publickey
# Key pairs are saved in same path you typed these commands in

Now create a configuration file in /etc/wireguard/wg0.conf. AnUse the example configuration is below. If you need a private public key pair you can generate one in tunsafe (windows wireguard client). Or you can generate one with the commands in line 17-21 referenced here.

Important!!! REPLACEIn the example config, replace eth0  below with your server’s actual interface name in lines 7-8. Ignore lines 11-18 because they are for reference. You can find yourthe server’s actual interface name with ip a
. Continue on below to replace the rest of the variables in the example config.

[Interface]
Address = 10.xx.xx.1/24
ListenPort = 51820
PrivateKey = <Server's Private Key Here>

# Demo Config
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
# Demo Config END

# Main Docker Config
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens18 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens18 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens18 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ens18 -j MASQUERADE
# Main Docker Config END
# JoinDigital Config START
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens2 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens2 -j MASQUERADE; iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o ens7 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -A POSTROUTING -o ens2 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens2 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ens2 -j MASQUERADE; iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o ens7 -j MASQUERADE; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o ens2 -j MASQUERADE
# JoinDigital Config END

# SaveConfig = true #If you want your configs to be saved on restart

[Peer]
PublicKey = <Client's Public Key Here>
AllowedIPs = 10.xx.xx.2/32

[Peer]
PublicKey = <Client's Public Key Heree>
AllowedIPs = 10.xx.xx.3/32
Server Config Explanation for [Interface] section

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. 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/1/xx
For this field 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 /2424. orA /20valid etc.example would be “10.10.10.1/24”.

SaveConfigListenPort = true51820

/ false.

This setting when set to "true" will automatically saveis the currentUDP liveport configthe wireguard instance listens on. If you chose a different port replace 51820 with your specified UDP port.

PrivateKey = <Server’s Private Key Here>

Enter in standardthe formatkey intoyou yourgenerated. wg0.confThere filewere whenevertwo wireguardkeys servicegenerated, isthe turnedserver’s off.public Becauseand itthe isserver’s private key pair. Enter in standard format any comments you made to the wg0.confserver’s fileprivate whilekey. beFor gone.example

Set
PrivateKey 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.

KDNXaJyH+/zlad6d4QjWw7ia6NFVbx0aZUK/MzNfemg=
Server Config Explanation for [Peer] section

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

Adding Clients to Server

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

This method requires SaveConfig = true in your config.

Adding a peer (Changes not saved yet)

sudo wg set wg0 peer <Client Public Key> allowed-ips 10.X.X.X/3

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

sudo wg

Finally do a

sudo systemctl restart wg-quick@wg0.service
Method #2: While Wireguard Is Live (wg-quick save wg0)

Also requires SaveConfig = true in your config.

sudo wg set wg0 peer <Client Public Key> allowed-ips 10.X.X.X/32
sudo wg show
sudo wg-quick save wg0
route 10.X.X.X/32 wg0

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.

Method #3: While Wireguard Is Off
sudo wg-quick down wg0
# Edit your /etc/wireguard/wg0.conf file and add the peers you need there
sudo wg-quick up wg0

Generating Client Configurations For Users

Example configuration. Please read the gotchas for each OS.

[Interface]
PrivateKey = < Client Private Key Here >
Address = 10.X.X.0/24
DNS = 8.8.8.8

[Peer]
PublicKey = < Server Public Key Here >
AllowedIPs = 0.0.0.0/0, ::/0
Endpoint = ServerPublicIPAdress:51820
PersistentKeepalive = 25

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"

iptables -I FORWARD -i wg0 -o wg0 -j REJECT

Command References

sysctl net.ipv4.ip_forward             ### Verifies if IP Forward is working
sudo systemctl enable wg-quick@wg0     ### Makes Wireguard auto-start on boot
sudo systemctl restart wg-quick@wg0    ### Restart Wireguard in systemctl
sudo systemctl start wg-quick@wg0      ### Start Wireguard in systemctl
sudo systemctl stop wg-quick@wg0       ### Stop Wireguard in systemctl
sudo wg show                           ### #Check if VPN tunnel is running
wg-quick up wg0     # Depreciated: Turn on Wireguard Interface
wg-quick down wg0   # Depreciated: Turn off Wireguard Interface


#command to remove client (peer)
wg set wg0 peer peer_pubkey remove

#Don't know if this command is needed after wg-quick save or removal of client
wg addconf wgnet0 <(wg-quick strip wgnet0)

### Generating Key Pairs ###
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
# Key pairs are saved in same path you typed this command in
### End Generating Key Pairs ###