Setup wireguard VPN
It is really easy to configure wireguard VPN, compare to the old IPsec and OpenVPN methods.
In wireguard method, there is no server/client, they are all peers. So a “server” is a peer, a client is also a peer. If we want to let multi clients to connect to a server, they are actually multi peers connecting to one peer (server).
1, Server configuration
1) Installation
On ubuntu < 19.10, you need to do:
1 | $ sudo add-apt-repository ppa:wireguard/wireguard |
For ubuntu >=19.10, you can just run:
1 | $ apt install wireguard |
Also to support the DNS field in the configuration file (showing below), do:
1 | $ apt install openresolv |
2) Generate private anad public key
Once done, generate private-key and public-key pair for your server:
1 | $ umask 077 |
3) add configuration file
Now go to /etc/wireguard/ folder, add a config file: wg0.conf:
1 | [Interface] |
In the PostUp and PostDown step, replace eth0 if your network is running on a different interface.
4) Setup firewall with ufw
Here we are using 51820 as the port. If you are using ufw, remember to unblock this port (wireguard is running on UDP protocol)
1 | $ ufw allow 51820/udp |
5) Enable IP forwarding
Also make sure your server enabled the ip_forwarding. Open /etc/sysctl.conf, add or uncomment line:
1 | net.ipv4.ip_forward=1 |
If you are using IPv6 with WireGuard, use:
1 | net.ipv6.conf.all.forwarding=1 |
Then make sure to reload this settings with sysctl -p
6) Enable wireguard auto start on system boot with systemd
1 | $ systemctl enable wg-quick@wg0.service |
2, Client setup
Client is another peer. Install client here: https://www.wireguard.com/install/
Wireguard uses public key to identify a peer, so the first setup is still generate private/public key pair:
- MacOS user: open the Wireguard app, click “Add Tunnel” > “Empty Tunnel”, a window will pop-up to show generated private/public key, save those keys somewhere, then click “Discard” (Don’t click save)
- Windows user: open the Wireguard app, click “Add Tunnel” > “Empty Tunnel”, a window will pop-up to show generated private/public key, save those keys somewhere, then click “Cancel” (Don’t click save)
- Ubuntu user: use the same way which used by server in above step. i.e. the “wg genkey” etc. method.
Once you have the private key and public key, create a config file anywhere (If you are using Ubuntu, put that config in /etc/wireguard/wgclient.conf), the content is like this:
1 | [Interface] |
Save this wgclient.conf file, for MacOS and Windows user, click “Add Tunnel” > “Add Tunnel from existing config file”, then select this config file. After this, configuration on the client side are all set.
3, Add client to server
Now we have client’s private key and public key, it is time to add the client public key to server. Back to our /etc/wireguard/wg0.conf file in server side, then add:
1 | [Peer] |
Now activate the wireguard in server side:
1 | # activate |
4, Connect client to server
For MacOS and Windows user, since we have added the configuration file in step 2, all you need to do is click the “activate” button. For Ubuntu user, just run wg-quick up wgclient. Once done, your client peer should be now connected to the server peer.
On server side you can run wg show to check the connected peers status.
To disconnect it, MacOS and Windows user can just click the “deactivate” button. Ubuntu user can run wg-quick down wgclient.
Others
If you have some website running in your VPN server and you want to access it with wireguard VPN connected, the access IP now will be the private IP (e.g. 10.0.0.2, 10.0.0.3). If you website have an IP whitelist, you need to list those IPs as well. For example, in Nginx, you can add:
1 | allow 10.0.0.1; |
You can also add a range, e.g. if you want to allow 10.0.0.1 to 10.0.0.19, a “IP to CIDR” address tool can help: https://www.ipaddressguide.com/cidr#range. For example, for IP range 10.0.0.1 to 10.0.0.19, you can add allow like this:
1 | allow 10.0.0.1/32; |