WireGuard Essentials


WireGuard Essentials

Install Wireguard Ubuntu 20.04 +

Works on Ubuntu 22.04

sudo apt-get update && sudo apt-get install wireguard -y

Install WireGuard on Ubuntu Pre 19.10

sudo add-apt-repository ppa:wireguard/wireguard
sudo apt-get update
sudo apt-get install wireguard

Generate Server Keys

umask 077
wg genkey | tee privatekey | wg pubkey > publickey

Setup Firewall

sudo ufw allow 22/tcp
sudo ufw allow 51820/udp
sudo ufw enable
sudo ufw status

For more information regarding UFW Firewall

Enable Packet Forwarding

echo "net.ipv4.ip_forward = 1 net.ipv6.conf.all.forwarding = 1" > /etc/sysctl.d/wg.conf
sysctl --system

Additional info on IP Forwarding

Install Open Resolv

#Otherwies a client will fail

sudo apt install openresolv

Setup Server Interface

sudo nano /etc/wireguard/wg0.conf

[Interface]
Address = 10.100.100.1/24
SaveConfig = true
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -A FORWARD -o %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -D FORWARD -o %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
ListenPort = 51820
PrivateKey = ###


Online Server Interface

sudo wg-quick up wg0
#Query it it is running
sudo wg
ip addr show wg0

Set the Interface to Start Automatically

sudo wg-quick down wg0
systemctl start wg-quick@wg0
systemctl enable wg-quick@wg0

Generating Client Keys

sudo wg genkey | tee Client1-PrivateKey | wg pubkey > Client1-PublicKey

Add Client To Server As Peer

#On the server

sudo nano /etc/wireguard/wg0.conf

add the following under the line referencing the server private key


[Peer]
PublicKey = ###
AllowedIPs = 10.100.100.2/24


Install QR Generator

sudo apt install qrencode

Generate QR Code (Optional)

qrencode -t ansiutf8 < Client1.conf

Make WireGuard Auto Start Automatically

sudo systemctl enable wg-quick@wg0

Problems and Fixes

This Problem occured on Ubuntu 18.04:

Note this method did NOT fix a Pi Zero W

sudo wg-quick up wg0
[#] ip link add wg0 type wireguard
Error: Unknown device type.
Unable to access interface: Protocol not supported
[#] ip link delete dev wg0
Cannot find device "wg0"

Fix:

sudo apt purge wireguard-dkms && sudo reboot

sudo apt install wireguard-dkms

Links

  • -https://www.wireguard.com/
  • -https://www.wireguard.com/repositories/
  • https://git.zx2c4.com/

,