Install WireGuard on the Ubuntu server and Linux client, generate a key pair on each host, and put the other host’s public key in /etc/wireguard/wg0.conf. Allow inbound UDP 51820 on the server, start both peers, and ping the server’s tunnel IP from the client. Once that works, you can add forwarding and NAT for IPv4 internet access through the server.
What is actually being installed
Ubuntu server and Ubuntu client. Run these commands on each host. The wireguard package pulls in wireguard-tools, which provides wg and wg-quick. wg configures WireGuard and shows peer status. wg-quick also manages interface addresses and routes. Ubuntu APT guide, wg(8), wg-quick(8).
sudo apt update
sudo apt install wireguard
Ubuntu publishes the package for 22.04 LTS, 24.04 LTS, and 26.04 LTS. On a different Linux client, use the package listed for your distribution. The service commands below use systemd. WireGuard installation.
Generate keys with a restrictive umask
Generate each key pair on the host that will use it. Run the blocks in a terminal in a private directory on each host. The umask restricts access to newly created files. Ubuntu default gateway guide.
Ubuntu server.
umask 077
wg genkey > server.key
wg pubkey < server.key > server.pub
Linux client.
umask 077
wg genkey > client.key
wg pubkey < client.key > client.pub
Send server.pub to the client and client.pub to the server. Keep each .key file on the host that generated it. The private key goes under [Interface] on that host. wg(8).
Use an available wg0 interface on both hosts. The examples use 10.10.11.0/24 for the tunnel. If that subnet overlaps a network either host uses, choose a different subnet and update every tunnel address and rule below. Ubuntu peer-to-site configuration, Ubuntu default gateway guide.
On both hosts, create a root-owned config readable and writable only by root before pasting in the key. Fill in the matching config below and replace each placeholder, including its angle brackets. Ubuntu peer-to-site configuration, touch(1), sudoedit(8).
sudo touch /etc/wireguard/wg0.conf
sudo chmod 0600 /etc/wireguard/wg0.conf
sudo chown root: /etc/wireguard/wg0.conf
sudoedit /etc/wireguard/wg0.conf
The server config
On the Ubuntu server, save this as /etc/wireguard/wg0.conf.
[Interface]
Address = 10.10.11.1/24
ListenPort = 51820
PrivateKey = <contents of server.key>
[Peer]
PublicKey = <contents of client.pub>
AllowedIPs = 10.10.11.2/32
The client gets one tunnel address in the server’s AllowedIPs. WireGuard uses it to select the peer for outgoing packets and validate that peer’s incoming source addresses. The server learns the client’s endpoint from authenticated traffic. Ubuntu peer-to-site guide, wg(8).
The client config
On the Linux client, save this as /etc/wireguard/wg0.conf. Replace vpn.example.net with the server’s reachable public IP or DNS name.
[Interface]
Address = 10.10.11.2/24
PrivateKey = <contents of client.key>
[Peer]
PublicKey = <contents of server.pub>
Endpoint = vpn.example.net:51820
AllowedIPs = 10.10.11.1/32
The client sends only traffic addressed to 10.10.11.1 into WireGuard. Those packets end at the server itself. Enable IP forwarding when packets need to pass through the server to another network. The optional internet setup also uses source NAT to send replies back through the server. wg-quick(8), Ubuntu default gateway guide.
Allow UDP ingress
- Ubuntu host firewall. Allow inbound UDP
51820to matchListenPort. If UFW is active, runsudo ufw allow 51820/udpon the server. ufw(8). - Cloud firewall. If one protects the server, allow inbound UDP
51820there too. DigitalOcean cloud firewall rules. - Router in front of the server. Forward public UDP
51820to UDP51820at the server’s LAN address, and allow that traffic in the router’s firewall. This rule delivers WireGuard packets to the server. Ubuntu guide to WireGuard behind a router.
Start it and check it
Run this on the server first, then on the client. It starts wg0 now and enables it for boot. Ubuntu WireGuard common tasks, systemctl(1).
sudo systemctl enable --now wg-quick@wg0
On the client, ping the server’s tunnel address. WireGuard can wait for traffic before initiating a handshake. Ubuntu peer-to-site testing.
ping -c 3 10.10.11.1
On both hosts, check the peer after the ping. Look for a recent latest handshake and traffic in both directions under transfer. Ping replies confirm the server’s tunnel IP is reachable. Ubuntu peer-to-site testing.
sudo wg show
| Symptom | What to check |
|---|---|
wg0 is missing | Run sudo systemctl status wg-quick@wg0 on the affected host. Check the service error and the path and contents of wg0.conf. |
| No handshake after the ping | Check the client’s Endpoint, the server’s listening port in sudo wg show, and each UDP ingress rule. Compare each host’s displayed interface public key with the opposite host’s peer entry. |
| Handshake appears, but the tunnel ping fails | Run ip addr show dev wg0 and ip route on both hosts. Check the tunnel addresses, peer AllowedIPs, overlapping routes, and whether the server firewall permits the ping. |
| Tunnel ping works, but IPv4 internet access fails after enabling it | Check the client’s default tunnel route and the server’s forwarding, outbound firewall, return traffic, and masquerade rules. If a numeric IPv4 destination works and names fail, check the client’s DNS resolver. |
See Ubuntu WireGuard troubleshooting and the Ubuntu firewall guide for the route and firewall checks.
Optional IPv4 internet routing
Use this after the tunnel ping works. The server needs its own working IPv4 internet connection. To send the client’s IPv4 internet traffic through it, enable forwarding, allow routed traffic, and masquerade the private tunnel source addresses. Ubuntu default gateway guide.
On the server, run ip route and find the interface after dev on the default route. Replace eth0 below with that interface. In /etc/sysctl.d/70-wireguard-routing.conf, put this setting. Ubuntu default gateway guide.
net.ipv4.ip_forward = 1
For a server managed by UFW, add this block to /etc/ufw/before.rules after the opening comments and before the filter table. If a *nat section already exists, add only the -A POSTROUTING line before that section’s COMMIT. Ubuntu firewall guide.
*nat
:POSTROUTING ACCEPT [0:0]
-A POSTROUTING -s 10.10.11.0/24 -o eth0 -j MASQUERADE
COMMIT
Allow forwarding from wg0 to the uplink and reload UFW, then apply the IPv4 forwarding setting. The routed rule permits traffic from the tunnel subnet and UFW’s state tracking permits replies. ufw(8), Ubuntu default gateway guide.
sudo ufw route allow in on wg0 out on eth0 from 10.10.11.0/24
sudo ufw reload
sudo sysctl -p /etc/sysctl.d/70-wireguard-routing.conf -w
For a server managed directly with iptables, apply the forwarding setting and the rules below. The -A flag appends each rule. If an existing FORWARD rule would reject or drop this traffic first, insert the new rules before it. Save the rules using the firewall configuration loaded at boot. Ubuntu firewall guide, iptables(8).
sudo sysctl -p /etc/sysctl.d/70-wireguard-routing.conf -w
sudo iptables -t nat -A POSTROUTING -s 10.10.11.0/24 -o eth0 -j MASQUERADE
sudo iptables -A FORWARD -i wg0 -o eth0 -s 10.10.11.0/24 -j ACCEPT
sudo iptables -A FORWARD -i eth0 -o wg0 -d 10.10.11.0/24 -m state --state ESTABLISHED,RELATED -j ACCEPT
On the client, change the server peer’s AllowedIPs in /etc/wireguard/wg0.conf to the following. The server keeps the client’s /32. Restart wg0 on the client to rebuild its routes. Use a local terminal if your remote session depends on the tunnel. Ubuntu default gateway guide, Ubuntu WireGuard common tasks.
AllowedIPs = 0.0.0.0/0
sudo systemctl restart wg-quick@wg0
Check from the client with mtr. If it is missing on Ubuntu, install it with sudo apt install mtr-tiny while ordinary internet access still works. mtr(8), Ubuntu default gateway testing.
mtr -r 1.1.1.1
Look for the server’s tunnel address 10.10.11.1 as the first hop and a response from the destination. wg-quick sets the IPv4 default using policy routing, so the ordinary ip route listing can still show the client’s original default. IPv6 keeps using the client’s normal routes. Routing IPv6 through WireGuard requires an IPv6 tunnel and gateway setup. The client keeps its existing DNS settings. Ubuntu default gateway testing, wg(8), wg-quick(8).
Add or remove a client without restarting the server tunnel
On the new client, generate a fresh key pair as above. Use the client config with the new private key and Address = 10.10.11.3/24. On the server, append this peer to /etc/wireguard/wg0.conf, then start the new client once the server reload completes. Ubuntu WireGuard common tasks.
[Peer]
PublicKey = <new client's public key>
AllowedIPs = 10.10.11.3/32
To remove a client, delete its entire [Peer] block from the same server file. After either edit, reload the server service and check the live peer list. The existing tunnel subnet route already covers the new address. Ubuntu WireGuard common tasks.
sudo systemctl reload wg-quick@wg0
sudo wg show
The file edit persists across reboot and the reload applies it to the running interface. A wg set edit affects only the live interface. Keep disk edits as the source of truth, since SaveConfig = true can overwrite them on shutdown. wg(8), wg-quick(8).
For more route examples, see WireGuard AllowedIPs. Separate guides cover connecting two LANs, deploying on Raspberry Pi, and choosing an MTU.
Evidence and limits. The steps were adapted from the current Ubuntu Server WireGuard documentation and upstream WireGuard manuals. Ubuntu’s package listings cover 22.04, 24.04, and 26.04 LTS. The steps were not run for this guide.
Related across the network
- OPNsense WireGuard Road Warrior: Remote Access Setup — opnsenselab.com