Setting up Wireguard VPN with a custom IP range (/24)
Wireguard is a modern VPN protocol that aims to provide better security, faster performance, and overall ease-of-use compared to older VPN protocols. In this article, we will cover the steps to set up Wireguard with a custom IP range of /24.
Prerequisites
Before we begin, make sure you have the following:
- A server or VPS with a fresh installation of a Linux distribution (e.g. Ubuntu 20.04).
- Root access to the server.
- Basic knowledge of Linux command line.
Installing Wireguard
Wireguard is not included in the default repositories of most Linux distributions. We will need to add the Wireguard repository to our server and install it.
For Ubuntu 20.04, run the following commands:
sudo add-apt-repository ppa:wireguard/wireguard
sudo apt update
sudo apt install wireguard
Generating Keys
Wireguard uses public key cryptography to secure the VPN connection. We need to generate a private key and a public key for both the server and each client.
On the server, run the following command to generate the private key:
sudo apt install wireguard-tools
wg genkey | tee server-private-key.txt | wg pubkey > server-public-key.txt
This will generate two files: server-private-key.txt and server-public-key.txt.
For each client, repeat the following command to generate the private and public keys:
wg genkey | tee client-private-key.txt | wg pubkey > client-public-key.txt
Configuring Wireguard
Now we need to configure Wireguard on the server and each client.
Server Configuration
Create a new Wireguard configuration file on the server:
sudo nano /etc/wireguard/wg0.conf
Add the following content to the file:
[Interface]
Address = 10.0.0.1/24
PrivateKey =
ListenPort = 51820
Replace <contents of server-private-key.txt> with the actual contents of the server-private-key.txt file.
Client Configuration
Create a new Wireguard configuration file for each client:
sudo nano /etc/wireguard/wg-client1.conf
Add the following content to the file:
[Interface]
Address = 10.0.0.2/32
PrivateKey =
[Peer]
PublicKey =
Endpoint = :51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Replace <contents of client-private-key.txt> and <contents of server-public-key.txt> with the actual contents of the corresponding files.
Replace <server-ip> with the IP address of the server.
Starting Wireguard
Start the Wireguard service on the server:
sudo systemctl start wg-quick@wg0
Enable the Wireguard service to start automatically on boot:
sudo systemctl enable wg-quick@wg0
For each client, start the Wireguard service:
sudo systemctl start wg-quick@wg-client1
Testing the VPN Connection
To test the VPN connection, try pinging the server from a client:
ping 10.0.0.1
You should see replies from the server.
- We have covered the steps to set up Wireguard VPN with a custom IP range of /24.
- We have learned how to install Wireguard, generate keys, and configure Wireguard on the server and each client.
- We have tested the VPN connection by pinging the server from a client.