How to Set Up WireGuard VPN on Ubuntu 24.04
Learn how to set up wireguard vpn on ubuntu 24.04 with kernel modules, cryptographic keys, server and client configurations, and firewall forwarding.

5 min read
Learn how to set up wireguard vpn on ubuntu 24.04 with kernel modules, cryptographic keys, server and client configurations, and firewall forwarding.
- Prerequisites and Supported Releases
- Step 1: Installing WireGuard Tools on Ubuntu 24.04
- Step 2: Generating Server and Client Cryptographic Keys
Virtual Private Networks (VPNs) are essential tools for securing internet communications, encrypting data over untrusted public Wi-Fi networks, and establishing private tunnels into remote cloud infrastructure.
Traditional VPN protocols like OpenVPN and IPsec are notoriously complex to configure, carry massive codebases with legacy cryptographic ciphers, and suffer from high latency and CPU overhead.
WireGuard represents a modern revolution in VPN technology.
Built directly into the Linux kernel since version 5.6, WireGuard runs with blistering performance, uses modern state-of-the-art cryptography (Noise protocol framework, Curve25519, ChaCha20-Poly1305, and BLAKE2s), and contains fewer than 4,000 lines of code.
In this guide, we walk through how to set up wireguard vpn on ubuntu from server key generation to routing encrypted client traffic.
Complete step-by-step administrator guide to deploying a secure, high-speed WireGuard VPN server on Ubuntu 24.04 LTS (Noble Numbat). Covers cryptographic key generation, configuring the wg0 server interface, enabling IPv4 packet forwarding in the Linux kernel, configuring UFW NAT masquerading, and connecting desktop and mobile clients.
Prerequisites and Supported Releases
This guide is thoroughly verified on Ubuntu 24.04 LTS (Noble Numbat) & Ubuntu 22.04 LTS. The instructions apply cleanly to cloud instances (AWS EC2, Google Compute Engine, DigitalOcean, Hetzner), bare-metal servers, and local virtual machines.
Before proceeding, ensure you have the following requirements satisfied:
- A server running Ubuntu 24.04 LTS or Ubuntu 22.04 LTS with a public IPv4 address.
- A non-root user with
sudoadministrative privileges. - UFW firewall installed and configured.
- A client device (Linux, macOS, Windows, iOS, or Android) with WireGuard client installed.
Step 1: Installing WireGuard Tools on Ubuntu 24.04
Because WireGuard runs as an in-tree kernel module in Ubuntu 24.04 LTS, you only need to install the user-space management utilities.
Refresh your APT repositories and install the wireguard package along with iptables for routing rules:
Verify that the WireGuard kernel module is loaded and operational:
# Update repository cache and install WireGuard:
sudo apt update
sudo apt install -y wireguard iptables
# Verify kernel module status:
sudo modprobe wireguard
lsmod | grep wireguard
# Output: wireguard 114688 0
Step 2: Generating Server and Client Cryptographic Keys
WireGuard relies on public-key cryptography where each peer generates a private key and a corresponding public key.
Peers authenticate each other by exchanging public keys, similar to SSH authentication.
Create a secure directory with restricted permissions to hold the server and client cryptographic material:
Generate the private and public keys using the wg command-line utility:
# Create secure directory with 700 permissions:
sudo mkdir -p /etc/wireguard
sudo chmod 700 /etc/wireguard
cd /etc/wireguard
# Generate server private and public keys:
umask 077
wg genkey | tee server_private.key | wg pubkey > server_public.key
# Generate client private and public keys:
wg genkey | tee client_private.key | wg pubkey > client_public.key
# View generated keys:
cat server_public.key
cat client_public.key
Step 3: Creating the WireGuard Server Configuration (wg0.conf)
WireGuard interfaces are configured through standard INI configuration files located in /etc/wireguard/.
Identify your server's primary public network interface name (such as eth0 or ens3) using the ip route command:
Create the server configuration file at /etc/wireguard/wg0.conf, substituting your server private key, client public key, and public interface name:
# Find public interface name:
ip route show default | awk '{print $5}'
# Output: eth0
# /etc/wireguard/wg0.conf
[Interface]
Address = 10.0.0.1/24
ListenPort = 51820
PrivateKey = <PASTE_CONTENTS_OF_server_private.key>
# Firewall packet routing and NAT masquerading rules:
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
# Client Peer Definition
[Peer]
PublicKey = <PASTE_CONTENTS_OF_client_public.key>
AllowedIPs = 10.0.0.2/32
Step 4: Enabling Kernel Packet Forwarding and Firewall Access
To allow the server to forward internet traffic on behalf of connected VPN clients, IPv4 packet forwarding must be enabled in the Linux kernel.
Open /etc/sysctl.conf and uncomment net.ipv4.ip_forward=1:
Apply the kernel sysctl parameter immediately without rebooting:
Open the WireGuard UDP listening port (51820) in your UFW firewall:
# Enable IPv4 forwarding permanently:
sudo sed -i 's/#net.ipv4.ip_forward=1/net.ipv4.ip_forward=1/' /etc/sysctl.conf
# Apply sysctl settings immediately:
sudo sysctl -p
# Output: net.ipv4.ip_forward = 1
# Allow WireGuard UDP traffic through UFW:
sudo ufw allow 51820/udp comment 'WireGuard VPN'
sudo ufw status
Step 5: Starting and Enabling the WireGuard Interface
The wg-quick script manages WireGuard network interfaces and configures routing tables automatically.
Start the wg0 interface and enable it as a systemd service to persist across system reboots:
Inspect active interface status and handshake telemetry using wg show:
# Start the WireGuard interface:
sudo wg-quick up wg0
# Enable systemd service for wg0:
sudo systemctl enable wg-quick@wg0
# Inspect interface state:
sudo wg show
# Output:
# interface: wg0
# public key: 4nJ8...=
# private key: (hidden)
# listening port: 51820
#
# peer: aZ9k...=
# allowed ips: 10.0.0.2/32
Step 6: Configuring Client Devices (wg0-client.conf)
Create a configuration file for your client device named wg0-client.conf.
Set AllowedIPs = 0.0.0.0/0 to route all client internet traffic through the encrypted VPN tunnel, or specify private CIDR blocks for split-tunneling:
Replace YOUR_SERVER_PUBLIC_IP with your Ubuntu server's public IP address:
# wg0-client.conf
[Interface]
PrivateKey = <PASTE_CONTENTS_OF_client_private.key>
Address = 10.0.0.2/24
DNS = 1.1.1.1, 8.8.8.8
[Peer]
PublicKey = <PASTE_CONTENTS_OF_server_public.key>
Endpoint = YOUR_SERVER_PUBLIC_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25
Generating Mobile QR Codes for iOS and Android
You can import client configurations into the mobile WireGuard app instantly by scanning a terminal QR code.
Install qrencode and display your client configuration file as an ASCII QR code in your terminal:
# Install QR code generator:
sudo apt install -y qrencode
# Display QR code in terminal:
qrencode -t ansiutf8 < wg0-client.conf
Troubleshooting and Common Errors
When configuring Ubuntu servers, unexpected permission errors, network timeouts, or lock contention can occur. Review these common issues and verified resolutions:
Client connects but cannot access the internet
Cause: IPv4 packet forwarding is disabled, or iptables MASQUERADE rules are missing in wg0.conf.
Resolution: Verify cat /proc/sys/net/ipv4/ip_forward returns 1 and ensure the public interface name in PostUp matches ip route.
sudo sysctl net.ipv4.ip_forward && sudo iptables -t nat -L -n -v
No handshake received on server (wg show displays no handshake)
Cause: UDP port 51820 is blocked by a cloud provider security group (AWS, GCP, DigitalOcean) or local firewall.
Resolution: Open UDP port 51820 in your cloud firewall / security group console.
sudo ufw status | grep 51820
DNS resolution fails on client device while connected to VPN
Cause: Missing or unreachable DNS servers defined in the [Interface] section of client configuration.
Resolution: Set DNS = 1.1.1.1, 8.8.8.8 in wg0-client.conf.
cat wg0-client.conf | grep DNS
Security Best Practices and Hardening
Keep server private keys strictly confidential with file permissions set to 600.
Set PersistentKeepalive = 25 on client configurations that reside behind NAT routers or cellular firewalls to keep connections alive.
Regularly review active peer connections using sudo wg show and revoke public keys for lost or decommissioned devices.
Related Ubuntu Tutorials and Documentation
Deepen your Linux system administration skills with our related production-tested guides:
- How to Set Up a Firewall with UFW on Ubuntu 24.04 — Configure UFW firewall rules and allow WireGuard UDP port traffic.
- Initial Server Setup on Ubuntu 24.04 LTS — Harden your base Ubuntu system before deploying networking tunnels.
- How to Configure Networking with Netplan on Ubuntu — Manage system network interfaces and IP routing tables.
Authoritative References and Official Documentation

