Configure Wireguard VPN
Wireguard is fast, simple (around 4k lines of code) and secure VPN. From my perspective as a user, a configuration is as simple as in SSH.
Installation
Add repository and install package (for other systems go to official docs)
add-apt-repository ppa:wireguard/wireguardapt-get updateapt-get install -y wireguardEnsure that you enabled forwarding in sysctl.
echo "net.ipv4.ip_forward = 1" > /etc/sysctl.d/wg.confecho "net.ipv6.conf.all.forwarding = 1" >> /etc/sysctl.d/wg.confsysctl --systemConfiguration
-
Create server and client keys
Terminal window wg genkey | tee server.private.key | wg pubkey > server.public.keywg genkey | tee client.private.key | wg pubkey > client.public.key -
touch /etc/wireguard/wg0.confand put config for VPN interface:[Interface]Address=<server VPN ip>/24PrivateKey = <server private key>ListenPort = 51820PostUp = iptables -t nat -A POSTROUTING -o <server NAT interface> -j MASQUERADE; ip6tables -t nat -A POSTROUTING -o <server NAT interface> -j MASQUERADEPostDown = iptables -t nat -D POSTROUTING -o <server NAT interface> -j MASQUERADE; ip6tables -t nat -D POSTROUTING -o <server NAT interface> -j MASQUERADE[Peer]PublicKey = <client public key>AllowedIPs = <client VPN ip>/32Example:
[Interface]Address=192.168.101.1/24PrivateKey = mHjrLYUTKbrGqJViVOHfQX9dN0Sn49gJNoof68nbJHA=ListenPort = 51820PostUp = iptables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADE; ip6tables -t nat -A POSTROUTING -o enp0s3 -j MASQUERADEPostDown = iptables -t nat -D POSTROUTING -o enp0s3 -j MASQUERADE; ip6tables -t nat -D POSTROUTING -o enp0s3 -j MASQUERADE[Peer]PublicKey = XKT1Ctj5b+gjXc1gMtOdxNEpc9UUM2TsXaFdAyABd3w=AllowedIPs = 192.168.101.2/32 -
Run VPN server with
wg-quick up -
Create config for client
[Interface]Address = <client VPN ip>/24PrivateKey = <Client Private Key>ListenPort = 21841DNS = <dns ip 1>,<dns ip 2>[Peer]PublicKey = <server public key>Endpoint = <server bridge interface address>:51820AllowedIPs = 0.0.0.0/0Example:
[Interface]Address = 192.168.101.2/32PrivateKey = 0AQI65ehzszpXf9f2FWEABX90PX+gv5DJH3/mkZ/eW8=ListenPort = 21841DNS = 1.1.1.1,1.1.0.0[Peer]PublicKey = ccDLW5zKussL3ejxMqWpx1uZMfN09bkGAirCWXZWp0s=Endpoint = 192.168.1.5:51820AllowedIPs = 0.0.0.0/0 -
Install client software https://www.wireguard.com/install/ and paste client config