Guide G-01 · Networking

How to Configure WireGuard on Ubuntu

Set up a point-to-site VPN between one Ubuntu server and one client, check the handshake, and make the tunnel come back on its own after a reboot.

Outcome
  • A WireGuard interface on the server that starts at boot
  • A client that reaches the server at 10.8.0.1 through the tunnel
  • Private keys that never left the machine they were made on
Written for
Ubuntu 24.04 LTS on the server; Ubuntu or the WireGuard app on the client
Software
WireGuard in the Ubuntu kernel, plus the wireguard package from Ubuntu’s repositories. Versions not pinned.
Time · level
About 40 minutes · Intermediate: you’re comfortable with SSH and sudo
Last edited
Oct 24, 2026
Status
Editorial draftCommands and output not yet verified on a test system.

Before you start

This guide connects one client to one Ubuntu server. When you’re done, the client reaches the server at 10.8.0.1 through an encrypted tunnel, while its normal internet traffic goes out the way it always has.

You’ll need

  • An Ubuntu 24.04 LTS server with a public IP address and SSH access
  • A user with sudo rights on the server, and on the client if it runs Ubuntu
  • Permission to open UDP port 51820, including in any cloud provider firewall
  • A client: another Ubuntu machine, or any device that runs the official WireGuard app

Address plan

RoleHostTunnel addressListens onKey files
Servervpn-01 · 203.0.113.1010.8.0.1/24UDP 51820server.key · server.pub
Clientlaptop10.8.0.2/32connects outclient.key · client.pub
Scroll the table sideways to see every column →

203.0.113.0/24 is reserved for documentation (RFC 5737). Replace it with your server’s real public address, and choose a tunnel subnet that doesn’t overlap any network the client joins.

client · laptop
wg0 10.8.0.2
client private key stays here
server · vpn-01
public 203.0.113.10
wg0 10.8.0.1
server private key stays here
Fig. 1Only public keys travel between the two machines. Step 8 confirms the blue interface is reachable.
Step 1 of 8 · Server

Install the WireGuard tools

Connect to the server over SSH and install the userspace tools. Nothing starts or listens yet; this only adds the commands the next steps use.1

WireGuard itself is part of the Linux kernel Ubuntu ships. The wireguard package adds the wg and wg-quick commands.

Server · shell
sudo apt update
sudo apt install wireguard

Confirm the tools are on the path:

Server · shell
wg --version
Expected output · illustrative
wireguard-tools v1.0.<date> - https://git.zx2c4.com/wireguard-tools/

Illustrative. The version on your system will differ; any version line means the tools are installed.

Checkpoint
wg --version prints a version line instead of “command not found”.
Step 2 of 8 · Server

Generate the server’s key pair

Each WireGuard peer has a private key it keeps and a public key it hands to others. Generate the server’s pair straight into /etc/wireguard, so the private key never sits in your home directory.2

The public key is safe to share; it is how peers recognize the server. The private key should exist in exactly one place.

Server · shell
sudo install -d -m 700 /etc/wireguard
wg genkey | sudo tee /etc/wireguard/server.key | wg pubkey | sudo tee /etc/wireguard/server.pub
sudo chmod 600 /etc/wireguard/server.key
Expected output · illustrative
<server public key: 44 characters ending in =>

Illustrative. The command prints the public key once. This page never shows a real key.

Checkpoint
sudo ls -l /etc/wireguard lists server.key as -rw------- and server.pub next to it.
Step 3 of 8 · Client

Create the client’s key pair

On the client, install the same package and generate a separate pair. Every device gets its own keys; never reuse the server’s.3

Only public keys travel between machines. Move them however you like: they aren’t secret.

Client · shell
sudo apt install wireguard
sudo install -d -m 700 /etc/wireguard
wg genkey | sudo tee /etc/wireguard/client.key | wg pubkey | sudo tee /etc/wireguard/client.pub
sudo chmod 600 /etc/wireguard/client.key
Then
  1. Copy the public key the command printed. You’ll paste it into the server’s configuration in step 4.
Checkpoint
You have the client’s public key, and client.key stayed on the client.
Step 4 of 8 · Server

Write the server configuration

wg-quick reads one file per interface, and the interface takes its name from the file. wg0.conf creates wg0.4

On the server, AllowedIPs works as a routing rule and an access rule at once: this peer is accepted only from 10.8.0.2.

Server · shell
sudo nano /etc/wireguard/wg0.conf
File · /etc/wireguard/wg0.conf
configuration file
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = <paste the contents of server.key>

[Peer]
# laptop
PublicKey = <paste the client’s public key>
AllowedIPs = 10.8.0.2/32
Fill in the placeholders
  1. Replace both values in angle brackets, brackets included.
  2. To read the private key, run sudo cat /etc/wireguard/server.key and paste it in.
  3. Save the file, then restrict it: sudo chmod 600 /etc/wireguard/wg0.conf.
