Home UbuntuHow to Set Up a WireGuard VPN Server on Ubuntu

How to Set Up a WireGuard VPN Server on Ubuntu

by Knowledge Base Linux
How to Set Up a WireGuard — How to Set Up a WireGuard VPN Server on Ubuntu

This article covers How to Set Up a WireGuard in detail. A WireGuard VPN server on Ubuntu is the fastest way to give yourself and your team private, encrypted access to a server or an entire network. WireGuard is built into the Linux kernel, uses state-of-the-art cryptography, and its codebase is tiny compared with legacy VPNs, which makes it both quick and auditable. A WireGuard VPN server on Ubuntu takes about fifteen minutes to deploy: generate keys, write one configuration file, open one UDP port, and add each client as a “peer”. This guide walks through the full server setup plus a laptop and phone client, and ends with the checks that prove the tunnel really works.

How a WireGuard VPN Server on Ubuntu Works – How to Set Up a WireGuard

WireGuard identifies machines by public keys, not usernames and passwords. The server holds a list of peers, each with a public key and an allowed IP range inside the tunnel. Clients do the reverse: they know the server’s public key and its public endpoint. There are no certificates to renew and no negotiation dance; if a packet is not signed by a known key, it is silently dropped, which makes an idle WireGuard port invisible to port scans. Ubuntu 20.04 and newer include the kernel module, so installation is just the user-space tools.

Prerequisites – How to Set Up a WireGuard

  • An Ubuntu 20.04, 22.04 or 24.04 server with a public IP and sudo access.
  • UDP port 51820 reachable (open it in any cloud security group as well as the local firewall).
  • The name of your public network interface. Find it with ip route get 1.1.1.1 and note the dev value, for example eth0 or ens3.

Step 1: Install WireGuard

sudo apt update
sudo apt install wireguard qrencode -y

Step 2: Generate the Server Keys

Restrict permissions first, because the private key must stay private:

umask 077
wg genkey | sudo tee /etc/wireguard/server_private.key > /dev/null
sudo cat /etc/wireguard/server_private.key | wg pubkey | sudo tee /etc/wireguard/server_public.key

Print both keys and keep them somewhere safe for the next steps:

sudo cat /etc/wireguard/server_private.key
sudo cat /etc/wireguard/server_public.key

Step 3: Write the Server Configuration

We will use the tunnel subnet 10.8.0.0/24, with the server at 10.8.0.1. Create /etc/wireguard/wg0.conf, replacing eth0 with your public interface and pasting the server private key:

[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY_HERE
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

The PostUp rules NAT the clients’ traffic out to the internet; PostDown removes them when the tunnel stops. Networking fundamentals like interface names and routes are covered in our Netplan static IP guide if you need a refresher.

Devices connected to a WireGuard VPN server on Ubuntu

Step 4: Enable IP Forwarding and the Firewall

echo "net.ipv4.ip_forward = 1" | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf
sudo ufw allow 51820/udp
sudo ufw allow OpenSSH

Without forwarding, clients can reach the server itself but nothing beyond it. The UFW tutorial explains how to check the final ruleset with sudo ufw status verbose.

Step 5: Start the VPN Service

sudo systemctl enable --now wg-quick@wg0
sudo wg show

The second command should list interface wg0 listening on port 51820. No peers yet, which the next step fixes.

Step 6: Create a Client and Add It as a Peer

Generate a key pair for the client (repeat per device), then append the peer to the server configuration:

wg genkey | tee ~/client1_private.key | wg pubkey > ~/client1_public.key
cat ~/client1_public.key
[Peer]
PublicKey = CLIENT1_PUBLIC_KEY_HERE
AllowedIPs = 10.8.0.2/32

Reload without dropping the interface using sudo wg-quick strip wg0 | sudo wg syncconf wg0 /dev/stdin, or simply restart wg-quick@wg0 during setup. Then write the client configuration file client1.conf:

[Interface]
PrivateKey = CLIENT1_PRIVATE_KEY_HERE
Address = 10.8.0.2/24
DNS = 1.1.1.1

[Peer]
PublicKey = SERVER_PUBLIC_KEY_HERE
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

AllowedIPs = 0.0.0.0/0 sends all client traffic through the tunnel (full tunnel). Use 10.8.0.0/24 instead if the client should only reach the VPN subnet. For a phone, turn the file into a QR code with qrencode -t ansiutf8 < client1.conf and scan it from the WireGuard app.

Step 7: Verify the Tunnel

Connect the client, then on the server run sudo wg show. A healthy peer shows a latest handshake within the last couple of minutes and growing transfer counters. On the client, visit an IP-check website: your public address should now be the server’s. If the handshake exists but websites do not load, the problem is almost always forwarding or NAT from Steps 3–4, not the keys.

Troubleshooting

No handshake at all. UDP 51820 is blocked somewhere: check UFW, the cloud provider’s security group and any NAT router in front of the server. Also confirm the client’s Endpoint IP and port.

Handshake works, no internet. Verify sysctl net.ipv4.ip_forward returns 1 and that the PostUp interface name matches your real public interface.

Connection dies when the client changes networks. Keep PersistentKeepalive = 25 in the client config so NAT mappings stay alive.

DNS leaks or does not resolve. The client config’s DNS line only takes effect in full-tunnel mode with the official apps; set it explicitly and test a lookup after connecting.

Conclusion

Your WireGuard VPN server on Ubuntu now gives every enrolled device an encrypted path home: keys instead of passwords, one UDP port, and configuration files you can read at a glance. Add peers one at a time, keep the server’s private key offline, and check wg show whenever something feels slow. For protocol details and platform downloads, the official WireGuard website is the canonical source.

You may also like

KbLinux

Typically replies within a day

Hello, Welcome to the site. Please click below button for chatting me through Telegram.