How to Install and Configure WireGuard VPN on Ubuntu 26.04

By 

•

Published on

•

11 min read

Laptop connected to a server through a blue VPN tunnel

If you want to route your laptop or phone’s internet traffic through a server you control, you can set up a WireGuard VPN on that server. WireGuard is built into the Linux kernel and uses UDP to carry encrypted traffic. Each device has its own key pair and uses the other device’s public key to authenticate it.

In this guide, we will set up a WireGuard server on Ubuntu 26.04 and use UFW to forward IPv4 traffic from the VPN to the internet. We will connect an Ubuntu client first, then use the same configuration format for Windows, macOS, Android, and iOS.

Quick Reference

TaskCommand
Start the tunnel and enable it at bootsudo systemctl enable --now wg-quick@wg0
Load changes to the server’s peerssudo systemctl reload wg-quick@wg0
Show peers and handshakessudo wg show wg0
Read service errorssudo journalctl -u wg-quick@wg0 -n 50
Check the firewall rulessudo ufw status verbose
Stop the tunnelsudo systemctl stop wg-quick@wg0

Prerequisites

You will need an Ubuntu 26.04 server with a user with sudo privileges , a reachable public IPv4 address, and at least one client device. We will use the 10.10.10.0/24 subnet for the tunnel. If your server or any client already uses that range on its local network, choose a different private subnet and use it throughout the guide.

We assume you manage the server’s firewall with UFW . If you connect to the server over SSH, make sure your SSH port is allowed before enabling or reloading the firewall.

Step 1: Install WireGuard

Refresh the package index and install the WireGuard tools and UFW on the server.

Terminal
sudo apt update
sudo apt install wireguard-tools ufw

The package provides wg, which manages WireGuard keys and peers, and wg-quick, which brings up the tunnel from a configuration file. The standard Ubuntu 26.04 kernel already includes the WireGuard module.

Step 2: Generate Server Keys

Each WireGuard peer needs its own private and public key. Use a restrictive file creation mask when you generate the server’s keys.

Terminal
sudo sh -c 'umask 077; set -C; wg genkey > /etc/wireguard/privatekey && wg pubkey < /etc/wireguard/privatekey > /etc/wireguard/publickey'

The command creates two files readable only by root. The set -C option protects existing key files from being overwritten. Keep a copy of the server’s public key for the client configuration.

Terminal
sudo cat /etc/wireguard/publickey
Warning
Share only the public key. Keep the private key and any configuration file that contains it out of version control. If you maintain a repository for your server configuration, add the secret files to .gitignore before you copy any configuration into it.

Step 3: Create the Server Configuration File

First, use the ip command to find the interface the server uses to reach the internet.

Terminal
ip -4 route get 1.1.1.1

Look for the interface name after dev. We use enp1s0 in this guide. Replace it with the name from your output in the server configuration and UFW rule.

We will create the configuration file with root-only permissions and open it in nano .

Terminal
sudo sh -c 'umask 077; touch /etc/wireguard/wg0.conf && chmod 600 /etc/wireguard/wg0.conf'
sudo nano /etc/wireguard/wg0.conf

Paste the following configuration. Replace SERVER_PRIVATE_KEY with the contents of /etc/wireguard/privatekey. You can read it in a private terminal with sudo cat /etc/wireguard/privatekey.

/etc/wireguard/wg0.confini
[Interface]
Address = 10.10.10.1/24
ListenPort = 51820
PrivateKey = SERVER_PRIVATE_KEY
PostUp = iptables -w -t nat -A POSTROUTING -s 10.10.10.0/24 -o enp1s0 -j MASQUERADE
PostDown = iptables -w -t nat -D POSTROUTING -s 10.10.10.0/24 -o enp1s0 -j MASQUERADE

Address is the server’s IP address inside the tunnel. ListenPort is the UDP port the clients will connect to. We use 51820 in this example. If you choose another port, use that same port in the firewall rules and client configurations.

PostUp adds a masquerade rule when the tunnel starts. It rewrites the source address of packets from the VPN subnet as they leave enp1s0, so replies return through the server. PostDown removes the same rule when the tunnel stops.

Step 4: Enable IP Forwarding

The server needs to forward IPv4 packets between the VPN tunnel and the public network. A sysctl drop-in keeps the setting in effect after a reboot.

Terminal
echo 'net.ipv4.ip_forward=1' | sudo tee /etc/sysctl.d/99-wireguard.conf

Load the drop-in with sysctl -p to apply the setting right away.

Terminal
sudo sysctl -p /etc/sysctl.d/99-wireguard.conf
output
net.ipv4.ip_forward = 1

