Setting Up Wireguard VPN on Ubuntu Cloud Servers
What You’ll Need
- Hetzner VPS or Contabo VPS running Ubuntu 22.04 or 24.04 LTS
- DigitalOcean droplet as an alternative hosting platform
- Namecheap if you plan to assign a custom DNS hostname to your VPN gateway
- Basic familiarity with Linux command-line administration, SSH keys, and UFW firewall management
Table of Contents
- Preparing the Server and Enabing IP Forwarding
- Installing WireGuard and Generating Key Pairs
- Configuring the WireGuard Interface and Firewall Rules
- Creating Client Configurations and Establishing Tunnels
- Routing Internal Microservices Across the WireGuard Mesh
- Getting Started
Preparing the Server and Enabing IP Forwarding
WireGuard operates directly inside the Linux kernel, offering lower latency, better battery performance on mobile devices, and significantly higher throughput compared to traditional VPN protocols like OpenVPN or IPSec. Because it operates at the network layer as a virtual interface, your server must be configured to route IP packets between the virtual VPN subnet and the public network interface.
First, log into your cloud instance. If you need reliable virtual infrastructure, deploying an instance on Hetzner VPS or DigitalOcean provides high networking throughput and unthrottled bandwidth ideal for VPN termination nodes.
Update your package index and upgrade existing software to ensure kernel modules are synchronized:
sudo apt update && sudo apt upgrade -y
Next, enable IPv4 packet forwarding in the kernel. By default, Linux disables packet routing between network interfaces for security reasons. To allow your Ubuntu server to act as a gateway router for connected VPN peers, you must modify the kernel parameters stored in /etc/sysctl.d/99-wireguard.conf.
Create and open this file using your editor of choice:
sudo nano /etc/sysctl.d/99-wireguard.conf
Add the following kernel configuration lines to enable packet forwarding for IPv4 and IPv6 traffic:
net.ipv4.ip_forward=1
net.ipv6.conf.all.forwarding=1
Save the file and apply the new configuration parameters immediately without rebooting the system:
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf
Verify that IP forwarding is active by querying the kernel state directly:
cat /proc/sys/net/ipv4/ip_forward
If the command returns 1, your kernel is ready to act as a routing gateway for incoming traffic.
Installing WireGuard and Generating Key Pairs
WireGuard simplifies encryption by abandoning traditional cryptographic negotiation protocols. Instead, it relies on modern state of the art primitives including Curve25519 for key exchange, ChaCha20 for symmetric encryption, and Poly1305 for data authentication. Every peer in a WireGuard network has a public key and a private key.
Install the wireguard package along with resolvconf, which allows client network configurations to automatically update system DNS resolvers upon establishing a tunnel connection:
sudo apt install wireguard resolvconf -y
Navigate to the WireGuard configuration directory on your server. Restrict folder permissions so that only the root user can read or modify generated private keys:
cd /etc/wireguard
sudo umask 077
Now, generate the server private key and extract its matching public key using the wg utility:
wg genkey | tee privatekey | wg pubkey > publickey
Inspect the files to ensure both keys were successfully generated:
cat privatekey
cat publickey
Keep your terminal open or write down these keys. The contents of privatekey will be placed directly in the server interface configuration file, while publickey will be distributed to clients connecting to this server.
Now, generate a dedicated key pair for your first client device (for example, your laptop or mobile phone) right on the server:
wg genkey | tee client_privatekey | wg pubkey > client_publickey
You now possess the cryptographic material required to build an encrypted bridge between your cloud gateway and client endpoints.
Configuring the WireGuard Interface and Firewall Rules
WireGuard interfaces are configured using simple INI-style files. By convention, the primary interface is named wg0, which corresponds to a configuration file located at /etc/wireguard/wg0.conf.
Before creating the configuration, identify the name of your server public physical network interface using the ip command:
ip route show default | awk '{print $5}'
On Ubuntu cloud instances hosted on providers like Contabo VPS or Hetzner VPS, this interface is typically named eth0, ens3, or enp1s0. For the configuration below, assume the primary public interface name is eth0.
Create the main interface configuration file /etc/wireguard/wg0.conf:
sudo nano /etc/wireguard/wg0.conf
Paste the following absolute configuration into the file. Replace <SERVER_PRIVATE_KEY> with the exact contents of the privatekey file you generated earlier, and replace <CLIENT_PUBLIC_KEY> with the exact contents of the client_publickey file:
[Interface]
PrivateKey = <SERVER_PRIVATE_KEY>
Address = 10.8.0.1/24, fd42:42:42::1/64
ListenPort = 51820
PostUp = iptables -A FORWARD -i wg0 -j ACCEPT; iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE; ip6tables -A FORWARD -i wg0 -j ACCEPT; ip6tables -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; ip6tables -D FORWARD -i wg0 -j ACCEPT; ip6tables -t nat -D POSTROUTING -o eth0 -j MASQUERADE
[Peer]
PublicKey = <CLIENT_PUBLIC_KEY>
AllowedIPs = 10.8.0.2/32, fd42:42:42::2/128
This configuration assigns the private IP address 10.8.0.1 to the VPN server itself. The PostUp and PostDown rules use iptables to dynamic network address translation (NAT). When the WireGuard interface comes up, it enables packet masquerading over eth0, routing client packets out to the public internet securely.
Now adjust UFW (Uncomplicated Firewall) rules to allow traffic on WireGuard default UDP port 51820.
First, edit /etc/default/ufw to enable default packet forwarding:
sudo nano /etc/default/ufw
Find the DEFAULT_FORWARD_POLICY setting and change its value from DROP to ACCEPT:
DEFAULT_FORWARD_POLICY="ACCEPT"
Save and exit. Next, permit SSH and UDP port 51820 through UFW, then reload the firewall:
sudo ufw allow 22/tcp
sudo ufw allow 51820/udp
sudo ufw reload
Enable and start the WireGuard systemd service so that the tunnel automatically initialises upon server boot:
sudo systemctl enable wg-quick@wg0
sudo systemctl start wg-quick@wg0
Verify that the WireGuard interface is running and listening:
sudo wg show
💡 Fast-Track Your Project: Don’t want to configure this yourself? I build custom n8n pipelines and bots. Message me with code SYS3-HUGO.
Creating Client Configurations and Establishing Tunnels
With the server listening, you must now construct the client configuration file. This file will be loaded into a WireGuard client application on your laptop, desktop, or mobile phone.
On your server, build a client configuration file named wg0-client.conf:
sudo nano /etc/wireguard/wg0-client.conf
Add the following complete configuration details. Replace <CLIENT_PRIVATE_KEY> with the contents of your client_privatekey file, replace <SERVER_PUBLIC_KEY> with the contents of your server publickey file, and replace <YOUR_SERVER_PUBLIC_IP> with your public cloud instance IP address:
[Interface]
PrivateKey = <CLIENT_PRIVATE_KEY>
Address = 10.8.0.2/24, fd42:42:42::2/64
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey = <SERVER_PUBLIC_KEY>
Endpoint = <YOUR_SERVER_PUBLIC_IP>:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
Setting AllowedIPs = 0.0.0.0/0, ::/0 routes all client internet traffic through the encrypted WireGuard tunnel (Full Tunnel mode). If you only want to access internal private cloud servers over the VPN without routing general web browsing through it (Split Tunnel mode), adjust the setting to specify the target internal IP subnet:
AllowedIPs = 10.8.0.0/24
To easily transfer this configuration to a mobile phone running the official WireGuard app, convert the configuration file into a QR code inside your terminal. First, install the qrencode utility:
sudo apt install qrencode -y
Then generate a terminal displayable QR code:
qrencode -t ansiutf8 < /etc/wireguard/wg0-client.conf
Scan this code using the mobile app to automatically import the configuration and establish your secure VPN tunnel instantly.
Routing Internal Microservices Across the WireGuard Mesh
Once your WireGuard mesh network is active, your cloud infrastructure no longer requires public internet exposure for internal microservices, administrative dashboards, or database backends.
By binding your infrastructure strictly to the private WireGuard IP address space (such as 10.8.0.1 or additional peer addresses), you isolate your application architecture from public port scans and brute force attacks.
For instance, when configuring self-hosted platforms, you can bind services directly to internal interfaces. If you are Deploying Self-Hosted PocketBase Backend on Ubuntu, you can instruct the database server to listen strictly on 10.8.0.1:8090 rather than 0.0.0.0:8090. This ensures that only clients authenticated onto your WireGuard VPN network can access administrative APIs.
If you are exposing administrative web portals to specific internal users via web proxies, check out our guide on Securing Nginx Reverse Proxy Subdomains with Certbot to handle SSL certificates efficiently while routing upstream requests across private encrypted adapters.
Furthermore, if your application architecture involves distributing data across multiple cloud nodes on providers like DigitalOcean and Hetzner VPS, you can secure cross-datacenter database streaming. If you are Configuring PostgreSQL Change Data Capture with Debezium, running the CDC pipeline transport over WireGuard IP aliases guarantees end-to-end encryption for sensitive data streams moving between cloud hosting environments.
To verify handshakes and active bandwidth consumption across all connected peers, run the monitoring command on your Ubuntu gateway:
sudo wg show wg0
The terminal output provides real-time statistics regarding handshake timing and bandwidth transferred across each active peer tunnel:
interface: wg0
public key: 8vKxX...
private key: (hidden)
listening port: 51820
peer: bM4zP...
endpoint: 203.0.113.45:62104
allowed ips: 10.8.0.2/32, fd42:42:42::2/128
latest handshake: 14 seconds ago
transfer: 1.42 MiB received, 12.85 MiB sent
Getting Started
Deploying WireGuard on an Ubuntu cloud server gives you complete control over your privacy, infrastructure accessibility, and traffic encryption. By bypassing heavy protocol stacks, WireGuard offers fast speeds alongside minimal CPU overhead.
To start building your own private network gateway, choose a cloud server provider:
- Launch a fast instance on Hetzner VPS or Contabo VPS.
- Deploy scalable developer nodes using DigitalOcean.
- Register custom domain hostnames for your VPN nodes through Namecheap.
Outsource Your Automation
Don’t have time? I build production n8n workflows, WhatsApp bots, and fully automated YouTube Shorts pipelines. Hire me on Fiverr, mention SYS3-HUGO for priority. Or DM at chasebot.online.
Want to automate this yourself?
Start with n8n Cloud (free tier available) or self-host on a Hetzner VPS for full control.
Want this engine running on your own VPS?
This blog publishes itself — daily, unattended, on free API tiers. The full engine, Hugo theme, and setup guide are available as System 3.
Get System 3