May 13, 2021
This will be a bit longer tutorial than usual one-liners, but it’ll be as simple as it can be. Basic knowledge of linux is required.
apt add wireguard-tools
You can repeat key generating process for clients as much users as you need. Store keys somewhere safe. These files don’t have to be on the machine itself, we just need the content of those files.
# Generate server keyswg genkey | tee privatekey-server | wg pubkey > publickey-server# Generate keys for 1 clientwg genkey | tee privatekey-user-1 | wg pubkey > publickey-user-1
Create config file at: /etc/wireguard/wg0.conf
, paste this and update values that you want:
[Interface]Address = 192.168.50.1 # New (non-existing) local LAN subnet or domain IPListenPort = 51820 # Connection UDP portPrivateKey = <privatekey-server> # Private SERVER key string (not file path)PostUp = /home/pi/postup.sh # Script that will run after the VPN server is startedPostDown = /home/pi/postdown.sh # Script that will run after the VPN server is shut down[Peer]PublicKey = <publickey-client-1> # Public CLIENT key string (not file path)AllowedIPs = 192.168.50.2/32 # Client IP (defined in CLIENT config). Must end with '/32'.
If you need more clients, then copy and paste the entire [peer]
section and update values accordingly.
Create two new files in /home/pi/
named postup.sh
and postdown.sh
, make them executable by typing in terminal chmod +x postup.sh postdown.sh
. Also, change interface name in both files if you’re using eth0
instead predefined wlan0
.
postup.sh
#!/usr/bin/env bashset -ex# Traffic forwardingiptables -A FORWARD -i %i -j ACCEPTiptables -A FORWARD -o %i -j ACCEPTiptables -t nat -A POSTROUTING -o wlan0 -j MASQUERADEsysctl -w net.ipv4.ip_forward=1
postdown.sh
#!/usr/bin/env bashset -ex# Traffic forwardingiptables -D FORWARD -i %i -j ACCEPTiptables -D FORWARD -o %i -j ACCEPTiptables -t nat -D POSTROUTING -o wlan0 -j MASQUERADEsysctl -w net.ipv4.ip_forward=0
/etc/init.d
directorychmod +x wireguard
rc-update add wireguard default
#!/sbin/openrc-rundepend() {need localmountneed net}start() {ebegin "Starting wireguard"/usr/bin/wg-quick up wg0eend $?}stop() {ebegin "Stopping wireguard"/usr/bin/wg-quick down wg0eend $?}
Create new config as follows:
[Interface]Address = 192.168.50.2/32 # Unique client IP used in server config. Must end with `/32`.DNS = 94.140.14.15 # DNS (current is set to AdGuard, can be changed to 1.1.1.1 or 8.8.8.8)ListenPort = 51820 # Port to which we're connecting defined in server configPrivateKey = <privatekey-user-1> # Private CLIENT key string (not file path)[Peer]AllowedIPs = 0.0.0.0/0 # All trafic goes through VPNEndpoint = <SERVER-IP>:51820 # DDNS url or IP or Domain name with given port defined in server configPersistentKeepalive = 25 # Send keepalive every X secondsPublicKey = <publickey-server> # Public SERVER key string (not file path)
NOTE: Information about client’s [peer] AllowedIPs
:
You can combine these two last subnets as 192.168.50.0/24, 192.168.1.0/24
. This way you’ll have access to your server and your local LAN network at the same time while all other traffic is handled by your current network.
That’s it. You now have a functional VPN server. The benefit is that all your traffic (Through VPN) will be checked against AdGuard’s DNS server so all the ads will be removed from the responses, that way you get ad-free web experience.
Example:
You have entered both subnets as
AllowedIPs
value. You’re gonna have access to your google home, tv, rpi, NAS, Smart home… That will all go through the VPN and you can mess with the people at home by turning on/of light etc. But now you visit google.com, reddit.com… and that traffic will not go through the VPN… unless you entered0.0.0.0/0
, then everything will go through the VPN and ads will be stripped from the response._
Written by Milan Miljkovic — a tech enthusiast and design system practitioner.