Here are the general steps to set up a WireGuard VPN server on Ubuntu 20.04:
- Install the WireGuard package by running the following command:
sudo apt install wireguard
- Create a new WireGuard configuration file for the server. You can do this by running the following command:
sudo nano /etc/wireguard/wg0.conf
This will open the Nano text editor, where you can add the server’s configuration.
- Inside the configuration file, add the following information:
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
SaveConfig = true
[Peer]
PublicKey = <client public key>
AllowedIPs = 10.0.0.2/32
- In the [Interface] section, you can specify the IP address and subnet mask for the server, as well as the listening port.
- In the [Peer] section, you can specify the public key of the client, as well as the IP address range that the client will be assigned.
- Generate a private and public key pair for the server by running the following command:
sudo wg genkey | tee privatekey | wg pubkey > publickey
- Add the server’s public key to the configuration file by running the following command:
sudo nano /etc/wireguard/wg0.conf
- Inside the configuration file, add the following information:
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <server private key>
SaveConfig = true
- Enable and start the WireGuard service by running the following command:
sudo systemctl enable --now wg-quick@wg0
- Configure the firewall to allow incoming WireGuard traffic by running the following command:
sudo ufw allow 51820/udp
- You can also configure the firewall to forward the traffic to the wireguard interface by running:
sudo nano /etc/ufw/before.rules
- Add these line at the top of the file:
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -o ens3 -j MASQUERADE
COMMIT
- Restart the firewall service
sudo ufw disable
sudo ufw enable
Once you’ve completed these steps, you should have a working WireGuard VPN server on Ubuntu 20.04. You can now generate a client configuration and connect to the server using the WireGuard client on a different device.
Please note, this guide is just a basic setup, and there are other things to consider like security, firewall, and routing. Consider consulting the official documentation for more detailed information.