The net.ipv4.ip_forward = 1 line confirms that forwarding is now on.

Step 5: Allow WireGuard Traffic in UFW

Open the WireGuard UDP port and allow routed traffic from the VPN subnet out through the outbound interface.

Terminal
sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on enp1s0 from 10.10.10.0/24 to any

The route rule allows traffic from wg0 to the outbound interface while keeping UFW’s default deny policy for other routed traffic. If UFW is already active, it applies the rules immediately.

If UFW is inactive, allow any services you need to reach remotely before enabling it. The command sudo ufw allow OpenSSH allows SSH on port 22. If SSH uses a different port, allow that port instead. Then enable the firewall.

Terminal
sudo ufw enable

If your server is behind a cloud firewall, allow inbound UDP on port 51820 there too. If a router owns the public IP address, forward UDP port 51820 to the server’s LAN address.

Step 6: Start the WireGuard Service

WireGuard ships with a wg-quick systemd template service. Start the tunnel for wg0 and enable it at boot.

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

Check the service after it starts.

Terminal
sudo systemctl status wg-quick@wg0
output
● wg-quick@wg0.service - WireGuard via wg-quick(8) for wg0
     Loaded: loaded (/usr/lib/systemd/system/wg-quick@.service; enabled; preset: enabled)
     Active: active (exited)

The active (exited) status is normal for wg-quick. The service runs wg-quick up wg0 at start time and wg-quick down wg0 on shutdown, so the tunnel comes up automatically after a reboot.

Use wg show to confirm the interface’s public key and listening port.

Terminal
sudo wg show wg0
output
interface: wg0
  public key: SERVER_PUBLIC_KEY
  private key: (hidden)
  listening port: 51820

Once you add a client, this command also lists the configured peer and its latest handshake, when available.

Step 7: Configure an Ubuntu Client

Install the same tools on the Ubuntu client.

Terminal
sudo apt update
sudo apt install wireguard-tools

Generate a separate key pair on the client and print its public key. You will use that key in the next step on the server.

Terminal
sudo sh -c 'umask 077; set -C; wg genkey > /etc/wireguard/privatekey && wg pubkey < /etc/wireguard/privatekey > /etc/wireguard/publickey'
sudo cat /etc/wireguard/publickey

Create the client configuration file with the same permissions as on the server.

Terminal
sudo sh -c 'umask 077; touch /etc/wireguard/wg0.conf && chmod 600 /etc/wireguard/wg0.conf'
sudo nano /etc/wireguard/wg0.conf

Use the following template. Replace CLIENT_PRIVATE_KEY with the contents of the client’s /etc/wireguard/privatekey, SERVER_PUBLIC_KEY with the server’s public key, and SERVER_PUBLIC_IP with the server’s reachable IPv4 address.

/etc/wireguard/wg0.confini
[Interface]
Address = 10.10.10.2/24
PrivateKey = CLIENT_PRIVATE_KEY
DNS = 1.1.1.1

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

The Address is the client’s address inside the VPN. Each client needs a unique address from the 10.10.10.0/24 range. DNS tells the client which resolver to use while the tunnel is up.

AllowedIPs = 0.0.0.0/0 makes the VPN the client’s default IPv4 route. On a dual-stack client, IPv6 can continue to use the regular network connection. To route only the VPN subnet through the tunnel, change the value to 10.10.10.0/24 and omit the DNS line to keep using the client’s usual resolver.

PersistentKeepalive = 25 sends a small packet every 25 seconds to keep stateful firewalls and NAT entries alive between the peer and the server.

Step 8: Add the Peer to the Server

Switch back to the server and open /etc/wireguard/wg0.conf to register the client.

Terminal
sudo nano /etc/wireguard/wg0.conf

Append a [Peer] block at the end of the file. Replace CLIENT_PUBLIC_KEY with the public key from the client.

/etc/wireguard/wg0.confini
[Peer]
PublicKey = CLIENT_PUBLIC_KEY
AllowedIPs = 10.10.10.2/32

AllowedIPs links the peer to a single tunnel address, written as a /32 host route. It also limits the source IPs the server accepts from this peer. Save the file, then reload WireGuard to add the peer without interrupting other connections.

Terminal
sudo systemctl reload wg-quick@wg0

Step 9: Connect the Client

Start the tunnel on the client and enable it at boot.

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

Ping the server’s VPN address to send traffic through the tunnel.

Terminal
ping -c 2 10.10.10.1

If the server replies, check the handshake and transfer counters with wg show.

