Home / Configs / Servers / WireGuard server config (wg0.conf)

WireGuard server config: wg0.conf example and generator

Set up a WireGuard VPN server on Linux with one peer, NAT and the right commands.

For Linux servers with WireGuard (Ubuntu 20.04+, Debian 11+). Clients exist for Windows, macOS, iOS and Android.

This is the file for the WireGuard server. Generate the keys on your machine first, then paste them in. Your keys stay in your browser, and it is safest to leave the placeholders and paste the real private key locally.

Create your config file

Type your own values. The file below updates as you type, and nothing you type leaves your browser.

Made with: wg genkey
From the client
Find it with: ip route

/etc/wireguard/wg0.conf
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = PASTE_SERVER_PRIVATE_KEY
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

[Peer]
PublicKey = PASTE_CLIENT_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

How this setup works

Diagram: Client, then WireGuard server, then Internet or LAN
Client (sends encrypted UDP) → WireGuard server (port 51820) → Internet or LAN (through NAT)

Step-by-step setup

  1. Install it: sudo apt install wireguard.
  2. Make the keys: wg genkey | tee server_private.key | wg pubkey > server_public.key. Do the same on each client.
  3. Save the config as /etc/wireguard/wg0.conf and lock it: sudo chmod 600 /etc/wireguard/wg0.conf.
  4. Turn on forwarding: echo "net.ipv4.ip_forward=1" | sudo tee /etc/sysctl.d/99-wireguard.conf && sudo sysctl --system.
  5. Open the port: sudo ufw allow 51820/udp.
  6. Start it: sudo systemctl enable --now wg-quick@wg0, then check with sudo wg show.

What the important lines mean

Common errors and how to fix them

No handshake
Check that the keys are not swapped, the client uses the right endpoint and port, and UDP 51820 is open on the server and any cloud firewall.
Handshake works but there is no internet
Forwarding or NAT is off. Check ip_forward and that eth0 is your real network interface.
RTNETLINK answers: Operation not supported
The kernel has no WireGuard module. Some container-based VPS plans do not allow it.
Config file permissions are too open
Run sudo chmod 600 on the file. wg-quick warns about world-readable keys.

Frequently asked questions

How do I generate WireGuard keys?
Run wg genkey, and pipe it into wg pubkey for the public key. Each side makes its own pair.
Is it safe to paste my private key into a web page?
The key stays in your browser here, but the safest habit is to leave the placeholder and paste the real key into the file on your server.
Which port does WireGuard use?
51820 UDP is the usual choice, but any free UDP port works.

Written for: WireGuard 1.0, Ubuntu 24.04 · Updated 2026-10-03

Free tools that help

Fix a problem with this setup

More server configs

See all 41 config generators