EN

Applications

Set Up a WireGuard VPN on Your Server

Install WireGuard on Debian or Ubuntu, add Windows, macOS, Linux and mobile clients, configure routing and the firewall, then restrict SSH to the VPN.

10 min read, updated on 18 November 2026

On this page

Your values

Enter them once and the commands in this guide update.

This guide explains how to turn your server into a WireGuard gateway. Your devices connect to it through an encrypted tunnel. They can then reach the server's private services and, if you wish, go out to the Internet using its IP address.

By the end of the guide, you have a VPN that starts with the server, a first working client, and a method to add or revoke devices.

Order a Server

This guide applies to servers that you administer yourself:

Prerequisites

  • A server running Debian 12 or 13, or Ubuntu 22.04 or 24.04
  • SSH access with a user that has sudo
  • The server's public IPv4 address, written here as YOUR_SERVER_IP
  • A client device: Windows, macOS, Linux, Android or iOS

Addressing Plan and Ports

The guide uses the following values. Choose a different range if 10.8.0.0/24 is already used on one of your networks.

Element Value
VPN interface wg0
VPN network 10.8.0.0/24
Server address in the VPN 10.8.0.1
First client 10.8.0.2
Port to open inbound 51820/udp

This guide routes IPv4 through the tunnel. The client configuration also blocks IPv6 so that it cannot bypass the VPN.

Step 1: Install WireGuard

WireGuard is built into the Linux kernel of these systems. The distribution's official packages provide the wg and wg-quick tools. Open a root shell, then install them:

Terminal
sudo -i
apt update
apt install -y wireguard iptables qrencode

Unless stated otherwise, the commands to run on the server are executed in this root shell.

Step 2: Enable IPv4 Routing

The server must be able to forward client packets to the Internet:

Terminal
cat > /etc/sysctl.d/99-wireguard.conf <<EOF
net.ipv4.ip_forward = 1
EOF
sysctl -p /etc/sysctl.d/99-wireguard.conf

The command must print net.ipv4.ip_forward = 1.

Step 3: Generate the Server Keys

Terminal
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
IFACE=$(ip -4 route show default | awk '{print $5; exit}')
echo "$IFACE"

The last command prints the public network interface, for example eth0 or ens18. If it prints nothing, the server has no default IPv4 route: fix this before continuing. Keep this shell open, because the IFACE variable is used in the next step.

Step 4: Create the Server Configuration

Terminal
cat > /etc/wireguard/wg0.conf <<EOF
[Interface]
Address = 10.8.0.1/24
ListenPort = 51820
PrivateKey = $(cat /etc/wireguard/server.key)

PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o $IFACE -j MASQUERADE
PostUp = iptables -I FORWARD -i %i -o $IFACE -j ACCEPT
PostUp = iptables -I FORWARD -i $IFACE -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
PreDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o $IFACE -j MASQUERADE
PreDown = iptables -D FORWARD -i %i -o $IFACE -j ACCEPT
PreDown = iptables -D FORWARD -i $IFACE -o %i -m conntrack --ctstate RELATED,ESTABLISHED -j ACCEPT
EOF
chmod 600 /etc/wireguard/wg0.conf

The PostUp lines run when the tunnel starts and the PreDown lines when it stops. They hide the clients' addresses behind the server's and allow their traffic to pass through the machine. %i is replaced by the interface name, here wg0. These rules are placed at the top of the FORWARD chain: they also apply when UFW or Docker blocks forwarding by default.

Step 5: Open the Firewall

If UFW is active, allow the WireGuard port:

Terminal
ufw allow 51820/udp
ufw status

Step 6: Start the VPN

Terminal
systemctl enable --now wg-quick@wg0
systemctl status wg-quick@wg0 --no-pager
wg show
ss -lun | grep 51820

wg show must display the wg0 interface, its public key and port 51820. The service will restart automatically with the server.

Step 7: Add a Client

Choose a name and a free address in the VPN. For each new client, change these two values:

Terminal
cd /etc/wireguard
umask 077
mkdir -p clients
CLIENT=laptop
CLIENT_IP=10.8.0.2
wg genkey | tee clients/$CLIENT.key | wg pubkey > clients/$CLIENT.pub
wg genpsk > clients/$CLIENT.psk

