VPN Field Guides [A]

VPN field guide 2/5: build a WireGuard server without losing your way back in

This is a one-server, one-client lab on a current Ubuntu Server. It uses the example tunnel network 10.44.0.0/24 and UDP port 51820. Replace the public endpoint and interface names with your own values. Keep a working console or SSH session while changing networking, and know how to use your provider's out-of-band console before you touch the firewall. A successful handshake alone does not prove that internet routing works.

First, update the server and install WireGuard from Ubuntu's package repository. On Ubuntu the package command is sudo apt install wireguard. Generate one key pair on the server and a separate pair on the client; WireGuard's official Quick Start shows the wg genkey | wg pubkey flow. Set a restrictive umask before generating files and keep the private keys on their respective devices. Do not paste them into a forum post, a screenshot, or a QR-code generator on the web. Only exchange public keys.

Create /etc/wireguard/wg0.conf on the server, readable only by root. The minimal shape is:
[Interface]
Address = 10.44.0.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY

[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.44.0.2/32

The client has a different private key and a different address:
[Interface]
Address = 10.44.0.2/32
PrivateKey = CLIENT_PRIVATE_KEY

[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = YOUR_SERVER_PUBLIC_ADDRESS:51820
AllowedIPs = 0.0.0.0/0
PersistentKeepalive = 25

These are templates, not real keys. PersistentKeepalive is useful when a client sits behind NAT and must keep a mapping open; it is not mandatory for every peer. On the client, use a DNS server you actually intend to trust. On Ubuntu, wg-quick's DNS= line relies on resolvconf integration, which is not installed by default in every setup; check Ubuntu's common-tasks note before treating a DNS line as proof of DNS routing.

Now make the server a gateway. Permit the chosen UDP port in both the cloud firewall and the host firewall. Enable IPv4 forwarding persistently with a file under /etc/sysctl.d/ containing net.ipv4.ip_forward=1, then apply it and verify sysctl net.ipv4.ip_forward reports 1. Give packets from 10.44.0.0/24 a route out of the server; for a single public interface the Ubuntu default-gateway guide uses source NAT/masquerade on that interface. Use the rule manager already in charge of your machine (UFW, nftables or iptables) and its matching persistence method. Do not paste a generic “flush ruleset” or replace a live firewall over SSH. If you already use UFW or native nftables, adapt Ubuntu's example to that manager and test from a second connection.

Start the interface with sudo systemctl enable --now wg-quick@wg0. The client can then import its own config into the official WireGuard application. Check sudo wg show on the server: a recent handshake and rising transfer counters establish only that encrypted packets reach the peer. From the client, try 10.44.0.1, then a known public IPv4 address, then a hostname. If the first works and the public address does not, look at forwarding/NAT. If the address works and the hostname does not, look at DNS. If nothing handshakes, check UDP reachability, endpoint and both public keys. The last two guides turn those observations into repeatable tests and a troubleshooting map.

This lab is intentionally IPv4-only. Do not call it a complete leak-resistant full tunnel until you have made and tested an IPv6 decision. If you need IPv6, allocate tunnel addresses and route/firewall/NAT or delegated-prefix handling on both sides; otherwise use a verified client policy that blocks IPv6 outside the tunnel. The exact method depends on the operating system and server network.

Sources, checked 3 October 2026:
Ubuntu default-gateway guide — https://ubuntu.com/server/docs/how-to/wireguard-vpn/vpn-as-the-default-gateway/
Ubuntu common tasks — https://ubuntu.com/server/docs/how-to/wireguard-vpn/common-tasks/
Ubuntu security tips — https://ubuntu.com/server/docs/how-to/wireguard-vpn/security-tips/
WireGuard Quick Start — https://www.wireguard.com/quickstart/
Ubuntu nftables guide — https://documentation.ubuntu.com/security/security-features/network/firewall/nftables/

↑ 9

Обсуждение

Комментарии

1

На каждом уровне сначала новые комментарии; ответы остаются под своим родителем.

Написать Join — чтобы участвовать в обсуждении.

The point about a handshake not proving routing is the one that catches people. I've watched someone stare at a healthy wg show output for an hour while their traffic went nowhere because ip_forward read 0 after a reboot - the sysctl.d file was there, they just never loaded it.