As you must know, public VPN services can help to protect your data, but they often come with limitations and trust concerns. A better solution is to build a private VPN inside a VPS using WireGuard, which is a modern, fast, and lightweight VPN protocol.
In this guide, we will show you how to set up a secure and efficient private VPN inside your VPS using WireGuard so that you can take full control of your online privacy and network traffic.
If you don’t already have a server, you can check PerLod Hosting, which offers fast, affordable, and secure hosting solutions for VPN setups.
Requirements To Build a Private VPN Inside a VPS Using WireGuard
Before you start to set up your private VPN, you must make sure to meet the following requirements:
A fresh VPS with a public IPv4 address and optional IPv6.
Root or sudo access.
Open port 51820/UDP in your provider firewall.
Operating system: Ubuntu 22.04/24.04 or Debian 12, and AlmaLinux 9 notes will be included in the guide.
Define the Private VPN Network
It is recommended to plan the IP addresses, ports, and key variables that will allow your clients to connect securely to your server. Here is a set of defaults, which you can adjust to fit your existing home network and prevent subnet conflicts:
VPN interface: wg0
WireGuard UDP port: 51820
VPN network (IPv4): 10.8.0.0/24
Server’s VPN IP: 10.8.0.1
DNS for clients: 1.1.1.1, 1.0.0.1
Also, record the values below carefully, as they are the foundation of your entire setup:
Server public IP: YOUR_SERVER_PUBLIC_IP
Server public DNS or hostname: vpn.example.com
System Preparation: Install WireGuard and Enable IP Forwarding
Run the system update and install the required tools and WireGuard VPN network on your server with the following commands, depending on your OS.
Then, enable IP forwarding for IPv4 and optional IPv6, which is the core function that allows your server to pass traffic from your VPN clients out to the internet:
Create WireGuard Server Configuration and Enable NAT
In this step, you must create a WireGuard server config file, which defines the server's VPN interface and establishes the firewall rules that enable network address translation (NAT) and packet forwarding. This is what allows your VPN clients to access the internet through the server.
We use iptables commands that execute when the interface starts (PostUp) and stops (PostDown) to manage this automatically.
Create the directory and set the correct permissions for it:
Then, create the Wireguard server config file and enable NAT with:
Bash
SERVER_PRIV_KEY=$(cat/etc/wireguard/server_private.key)SERVER_PUB_IP="YOUR_SERVER_PUBLIC_IP"# or vpn.example.comWG_PORT=51820WG_NET_IPV4="10.8.0.0/24"WG_SVR_IPV4="10.8.0.1/24"sudotee/etc/wireguard/wg0.conf>/dev/null<<EOF[Interface]Address=${WG_SVR_IPV4}ListenPort=${WG_PORT}PrivateKey=${SERVER_PRIV_KEY}# Optional: set a stable interface MTU (usually auto is fine; 1420 helps on some VPS)# MTU = 1420# NAT and forwarding (IPv4)PostUp=iptables-tnat-APOSTROUTING-s${WG_NET_IPV4}-o $(iprouteshowdefault|awk'/default/ {print $5; exit}')-jMASQUERADEPostDown=iptables-tnat-DPOSTROUTING-s${WG_NET_IPV4}-o $(iprouteshowdefault|awk'/default/ {print $5; exit}')-jMASQUERADE# Allow forwarding between wg and the outsidePostUp=iptables-AFORWARD-iwg0-jACCEPTPostUp=iptables-AFORWARD-owg0-jACCEPTPostDown=iptables-DFORWARD-iwg0-jACCEPTPostDown=iptables-DFORWARD-owg0-jACCEPTEOF
Configure Firewall Rules for WireGuard
Your server is now configured; you must now open the UDP port 51820 to allow incoming VPN connections from your clients.
On Ubuntu and Debian, if you use UFW, run the commands below:
Bash
sudoufwallow51820/udpsudoufwallowOpenSSHsudoufwenable# if it wasn't enabled yetsudoufwstatus
At this point, you must enable the WireGuard service to run automatically at boot and then verify that the wg0 interface is up and running correctly with the following commands:
You must see wg0 with IP 10.8.0.1/24 and no clients yet.
Add the First Client Peer To WireGuard
Your WireGuard server is up and running, but it doesn't have clients. You can generate a unique key pair for a device, for example, "user-phone", and add it as a trusted peer to the server. You can repeat these steps for each client you want to add.
First, generate the client keys with the following commands:
For the first client, we chose the 10.8.0.2/32 VPN IP and created the config as shown below:
Bash
CLIENT_NAME="user-phone"CLIENT_PRIV_KEY=$(cat~/user-phone_private.key)CLIENT_PUB_KEY=$(cat~/user-phone_public.key)SERVER_PUB_KEY=$(cat/etc/wireguard/server_public.key)SERVER_ENDPOINT="YOUR_SERVER_PUBLIC_IP:51820"# or vpn.example.com:51820CLIENT_IPv4="10.8.0.2/32"tee~/${CLIENT_NAME}.conf>/dev/null<<EOF[Interface]PrivateKey=${CLIENT_PRIV_KEY}Address=${CLIENT_IPv4}DNS=1.1.1.1,1.0.0.1# Optional: MTU = 1420[Peer]PublicKey=${SERVER_PUB_KEY}AllowedIPs=0.0.0.0/0Endpoint=${SERVER_ENDPOINT}PersistentKeepalive=25EOF
Note: AllowedIPs = 0.0.0.0/0 means all traffic goes through the VPN. For split tunnel to only reach your VPS network, set AllowedIPs = 10.8.0.0/24.
Once you are done, you can add the client to the server and assign its VPN IP with the following commands:
The final step is to install the generated configuration on your client device. Install the official WireGuard application from your device's app store or the WireGuard website, then transfer the "user-phone.conf" file.
For phones and tablets, the easiest method is to generate a QR code to scan directly.
Bash
qrencode-tansiutf8<~/${CLIENT_NAME}.conf
For desktop operating systems, use the import function within the WireGuard application to load the file.
Test and Verify WireGuard VPN Connection
Now you must activate the tunnel in your WireGuard client app and use the following commands to check and confirm everything is working correctly.
Check the external IP with the following command:
Bash
curlhttps://ifconfig.me
It should show your VPS public IP if you’re using full tunnel.
Then, ping your server's VPN IP with:
Bash
ping10.8.0.1
On the server, you must confirm the handshakes with the command below:
Bash
wgshow
You should see the client with the latest handshake and some RX/TX bytes.
Optional: Use Private Unbound DNS Resolver on VPN Server
For maximum privacy, you can replace the public DNS servers (1.1.1.1) with your own recursive resolver running directly on the VPN server. This prevents your DNS queries from being visible to third-party DNS providers and can improve response times through caching.
To do this, you can use Unbound, which is a secure and lightweight DNS resolver that will listen for requests from your VPN clients.
Install Unbound with the command below:
Bash
sudoaptinstallunbound-y
Here is a minimal recursive config you can use:
Bash
sudotee/etc/unbound/unbound.conf.d/wg-recursive.conf>/dev/null<<'EOF'server:verbosity:0interface:127.0.0.1port:5353do-ip4:yesdo-ip6:nodo-udp:yesdo-tcp:yeshide-identity:yeshide-version:yesharden-referral-path:yesprefetch:yesaccess-control:10.8.0.0/24allowaccess-control:127.0.0.0/8allow# Root hints auto-managed on modern distros, otherwise add a root.hints file.EOF
Then, enable the Unbound service with:
Bash
sudosystemctlenable--nowunbound
Next, you must set client DNS = 10.8.0.1:5353 in their configs.
Stop and Uninstall WireGuard Private VPN Service
If you plan to stop and remove the WireGuard VPN service, you can use the commands below.
To stop temporarily, run:
Bash
sudosystemctlstopwg-quick@wg0
Disable auto start with:
Bash
sudosystemctldisablewg-quick@wg0
Remove the package with:
Bash
sudoaptremove--purgewireguard-y
Conclusion
Build a private VPN inside a VPS gives you full control over your online privacy and network security. By using WireGuard’s modern architecture, you can create a high-speed, low-latency VPN with minimal setup and maximum reliability.
Remember to explore our flexible VPS hosting solutions, which provide secure, high-performance environments perfect for VPN deployment, web applications, or personal projects.
We hope you enjoy this guide. Subscribe to our X and Facebook channels to stay up-to-date with the latest articles and network security updates.
Building your own VPN gives you full control over your data, encryption keys, and logging policies. Unlike commercial VPNs, your traffic isn’t routed through a third party, which makes it ideal for privacy, security, and learning.
You can change WireGuard’s port to another UDP port or use TCP tunneling through tools like udp2raw or WireGuard over WebSocket, which need advanced setup.
Remove the peer from the server config and restart the service with:
sudo wg set wg0 peer remove
sudo systemctl restart wg-quick@wg0
We use cookies to run PerLod and to understand how the site is used. You can accept everything, or open Manage to turn individual categories off. Cookie policy.