sudo apt install wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub # keep *.key private
wg genkey | tee client.key | wg pubkey > client.pub
# server: /etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <server private key>
[Peer]
PublicKey = <client public key>
AllowedIPs = 10.8.0.2/32
# client: wg0.conf
[Interface]
Address = 10.8.0.2/24
PrivateKey = <client private key>
[Peer]
PublicKey = <server public key>
Endpoint = vpn.example.org:51820
AllowedIPs = 10.8.0.0/24 # only the VPN network; 0.0.0.0/0 sends all traffic through the server
PersistentKeepalive = 25
sudo wg-quick up wg0 # and: sudo systemctl enable --now wg-quick@wg0
sudo wg show
Open the UDP port in the firewall (both the cloud security rules and the VM). Never share private keys. To reach other networks through the server enable forwarding: net.ipv4.ip_forward=1.