cat >> wg0.conf <<EOF

[Peer]
# $CLIENT
PublicKey = $(cat clients/$CLIENT.pub)
PresharedKey = $(cat clients/$CLIENT.psk)
AllowedIPs = $CLIENT_IP/32
EOF

systemctl reload wg-quick@wg0

Reloading applies the client list without disconnecting those already connected. The preshared key is optional in WireGuard: it adds a layer of symmetric encryption.

Next, create the file intended for the device:

Terminal
cat > clients/$CLIENT.conf <<EOF
[Interface]
PrivateKey = $(cat clients/$CLIENT.key)
Address = $CLIENT_IP/32
DNS = 1.1.1.1, 9.9.9.9

[Peer]
PublicKey = $(cat server.pub)
PresharedKey = $(cat clients/$CLIENT.psk)
Endpoint = YOUR_SERVER_IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF

PersistentKeepalive = 25 keeps the tunnel open when the device is behind a home router or a NAT network.

Step 8: Connect the Device

Install the application from the official WireGuard page, then import the configuration.

Android and iOS

Display a QR code in the terminal, then scan it from the app using the add-tunnel option:

Terminal
qrencode -t ansiutf8 < clients/$CLIENT.conf

Windows and macOS

Display the file:

Terminal
cat clients/$CLIENT.conf

Copy its contents into a laptop.conf file on your computer, then import it into the application with Import tunnel(s) from file. Then activate the tunnel.

Linux

Install wireguard on the computer, then copy the configuration to /etc/wireguard/wg0.conf:

Terminal
sudo apt install -y wireguard
sudo nano /etc/wireguard/wg0.conf
sudo chmod 600 /etc/wireguard/wg0.conf
sudo wg-quick up wg0

To stop the tunnel, use sudo wg-quick down wg0.

Remove Secrets from the Server

Once the device is configured, the server no longer needs its private key. On the server:

Terminal
shred -u clients/$CLIENT.key clients/$CLIENT.psk clients/$CLIENT.conf

Check That It Works

On the server, latest handshake must appear for the client, with transfer counters that keep increasing:

Terminal
wg show

On a Linux or macOS client, test the tunnel, then the exit address:

Terminal
ping -c 3 10.8.0.1
curl -4 https://ifconfig.me

The ping must answer and curl must print YOUR_SERVER_IP. On Windows, use ping 10.8.0.1, then look up your public IP address in a browser.

Choose What Goes Through the Tunnel

The client's AllowedIPs setting determines which traffic is sent into the VPN:

Need Client-side AllowedIPs
All traffic goes through the server 0.0.0.0/0, ::/0
Only the VPN network 10.8.0.0/24
The VPN and a private network of the server 10.8.0.0/24, 192.168.50.0/24

With ::/0, IPv6 traffic enters the tunnel but does not come out, since this guide routes only IPv4. Applications then fall back to IPv4, with no leak outside the VPN.

To reach a private network connected to the server, also add PostUp and PreDown lines for that interface, following the model in step 4, then restart the tunnel.

On the server side, AllowedIPs lists the addresses each client is allowed to use. Keep one /32 address per client. To reach a network behind a client, add it to this line, for example AllowedIPs = 10.8.0.3/32, 192.168.1.0/24. That client must itself allow routing to its network.

Let Clients Talk to Each Other

Depending on the FORWARD chain policy, clients may or may not be able to reach each other. To fix this behavior, add one of these pairs of lines to the [Interface] section of /etc/wireguard/wg0.conf.

To allow traffic between clients:

Configuration
PostUp = iptables -I FORWARD -i %i -o %i -j ACCEPT
PreDown = iptables -D FORWARD -i %i -o %i -j ACCEPT

To forbid it:

Configuration
PostUp = iptables -I FORWARD -i %i -o %i -j DROP
PreDown = iptables -D FORWARD -i %i -o %i -j DROP

Changes to the [Interface] section require a restart, which briefly interrupts the VPN:

Terminal
systemctl restart wg-quick@wg0

Restrict SSH to the VPN

Once the VPN is validated, you can close SSH on the public address. This section assumes UFW is active and denies incoming connections by default, which ufw status verbose can confirm. Adjust port 22 if you use a different one.

