What You Will Build
In about 20 minutes you will have your own WireGuard VPN server running on Ubuntu 24.04. Every device you connect — your laptop, your phone, a second server — will tunnel its traffic through your server, encrypted end to end, with a static private IP on your own virtual network.
This guide is for anyone with a basic Ubuntu VPS or home server and a little comfort on the command line. You do not need to understand cryptography to follow it: WireGuard hides almost all of the complexity, and the commands below are the same ones used in production setups.
Why WireGuard, and not OpenVPN or an off-the-shelf VPN app? WireGuard lives inside the Linux kernel, so it is fast — often faster than your raw connection on a small VPS. Its configuration is a handful of lines, not hundreds. And because you run the server yourself, no third party logs, throttles, or sells your browsing data.
Prerequisites
- An Ubuntu 24.04 server (a VPS with a public IP works best; a home server behind a router also works if you forward UDP port 51820).
- Root or sudo access, and SSH access to the server.
- A domain name is optional — a public IP address is enough.
- One client device to test with (a laptop or phone running WireGuard).
- About 20 minutes and a text editor such as nano.
Step 1: Update the Server and Install WireGuard
Start by updating the package list and installing WireGuard. On Ubuntu 24.04, WireGuard is in the default repositories, so no extra PPA is needed.
sudo apt update && sudo apt upgrade -y
sudo apt install -y wireguard
The first command refreshes the package index and applies pending updates. The second installs the WireGuard tools, including the wg command you will use to generate keys and inspect the tunnel.
Step 2: Enable IP Forwarding
Your server must forward traffic between the VPN interface and the internet. Without this, clients can connect but cannot reach anything beyond the server.
echo "net.ipv4.ip_forward=1" | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
The first line appends the forwarding setting to the system configuration so it survives a reboot. The second applies it immediately, without restarting.
Verify it took effect:
sysctl net.ipv4.ip_forward
You should see net.ipv4.ip_forward = 1. If you see 0, re-run the two commands above and check for typos.
Step 3: Generate the Server Keys
WireGuard identifies every peer by a public key, derived from a private key. Generate the server pair and store them in /etc/wireguard/, with strict permissions — the private key must never be shared or posted anywhere.
cd /etc/wireguard
umask 077
wg genkey | tee privatekey | wg pubkey > publickey
umask 077 makes every new file readable only by root. wg genkey creates the private key, tee saves it to a file, and wg pubkey derives the public key from it. Keep the private key secret; the public key is what clients and peers use to identify this server.
Display the keys so you can copy them later:
cat privatekey
cat publickey
Leave this terminal open, or copy both values into a safe note. You will paste them into configuration files in the next steps.
Step 4: Create the Server Configuration
Create /etc/wireguard/wg0.conf. This file defines the VPN interface, its private network address, the listening port, and the firewall rules that route client traffic to the internet.
sudo nano /etc/wireguard/wg0.conf
Paste the following, replacing YOUR_SERVER_PRIVATE_KEY with the private key from Step 3, and eth0 with your server's real network interface if it differs (check with ip a — on many VPS hosts it is eth0 or ens3):
[Interface]
PrivateKey = YOUR_SERVER_PRIVATE_KEY
Address = 10.6.0.1/24
ListenPort = 51820
SaveConfig = false
PostUp = iptables -A FORWARD -i %i -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i %i -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Here is what each part does. Address = 10.6.0.1/24 gives the server the first address on the private VPN network 10.6.0.0/24, leaving 10.6.0.2 onwards for clients. ListenPort = 51820 is WireGuard's default UDP port. The PostUp and PostDown rules turn on NAT (masquerading) when the interface starts and clean it up when it stops, so client traffic can reach the internet through the server's public IP.
Step 5: Open the Firewall
If you use UFW, Ubuntu's default firewall, allow the WireGuard port and make sure forwarding is permitted. If UFW is not active, you can still run these commands to set the rules for later.
sudo ufw allow 51820/udp
sudo ufw allow OpenSSH
sudo ufw enable
The first rule opens UDP port 51820, the only port WireGuard needs. The second keeps your SSH session reachable — never enable a firewall on a remote server without allowing SSH first, or you will lock yourself out. If your VPS provider has its own cloud firewall (such as AWS security groups or DigitalOcean cloud firewalls), open UDP 51820 there too.
If you use UFW, also allow routed traffic by editing /etc/default/ufw and setting DEFAULT_FORWARD_POLICY="ACCEPT", then reload UFW:
sudo sed -i 's/DEFAULT_FORWARD_POLICY="DROP"/DEFAULT_FORWARD_POLICY="ACCEPT"/' /etc/default/ufw
sudo ufw reload
This lets UFW forward packets between the VPN interface and the internet instead of dropping them.
Step 6: Start the VPN Interface
Start WireGuard with wg-quick, which reads wg0.conf and brings the interface up, and enable it to start automatically at boot.
sudo systemctl enable --now wg-quick@wg0
Check that the interface is up:
sudo wg show
ip a show wg0
You should see an interface named wg0 with address 10.6.0.1/24 and listening port 51820. There will be no peers yet — that is expected. Peers appear once you add a client in the next step.
Step 7: Generate a Client Key Pair
Every client needs its own key pair. Generate one on the server (or on the client itself — the method is identical), and keep the client's private key on the client only.
cd /etc/wireguard
wg genkey | tee client1-privatekey | wg pubkey > client1-publickey
cat client1-publickey
Copy the client's public key. You will add it to the server configuration so the server recognises this client, and copy the client's private key into the client configuration so the client can prove who it is.
Step 8: Add the Client as a Peer on the Server
Append a [Peer] section to /etc/wireguard/wg0.conf. The AllowedIPs value is the private VPN address this client will use — give each client a different one (10.6.0.2, 10.6.0.3, and so on).
sudo nano /etc/wireguard/wg0.conf
Add this at the end of the file, replacing YOUR_CLIENT_PUBLIC_KEY with the key from Step 7:
[Peer]
PublicKey = YOUR_CLIENT_PUBLIC_KEY
AllowedIPs = 10.6.0.2/32
Reload the interface so the new peer takes effect without dropping existing connections:
sudo wg syncconf wg0 <(sudo wg-quick strip wg0)
A simpler alternative, if no clients are connected yet, is to restart the service:
sudo systemctl restart wg-quick@wg0
Either way, verify the peer is listed:
sudo wg show
You should now see the peer with its public key and allowed IP 10.6.0.2/32, but no handshake yet — the handshake only happens when the client connects.
Step 9: Create the Client Configuration
On your laptop or phone, install WireGuard (available for Windows, macOS, Linux, Android, and iOS) and create a new tunnel with this configuration. Replace YOUR_CLIENT_PRIVATE_KEY with the private key from Step 7, YOUR_SERVER_PUBLIC_KEY with the server public key from Step 3, and YOUR_SERVER_IP with your server's public IP address or domain.
[Interface]
PrivateKey = YOUR_CLIENT_PRIVATE_KEY
Address = 10.6.0.2/32
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey = YOUR_SERVER_PUBLIC_KEY
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0 routes all of the client's traffic through the VPN — the standard choice for a personal privacy VPN. If you only want to reach your home network or server through the tunnel and keep normal browsing direct, set this to 10.6.0.0/24 instead (a split tunnel). PersistentKeepalive = 25 sends a packet every 25 seconds so the tunnel survives NAT routers and firewalls that drop idle UDP sessions. The DNS entries make the client resolve domains through public resolvers while connected, avoiding DNS leaks to your ISP.
Step 10: Connect and Verify It Worked
Activate the tunnel on the client, then run these checks.
First, on the server, confirm the handshake happened:
sudo wg show
Look for a latest handshake timestamp within the last minute or two. A missing or stale handshake means the client never reached the server — see the troubleshooting section below.
Second, from the client, check your public IP and confirm it matches your server:
curl ifconfig.me
The returned address should be your server's public IP, not your home or mobile IP. If it matches, all of your traffic is now flowing through your own VPN.
Third, test connectivity inside the tunnel:
ping -c 3 10.6.0.1
ping -c 3 8.8.8.8
The first ping reaches the server over the VPN; the second proves internet access through the tunnel. Both should succeed with low latency.
Adding More Clients
Repeat Steps 7 to 9 for each new device, giving each a unique private address (10.6.0.3, 10.6.0.4, and so on) and a unique key pair. Never reuse a key pair across devices: if one device is lost or compromised, you revoke just that peer by deleting its [Peer] section and restarting the interface, without touching your other devices.
Common Issues and Fixes
Handshake never completes
This is almost always a firewall or port problem. Confirm UDP 51820 is open in UFW (sudo ufw status) and in your VPS provider's cloud firewall, and that the client's Endpoint uses the server's correct public IP. A wrong public key on either side also blocks the handshake silently — WireGuard gives no error message for a key mismatch, so re-check both keys character by character.
Client connects but has no internet
Check IP forwarding (sysctl net.ipv4.ip_forward must be 1) and the NAT rules from PostUp. If you edited wg0.conf after starting the interface, restart it with sudo systemctl restart wg-quick@wg0 so the PostUp rules actually run. Also confirm UFW's forward policy is ACCEPT, as in Step 5.
Tunnel drops on mobile or behind strict NAT
Add or confirm PersistentKeepalive = 25 in the client's [Peer] section. Without it, home routers and mobile carriers silently close the idle UDP mapping, and the tunnel appears connected while nothing flows.
DNS leaks or sites see your real location
Make sure the client's AllowedIPs is 0.0.0.0/0 for a full tunnel, and that the DNS line is set. Then test at a DNS leak test site while connected: the reported DNS servers should belong to your chosen resolver, not your ISP.
Interface fails to start after a reboot
Run sudo systemctl status wg-quick@wg0 and sudo journalctl -u wg-quick@wg0 -n 50. The usual causes are a typo in wg0.conf or a network interface name that changed (for example, eth0 becoming ens3 after a VPS migration) — fix the interface name in the PostUp/PostDown lines and restart.
Conclusion
You now have a private, fast, self-hosted VPN: a WireGuard server on Ubuntu 24.04, a working client, firewall rules that route traffic correctly, and the knowledge to add and revoke devices. For the cost of a small VPS you get encryption you control, with no subscription and no third-party logging.
If you would rather have a team set up and harden your servers for you, XpertBees helps businesses with server setup, VPN configuration, and ongoing Linux administration — reach out through xpertbees.in and we will take it from here.



