Setup a Wireguard VPN Server on Ubuntu
This tutorial is going to show you how to set up your own WireGuard VPN server on Ubuntu.
WireGuard is made specifically for the Linux kernel. It runs inside the Linux kernel and allows you to create fast, modern, and secure VPN tunnel.
TL;DR
Prerequisites
This tutorial assumes that the VPN server and VPN client are both going to be running on Ubuntu 20.04 operating system.
Setting Up the WireGuard Server
Install Wireguard from the default Ubuntu repository:
You should got the two cli wg
and wg-quick
now.
Generate a key pair for your server:
Keep the server key pair in your clipboard or somewhere temporary place.
Now, edit the wireguard server configuration file.
Check the name of your public network interface first:
|
|
Then create wireguard configuration file:
|
|
Add this content:
|
|
- Address - A comma-separated list of v4 or v6 IP addresses for the wg0 interface. Use IPs from a range that is reserved for private networks (10.0.0.0/8, 172.16.0.0/12 or 192.168.0.0/16).
- SaveConfig - When set to true, the current state of the interface is saved to the configuration file when shutdown.
- PostUp - Command or script that is executed before bringing the interface up. In above configuration, we’re using iptables to enable masquerading. This allows traffic to leave the server, giving the VPN clients access to the Internet.
eth0
- Name of your public network interface
Secure the configuration file and private key file like so:
|
|
Launch the vpn network interface you just configured:
Configure Server Networking and Firewall
IP forwarding must be enabled for NAT to work. Do the following:
|
|
Uncomment or add this line:
|
|
Apply the change:
|
|
Optionally, if ufw is active on your server, do this:
|
|
Client Setup
Installation is all the same, and so the key pair generation is:
|
|
Create a Wireguard client configuration:
|
|
- Address - A comma-separated list of v4 or v6 IP addresses for the wg0 interface. Your client machine will have this private ip.
- PrivateKey - To see the contents of the file on the client machine run: sudo cat /etc/wireguard/privatekey
- AllowedIPs - A comma-separated list of v4 or v6 IP addresses from which incoming traffic for the peer is allowed and to which outgoing traffic for this peer is directed. We’re using 0.0.0.0/0 because we are routing the traffic and want the server peer to send packets with any source IP.
Add the Client Peer to the Server Peer
On the server, use this command:
|
|
You should be able to launch your client now:
If you want to remove a peer from your server, this will work:
|
|
That’s it.
Happy networking! 😎