A WireGuard VPN server lets you route your traffic through your own VPS, reach your servers over an encrypted private network, and stop trusting coffee-shop Wi-Fi or third-party VPN providers with your data. WireGuard is built into the Linux kernel, is dramatically simpler to configure than OpenVPN or IPsec, and reconnects almost instantly. In this guide you will set up a WireGuard VPN server on Ubuntu, generate client configurations, and connect from a phone or laptop.
Why Choose a WireGuard VPN Server? – How to Set Up a WireGuard
- Speed: kernel-space implementation with modern cryptography (Curve25519, ChaCha20) outperforms legacy VPN protocols
- Simplicity: a server config is about 15 lines; peers are identified by public keys, like SSH
- Roaming: clients keep the tunnel when switching from Wi-Fi to mobile data
- Small attack surface: the daemon stays silent to unauthenticated packets
Prerequisites – How to Set Up a WireGuard
- An Ubuntu 22.04 or 24.04 VPS with a public IP and sudo access
- UDP port 51820 reachable — allow it in your firewall; if you have not set one up, follow our guide to install and configure UFW on Ubuntu 22.04
- A client device with the WireGuard app (available for Windows, macOS, Linux, Android and iOS)
Step 1: Install WireGuard
sudo apt update
sudo apt install -y wireguard qrencode
qrencode is optional but handy: it prints QR codes in the terminal so phones can import configurations by scanning them.
Step 2: Generate Server Keys
WireGuard peers authenticate with key pairs. Generate the server’s keys and lock down their permissions:
wg genkey | sudo tee /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub
sudo chmod 600 /etc/wireguard/server.key
cat /etc/wireguard/server.pub
Note the public key — clients will need it. The private key never leaves the server.

Step 3: Configure the WireGuard VPN Server Interface
Create /etc/wireguard/wg0.conf. We use the private subnet 10.8.0.0/24, with the server as 10.8.0.1:
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <SERVER_PRIVATE_KEY>
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
PostDown = iptables -D FORWARD -i wg0 -j ACCEPT; iptables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
Replace eth0 with your public interface name if it differs (check with ip -brief addr). Then enable IP forwarding so client traffic can leave the server:
echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf
Open the port and start the tunnel:
sudo ufw allow 51820/udp
sudo systemctl enable --now wg-quick@wg0
sudo wg show
Step 4: Create a Client Configuration
Generate a key pair for the client and add the client as a peer on the server:
wg genkey | tee client1.key | wg pubkey > client1.pub
sudo nano /etc/wireguard/wg0.conf
Append a peer block to the server config:
[Peer]
PublicKey = <CLIENT1_PUBLIC_KEY>
AllowedIPs = 10.8.0.2/32
Now create the client file client1.conf:
[Interface]
PrivateKey = <CLIENT1_PRIVATE_KEY>
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
AllowedIPs = 0.0.0.0/0 routes all client traffic through the tunnel. Reload the server and hand the config to your phone as a QR code:
sudo systemctl restart wg-quick@wg0
qrencode -t ansiutf8 < client1.conf
The WireGuard quickstart on the official site explains every directive used here.
Step 5: Verify the Connection
Connect the client, then on the server run:
sudo wg show
curl ifconfig.me # run on the client: should print the server IP
A recent value in the latest handshake line confirms the tunnel is up and exchanging keys.
Troubleshooting
Handshake never completes: UDP 51820 is blocked somewhere — check UFW, any cloud firewall, and that the client used the correct server public key and endpoint IP.
Connected but no internet: almost always forwarding or NAT. Confirm sysctl net.ipv4.ip_forward returns 1 and that the PostUp masquerade rule references the correct outbound interface.
Handshake works, traffic stops after a while: NAT timeouts on the client network — PersistentKeepalive = 25 in the client config fixes it.
Once your tunnel is reliable, you can also restrict administrative access so services like SSH into your Linux server are only reachable from inside the VPN.
Adding and Revoking More Clients
Each additional device or teammate gets their own key pair and the next free address in the subnet: 10.8.0.3, 10.8.0.4, and so on. Never share one client configuration between two devices — WireGuard identifies peers by key, and simultaneous connections from the same key will fight over the tunnel. Add a peer without restarting the interface by loading it live: sudo wg set wg0 peer <PUBLIC_KEY> allowed-ips 10.8.0.3/32, then also append it to wg0.conf so it survives reboots. Revoking access is the mirror image: remove the peer from the running interface with sudo wg set wg0 peer <PUBLIC_KEY> remove and delete its block from the configuration file. Keep a simple table of which IP belongs to whom; when a laptop is lost, you will be glad you can revoke exactly one line.
Conclusion
Your WireGuard VPN server is ready: fast, encrypted and under your control. Add one peer per device, back up /etc/wireguard securely, and revoke a peer instantly by removing its block and restarting the interface. For most sysadmins, WireGuard has quietly become the default way to reach infrastructure safely from anywhere.