Checkpoint
wg0.conf contains no angle brackets, and sudo ls -l shows it as -rw-------.
Step 5 of 8 · Server

Open the port in the firewall

The server has to accept UDP packets on port 51820. If you use ufw, allow SSH first, so turning the firewall on can’t cut off your session.5

Cloud providers often run a second firewall outside the server. If yours does, allow UDP 51820 there as well.

Server · shell
sudo ufw allow OpenSSH
sudo ufw allow 51820/udp
sudo ufw enable
sudo ufw status
Expected output · illustrative
Status: active

To                         Action      From
--                         ------      ----
OpenSSH                    ALLOW       Anywhere
51820/udp                  ALLOW       Anywhere
OpenSSH (v6)               ALLOW       Anywhere (v6)
51820/udp (v6)             ALLOW       Anywhere (v6)

Illustrative. Rules that already exist on your server will be listed too.

Checkpoint
sudo ufw status lists 51820/udp as ALLOW, and your SSH session is still connected.
Step 6 of 8 · Server

Start the tunnel and enable it at boot

Bring the interface up through systemd, so it comes back on its own after a reboot.6

wg-quick@wg0 is a systemd template unit. The part after @ is the name of the configuration file.

Server · shell
sudo systemctl enable --now wg-quick@wg0
sudo wg show
Expected output · illustrative
interface: wg0
  public key: <server public key>
  private key: (hidden)
  listening port: 51820

peer: <client public key>
  allowed ips: 10.8.0.2/32

Illustrative. There’s no handshake line yet because the client hasn’t connected.

Checkpoint
sudo wg show lists interface wg0 on port 51820, with your client as a peer.
Step 7 of 8 · Client

Configure the client and connect

Create the client’s own wg0.conf. Endpoint tells the client where to find the server; AllowedIPs decides which traffic goes into the tunnel.7

AllowedIPs = 10.8.0.0/24 sends only tunnel traffic through WireGuard. Everything else on the client is unchanged.

Client · shell
sudo nano /etc/wireguard/wg0.conf
File · /etc/wireguard/wg0.conf (client)
configuration file
[Interface]
Address = 10.8.0.2/24
PrivateKey = <paste the contents of client.key>

[Peer]
PublicKey = <paste the contents of server.pub>
Endpoint = 203.0.113.10:51820
AllowedIPs = 10.8.0.0/24
PersistentKeepalive = 25
Client · shell
sudo chmod 600 /etc/wireguard/wg0.conf
sudo wg-quick up wg0
Checkpoint
wg-quick finishes without errors, and ip a shows a wg0 interface with 10.8.0.2.
Step 8 of 8 · Both

Verify the handshake and the route

Two checks: the peers have completed a handshake, and traffic really crosses the tunnel. Do both, because either can pass while the other fails.8

PersistentKeepalive sends a small packet every 25 seconds, which keeps the path open through NAT routers that forget idle connections.

Client · terminal
ping -c 3 10.8.0.1
Server · shell
sudo wg show
Expected output · illustrative
peer: <client public key>
  endpoint: <client public IP>:<port>
  allowed ips: 10.8.0.2/32
  latest handshake: 14 seconds ago
  transfer: 1.24 KiB received, 1.02 KiB sent

Illustrative. The timing and byte counts will differ. What matters is that a latest handshake line appears.

Checkpoint
ping gets replies from 10.8.0.1, and sudo wg show reports a handshake within the last two minutes.

Before you rely on it

0 of 6 done

Run through this once the tunnel works. Your ticks are saved in this browser only.

Troubleshooting

Check that the server firewall and any cloud firewall allow UDP 51820, that Endpoint holds the server’s public address and port, and that each side has the other’s public key, not its own. A key pasted without its trailing = also fails quietly.

Check that the client’s address sits inside the AllowedIPs the server lists for that peer (10.8.0.2/32), and that the client’s AllowedIPs include 10.8.0.1. If ufw filters traffic arriving on the tunnel, allow it.

Server · shell
sudo ufw allow in on wg0

The file is readable by other users. Restrict it, then restart the interface.

Server · shell
sudo chmod 600 /etc/wireguard/wg0.conf
sudo systemctl restart wg-quick@wg0

Read the most recent log lines for the unit. A mistyped key or a missing = is the usual cause.

Server · shell
journalctl -u wg-quick@wg0 -n 30

A NAT router between client and server is forgetting the connection. Keep PersistentKeepalive = 25 in the client’s [Peer] section.

Another interface or service is listening on it. Find out which, then change ListenPort and the client’s Endpoint to match.

Server · shell
sudo ss -ulpn | grep 51820