Terminal
sudo wg show wg0
output
interface: wg0
  public key: CLIENT_PUBLIC_KEY
  private key: (hidden)
  listening port: 55596
  fwmark: 0xca6c

peer: SERVER_PUBLIC_KEY
  endpoint: SERVER_PUBLIC_IP:51820
  allowed ips: 0.0.0.0/0
  latest handshake: 7 seconds ago
  transfer: 220 B received, 584 B sent
  persistent keepalive: every 25 seconds

Look for a recent latest handshake and nonzero transfer counters. If the VPN is the client’s default IPv4 route, check that DNS works and that traffic leaves through the server.

Terminal
getent ahostsv4 example.com
curl -4fsS https://api.ipify.org && printf '\n'

The first command should return IPv4 addresses for example.com. The second should return the server’s outbound public IPv4 address. If you are unsure what to expect, run the same curl command on the server and compare the results.

Windows, macOS, and Mobile Clients

Install the official WireGuard app on the device you want to connect. We will reuse the Ubuntu client template with a fresh key pair and a different tunnel address.

Generate a key pair for the new device in a private temporary directory on the server.

Terminal
client_dir=$(mktemp -d)
(umask 077; wg genkey > "$client_dir/privatekey")
wg pubkey < "$client_dir/privatekey"

Copy the printed public key for the server’s [Peer] block. Create the profile in the same directory to keep it private while you edit it.

Terminal
(umask 077; touch "$client_dir/client.conf")
nano "$client_dir/client.conf"

Use the template from Step 7 with the private key from "$client_dir/privatekey" and a free tunnel address, such as 10.10.10.3/24. Use the same server public key and endpoint. On the server, repeat Step 8 with this device’s public key and AllowedIPs = 10.10.10.3/32, then reload the service.

On Windows or macOS, transfer the profile to the device over a private channel such as SCP . Open WireGuard, choose Import tunnel(s) from file, select client.conf, and click Activate.

On Android or iOS, you can scan the profile. Install qrencode from Ubuntu’s Universe repository on the server.

Terminal
sudo apt install qrencode

Use qrencode to display the profile as a QR code in your terminal.

Terminal
qrencode -t ansiutf8 < "$client_dir/client.conf"

In the mobile app, tap +, choose the QR code option, and scan the code. Give the tunnel a name and switch it on.

Warning
The profile and QR code both contain the device’s private key. Keep them private and avoid saving the QR code in a screenshot or sharing it in a message.

Once the device connects, remove the temporary files from the server.

Terminal
rm -- "$client_dir/privatekey" "$client_dir/client.conf"
rmdir -- "$client_dir"

If you transferred the profile to a desktop client, delete the downloaded copy after confirming the imported tunnel works.

Troubleshooting

No handshake on the client
Ping the server’s VPN address from the client, then run sudo wg show wg0. If there is no recent handshake, check that the server is listening on UDP port 51820, that the client uses the correct endpoint, and that each side has the other side’s public key. Check UFW, the cloud firewall, and any router port forwarding between the client and server.

The tunnel works but there is no internet access
Run sysctl net.ipv4.ip_forward on the server and check that it returns 1. Use sudo ufw status verbose to check the forwarding rule and sudo iptables -t nat -S POSTROUTING to check the masquerade rule. Both rules should use the VPN subnet and the correct outbound interface.

Client connects but DNS does not resolve
On Ubuntu 26.04, wg-quick applies the DNS setting through the resolvconf command provided by systemd-resolved. Run resolvectl status wg0 to see which resolver the tunnel is using. If you configured split tunneling, follow the DNS instructions in Step 7.

wg-quick@wg0 fails to start
Run sudo journalctl -u wg-quick@wg0 -n 50 to see the error. Check for a typo in wg0.conf, a key placeholder that you forgot to replace, or another network manager already using the wg0 interface.

The client LAN overlaps with the VPN subnet
If the client’s local network also uses 10.10.10.0/24, choose a different private subnet. Update the addresses and allowed IPs in each configuration and the subnet in the server’s forwarding and NAT rules.

Conclusion

If you lose a device, remove its [Peer] block from the server and reload wg-quick@wg0 to revoke its access. Separate keys let you remove that device without changing the other clients.

Linuxize Weekly Newsletter

A quick weekly roundup of new tutorials, news, and tips.

About the authors

Dejan Panovski

Dejan Panovski

Dejan Panovski is the founder of Linuxize, an RHCSA-certified Linux system administrator and DevOps engineer based in Skopje, Macedonia. Author of 1000+ Linux tutorials with 20+ years of experience turning complex Linux tasks into clear, reliable guides.

View author page