Allow SSH from the VPN:

Terminal
ufw allow in on wg0 to any port 22 proto tcp

From a client connected to the VPN, open a new session:

Terminal
ssh YOUR_USER@10.8.0.1

If the connection works, remove the public rule, then check the result:

Terminal
ufw status numbered
ufw delete allow 22/tcp
ufw status verbose

If your rule has a different name, such as OpenSSH, use ufw delete allow OpenSSH. You can also delete it by its number with ufw delete NUMBER.

Revoke a Client

Delete the device's [Peer] block from /etc/wireguard/wg0.conf, then reload the configuration:

Terminal
nano /etc/wireguard/wg0.conf
systemctl reload wg-quick@wg0
wg show

The client must no longer appear. Also delete /etc/wireguard/clients/laptop.pub. If the server's private key has been exposed, generate a new one and redistribute the configuration to all clients.

Update

The tools are updated by APT and the WireGuard module by the kernel:

Terminal
apt update
apt upgrade -y
wg --version

Restart the server after a kernel update. The VPN comes back automatically, but plan another way in if SSH is restricted to it.

Back Up and Restore

The /etc/wireguard folder contains the keys and the client list:

Terminal
tar -czf /root/wireguard-$(date +%F).tar.gz -C /etc wireguard sysctl.d/99-wireguard.conf
chmod 600 /root/wireguard-$(date +%F).tar.gz

This archive contains private keys: keep it encrypted and off the server.

To restore on a server where the packages are installed, replacing YYYY-MM-DD with its date:

Terminal
tar -xzf /root/wireguard-YYYY-MM-DD.tar.gz -C /etc
sysctl -p /etc/sysctl.d/99-wireguard.conf
systemctl enable --now wg-quick@wg0

If the public interface name has changed, correct it in the PostUp and PreDown lines before starting.

Troubleshooting

No latest handshake in wg show

The client cannot establish the tunnel. WireGuard never answers unauthenticated packets, so a port scanner proves nothing. Instead, watch for traffic arriving on the server while the client tries to connect:

Terminal
apt install -y tcpdump
tcpdump -ni any udp port 51820
  • No packets arrive: check Endpoint, the UFW rule and the client's network. If everything is correct, contact support from your client area to find out whether filtering is applied upstream.
  • Packets arrive with no reply: the keys do not match. Compare the client's PublicKey with server.pub, and the key in the [Peer] block with the client's.

The tunnel comes up, but the Internet does not work

Check routing, NAT and forwarding in order:

Terminal
sysctl net.ipv4.ip_forward
ip -4 route show default
iptables -t nat -S POSTROUTING
iptables -S FORWARD

The interface in the rules must be the one from the default route. If it differs, fix wg0.conf, then run systemctl restart wg-quick@wg0.

The VPN works, but domain names do not resolve

Test ping 1.1.1.1 from the client. If it succeeds, the problem is DNS: check the client's DNS line or specify another resolver.

resolvconf: command not found on a Linux client

wg-quick needs this tool to apply the DNS line. Install openresolv, or remove that line if you only route the VPN network.

iptables: command not found

The package is missing, often on a minimal Debian installation. Install it, then restart the service:

Terminal
apt install -y iptables
systemctl restart wg-quick@wg0

The kernel cannot create the interface. This happens on a system running in a container whose host does not support WireGuard. Check the module with modprobe wireguard.

modprobe: FATAL: Module wireguard not found

The kernel has often been updated without a reboot. Compare uname -r with the contents of /lib/modules, then restart the server.

Key is not the correct length or format

A key is incomplete or contains an extra character. Copy it in full, with no spaces or line breaks.

The wg0 Interface Already Exists

The message contains already exists: the interface was started outside systemd. Stop it, then restart the service:

Terminal
wg-quick down wg0
systemctl restart wg-quick@wg0

Some Sites Stay Blocked or Transfers Freeze

The packet size is probably too large for the network being crossed. Add MTU = 1380 to the client's [Interface] section, then reactivate the tunnel. If the problem persists, try 1280.

IPv6 is disabled on the Linux client. Remove ::/0 from AllowedIPs.

References