The Linux Command Line, 2nd Edition: A Complete Introduction
Frequently Asked Questions
Quick answers to common questions and configurations
AllowedIPs = 0.0.0.0/0) routes all client internet traffic through the VPN server. Split-tunnel VPN (e.g. AllowedIPs = 10.0.0.0/24) only routes traffic destined for specific private networks through the tunnel, preserving normal internet browsing on the local connection.10.0.0.3/32), add a new [Peer] block to /etc/wireguard/wg0.conf, and reload the server configuration with sudo wg syncconf wg0 <(wg-quick strip wg0).--cap-add=NET_ADMIN and access to /dev/net/tun.Editorial Disclosure: Website Vidya is supported by our readers. When you purchase hosting, tools, or software through our verified partner links, we may earn an affiliate commission at no extra cost to you. We only recommend solutions we trust and rigorously verify.
Rajesh Ray
I’m Rajesh Rai, a full-stack developer, Linux administrator, and technology enthusiast passionate about making complex technology easier to understand and use.
Most Read Guides
TrendingUbuntu Server Not Booting? 10 Simple Steps to Fix It Fast
How to Create a 3D Button in HTML and CSS: Easy Steps
How to Install Python and Pip on Ubuntu 24.04 LTS
How to Install Nginx on Ubuntu 24.04
How to Install Python on Ubuntu 24.04 LTS
Related Technical Guides
Tested runbooks, server administration walkthroughs, and troubleshooting guides
Join the Conversation & Perspective
Have thoughts, counterpoints, or reflections on this story? Engage directly with fellow readers and editorial contributors.
No comments on this guide yet. Be the first to share your thoughts or ask a question!