On this page
- What should be ready before you begin?
- How do you install the WireGuard tools?
- How do you generate the server keys and write wg0.conf?
- How do you enable forwarding and NAT?
- How do you start the tunnel and keep it on after reboots?
- How do you add a client and print a QR code?
- How do you check that traffic really flows through the VPN?
- How do you remove a device and keep the server updated?
- When is the WireGuard image the faster choice?
- Questions
What should be ready before you begin?
You need a fresh VPS, a normal user with sudo rights, and one free UDP port for the tunnel. WireGuard is part of the Linux kernel on every supported image (Ubuntu 24.04 and 22.04, Debian 12 and 13, Rocky Linux 9, AlmaLinux 9), so there is no module to compile. The examples use the public address 198.51.100.24, the IPv6 address 2001:db8:4f2a::24 and UDP port 51820. Inside the tunnel the server takes 10.8.0.1 and fd42:42:42::1; clients take the next addresses up.
If you have not logged in yet, follow the first SSH session tutorial and then harden SSH with keys before opening any new port. Find the name of your public interface now, because the NAT rules need it:
ip route show default
ip -6 route show defaultThe word after dev (often eth0 or ens3) is the interface. This guide writes eth0; replace it if yours differs.
How do you install the WireGuard tools?
One package install gives you wg and wg-quick, and qrencode adds terminal QR codes for later. On Ubuntu and Debian:
sudo apt update
sudo apt install -y wireguard-tools qrencodeOn Rocky Linux 9 and AlmaLinux 9, enable EPEL first so that qrencode resolves on every minor release:
sudo dnf install -y epel-release
sudo dnf install -y wireguard-tools qrencodeConfirm the tools and the kernel module are present:
wg --version
sudo modprobe wireguard && lsmod | grep wireguardHow do you generate the server keys and write wg0.conf?
Create the key pair with a restrictive umask so the private key is readable by root only, then reference it from the interface file.
sudo -i
cd /etc/wireguard
umask 077
wg genkey | tee server.key | wg pubkey > server.pub
cat server.pub
exitNow create /etc/wireguard/wg0.conf with sudo nano /etc/wireguard/wg0.conf:
[Interface]
Address = 10.8.0.1/24, fd42:42:42::1/64
ListenPort = 51820
PrivateKey = PASTE_CONTENTS_OF_server.key
# Ubuntu and Debian only, see the NAT section:
PostUp = nft -f /etc/wireguard/nat.nft
PostDown = nft delete table inet wgnatThe fd42:42:42::/64 range is a unique local block, so clients get IPv6 inside the tunnel and leave the server through its public IPv6 address. Leave out the PostUp and PostDown lines on Rocky and Alma, where firewalld handles translation.
How do you enable forwarding and NAT?
The kernel must route packets between wg0 and the public interface, and outgoing tunnel traffic must be rewritten to the server address. Turn forwarding on permanently:
printf 'net.ipv4.ip_forward = 1\nnet.ipv6.conf.all.forwarding = 1\nnet.ipv6.conf.eth0.accept_ra = 2\n' | sudo tee /etc/sysctl.d/99-wireguard.conf
sudo sysctl --systemThe accept_ra = 2 line keeps router advertisements working on the public interface after IPv6 forwarding is switched on, which protects the server's own IPv6 default route.
Ubuntu and Debian: a small nftables table
Write /etc/wireguard/nat.nft. The PostUp line loads it when the tunnel starts and the PostDown line removes it again:
table inet wgnat {
chain postrouting {
type nat hook postrouting priority srcnat; policy accept;
oifname "eth0" ip saddr 10.8.0.0/24 masquerade
oifname "eth0" ip6 saddr fd42:42:42::/64 masquerade
}
}Then open the port and allow routed traffic in ufw. The ufw guide explains route rules in more depth.
sudo ufw allow 51820/udp
sudo ufw route allow in on wg0 out on eth0
sudo ufw allow OpenSSH
sudo ufw enableRocky Linux and AlmaLinux: firewalld
sudo firewall-cmd --permanent --add-port=51820/udp
sudo firewall-cmd --permanent --zone=public --add-interface=wg0
sudo firewall-cmd --permanent --zone=public --add-masquerade
sudo firewall-cmd --permanent --zone=public --add-forward
sudo firewall-cmd --reloadCurrent firewalld releases masquerade IPv6 as well as IPv4, and --add-forward lets packets move between interfaces in the same zone. See the firewalld guide for zone basics.
How do you start the tunnel and keep it on after reboots?
The wg-quick@ systemd template brings the interface up from its file name. Enable and start it in one step:
sudo chmod 600 /etc/wireguard/wg0.conf
sudo systemctl enable --now wg-quick@wg0
sudo wg show
ss -ulpn | grep 51820wg show prints the public key and listening port; ss confirms the UDP socket is open. If the unit fails, journalctl -u wg-quick@wg0 -e shows the line wg-quick choked on, which is usually a typo in a key or the interface name in the NAT file.
How do you add a client and print a QR code?
Every device gets its own key pair and its own tunnel address. Generate them on the server in a private folder, then add the peer to the running interface.
sudo -i
mkdir -p /etc/wireguard/clients && cd /etc/wireguard/clients
umask 077
wg genkey | tee phone.key | wg pubkey > phone.pub
wg set wg0 peer "$(cat phone.pub)" allowed-ips 10.8.0.2/32,fd42:42:42::2/128
wg-quick save wg0wg-quick save writes the new [Peer] into wg0.conf so it survives a restart. Next build the profile the phone will import:
cat > phone.conf <<EOF
[Interface]
PrivateKey = $(cat phone.key)
Address = 10.8.0.2/32, fd42:42:42::2/128
DNS = 9.9.9.9, 2620:fe::fe
[Peer]
PublicKey = $(cat /etc/wireguard/server.pub)
Endpoint = 198.51.100.24:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25
EOF
qrencode -t ansiutf8 < phone.conf
exitOpen the WireGuard app, choose to add a tunnel from a QR code and scan the block that fills your terminal. For a laptop, copy phone.conf over SFTP instead and import the file. AllowedIPs = 0.0.0.0/0, ::/0 sends all traffic through the server; list only 10.8.0.0/24 if you want a split tunnel that reaches the server and nothing else.
How do you check that traffic really flows through the VPN?
A recent handshake and growing byte counters prove the tunnel is alive. With the client connected, run:
sudo wg show wg0 latest-handshakes
sudo wg show wg0 transferOn the client, load any page that echoes your address: it should report 198.51.100.24 over IPv4 and an address inside 2001:db8:4f2a::/64 over IPv6. A missing handshake points to a blocked UDP port or a wrong server public key in the profile. A handshake with no web traffic nearly always means forwarding or masquerade is not active; recheck sysctl net.ipv4.ip_forward and sudo nft list ruleset.
How do you remove a device and keep the server updated?
Revoking access is a single command that drops the peer from the live interface, followed by a save:
sudo wg set wg0 peer PUBLIC_KEY_OF_DEVICE remove
sudo wg-quick save wg0After hand edits to wg0.conf, apply them without disconnecting other peers by running sudo bash -c 'wg syncconf wg0 <(wg-quick strip wg0)'. Because the tunnel lives in the kernel, normal updates (sudo apt upgrade or sudo dnf upgrade) plus a reboot after kernel releases keep it current. The automatic security updates guide takes care of the routine part, and vnstat usage monitoring shows how much of your monthly transfer the VPN consumes.
When is the WireGuard image the faster choice?
Choose the WireGuard image on the Deploy page when you want a working server in minutes and are happy with the standard layout. It is Ubuntu 24.04 with the tools installed, a server interface with its own keys and forwarding already on, so you only add peers. The manual route above is worth it when you need Debian, Rocky or Alma, a custom address plan, or want to understand every line before trusting it with your traffic.
Plans that fit this guide
Base city prices. Every plan includes a dedicated IPv4 address, IPv6, full root and all 18 images.
Questions about WireGuard VPN server (manual setup)
Which UDP port should WireGuard use?
Any free UDP port works. 51820 is the common default; some people pick 443 on UDP because restrictive networks rarely block it. Change ListenPort, the firewall rule and every client Endpoint together.
Does WireGuard log my traffic?
No. WireGuard keeps no connection or traffic logs; it only remembers the last handshake time and byte counters of each peer while the interface is up.
Can two devices share one client profile?
Not reliably. A peer key can be connected from one place at a time, so a second device using the same profile keeps stealing the session. Create one key pair per device.
Why do I have a handshake but no internet?
The tunnel works but routing does not. Check that net.ipv4.ip_forward is 1, that the NAT table names the right interface, and that ufw has a route allow rule or firewalld has masquerade and forward enabled.
How much server do I need for WireGuard?
Very little: a 1 GB plan such as A1-1 serves a household of devices. Watch the monthly transfer of your plan (2 TB on that size) rather than CPU or RAM.
Can I run WireGuard on FreeBSD 14 instead?
Yes, FreeBSD 14 includes a kernel WireGuard driver and the wireguard-tools package, but the NAT and service steps differ (pf and rc.conf), so this page covers the Linux images only.
More in VPN and private DNS
- wg-easy WireGuard web UI
- WGDashboard for WireGuard
- OpenVPN server with easy-rsa
- Pritunl VPN server
- OpenConnect VPN server (ocserv)
- AdGuard Home DNS filter
- Pi-hole DNS sinkhole
- Unbound validating resolver
- Technitium DNS Server
- IPsec VPN with strongSwan
- Squid forward proxy with authentication
- Shadowsocks proxy server
- Authoritative DNS with BIND 9