WireGuard server on Ubuntu or Debian: setup step by step

A WireGuard server is a VPS with root, one package, a pair of keys for the server and one for each device, packet forwarding with NAT and one open UDP port, usually 51820. By hand it is about a dozen commands, all of them below, with ready rules for ufw and nftables. Or an app does the same over SSH from the server's address and password and shows a QR code for the phone.

Updated

WireGuard config: server and clients with QR, AmneziaWG up to 3.1, keys

The app sets up WireGuard. In Veli: Profiles → Add → “Your own server”, the address and the password from the host, “Add the server”.

Download Veli for free
On this page

What you need

  • A VPS with a public IPv4 and root over SSH. A server at home works too if its router has a public address.
  • Ubuntu 20.04 or newer, Debian 11 or newer. WireGuard is in their kernel, nothing is compiled. Memory hardly matters.
  • A virtual machine (KVM), not a container. On OpenVZ and many LXC plans the kernel is the host's, and WireGuard may not be in it.
  • A network that lets UDP through. WireGuard has no TCP mode and no disguise: where VPNs are filtered, it is among the first to be cut.

Install and make the keys

Log in as root. The private keys must not leave the server, except the device's own one, which goes into its file.

On the server: packages
apt update &&
  apt install -y wireguard iptables qrencode
On the server: keys of the server and of the first device
umask 077
cd /etc/wireguard
wg genkey | tee server.key | wg pubkey > server.pub
wg genkey | tee phone.key | wg pubkey > phone.pub

One pair per device: the same key on two devices at once makes them throw each other off.

On the server: forwarding
echo net.ipv4.ip_forward=1 > /etc/sysctl.d/99-wireguard.conf
sysctl --system
On the server: the name of the outside interface
ip route show default

The word after dev, for example eth0 or ens3. Put it in place of eth0 below.

The server file wg0.conf

The file is /etc/wireguard/wg0.conf. SERVER_PRIVATE_KEY is what's in server.key, PHONE_PUBLIC_KEY what's in phone.pub. PostUp turns on NAT, without which devices connect but have no internet.

wg0.conf
[Interface]
PrivateKey = SERVER_PRIVATE_KEY
Address = 10.8.0.1/24
ListenPort = 51820
PostUp = iptables -t nat -A POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
PostDown = iptables -t nat -D POSTROUTING -s 10.8.0.0/24 -o eth0 -j MASQUERADE
[Peer]
PublicKey = PHONE_PUBLIC_KEY
AllowedIPs = 10.8.0.2/32

The device file phone.conf

PHONE_PRIVATE_KEY is phone.key, SERVER_PUBLIC_KEY is server.pub, IP is the server's address. ::/0 sends IPv6 into the tunnel as well: the server has none there, so it stops instead of going past the VPN.

phone.conf
[Interface]
PrivateKey = PHONE_PRIVATE_KEY
Address = 10.8.0.2/32
DNS = 1.1.1.1
[Peer]
PublicKey = SERVER_PUBLIC_KEY
Endpoint = IP:51820
AllowedIPs = 0.0.0.0/0, ::/0
PersistentKeepalive = 25

Start it and show the QR code

On the server: start now and after a reboot
systemctl enable --now wg-quick@wg0
wg show
On the server: a QR code in the terminal
qrencode -t ansiutf8 < phone.conf

Scan it in the WireGuard app or any client that reads its files. Then delete phone.conf and phone.key from the server: it only needs the public key.

On the server: keys for one more device
wg genkey | tee laptop.key | wg pubkey > laptop.pub

Then add a [Peer] with the new public key and 10.8.0.3/32 to wg0.conf.

On the server: apply it without a restart
wg syncconf wg0 <(wg-quick strip wg0)

Connected devices don't drop.

ufw: which rules WireGuard needs

Two: let the port in and let traffic through from the tunnel to the internet. The first alone gives a handshake without internet. Allow SSH before you turn ufw on, or you lock yourself out.

On the server
ufw allow OpenSSH
ufw allow 51820/udp
ufw route allow in on wg0 out on eth0
ufw enable
ufw status verbose

If SSH isn't on port 22, allow that port instead of OpenSSH. NAT stays in PostUp.

nftables instead of iptables

If you keep the rules in nftables, delete the PostUp and PostDown lines from wg0.conf and put this in /etc/nftables.conf. flush ruleset clears everything that was there, Docker's and Fail2ban's rules too, so this is for a server with nothing else on it. Check that the SSH port is yours.

/etc/nftables.conf
#!/usr/sbin/nft -f
flush ruleset

table inet filter {
  chain input {
    type filter hook input priority filter; policy drop;
    ct state established,related accept
    iif "lo" accept
    meta l4proto { icmp, ipv6-icmp } accept
    tcp dport 22 accept
    udp dport 51820 accept
  }
  chain forward {
    type filter hook forward priority filter; policy drop;
    ct state established,related accept
    iifname "wg0" oifname "eth0" accept
  }
}

table ip nat {
  chain postrouting {
    type nat hook postrouting priority srcnat;
    ip saddr 10.8.0.0/24 oifname "eth0" masquerade
  }
}
On the server: check, then apply
nft -c -f /etc/nftables.conf
systemctl enable --now nftables
systemctl restart nftables

The first line only checks the syntax and prints nothing when it's fine.

It doesn't work: what to look at

wg show on the server is the first check: with a connected device it shows latest handshake.

  • No latest handshake

    CauseUDP 51820 is closed at the host or in ufw, the key or the Endpoint is wrong, or the network cuts WireGuard

    What to doOpen the port in the host's panel too; tcpdump -ni any udp port 51820 shows if packets arrive

  • A handshake, but no internet

    CauseForwarding is off, or NAT has the wrong interface

    What to dosysctl net.ipv4.ip_forward must be 1; compare eth0 with ip route show default

  • A handshake, no internet, Docker on the server

    CauseDocker drops forwarded packets

    What to doAdd PostUp = iptables -I FORWARD -i wg0 -j ACCEPT and the same with -o wg0

  • Addresses open, names don't

    CauseDNS

    What to doDNS = 1.1.1.1 in the device's file; port 53 isn't blocked

  • Small sites open, big ones hang

    CausePackets are too large

    What to doMTU = 1280 in the device's [Interface]

  • Unknown device type or Operation not supported at start

    CauseThe kernel has no WireGuard: a container or a very old system

    What to doA KVM server, or update the system and reboot

  • Works until a second device connects

    CauseBoth have one key

    What to doA key for each device

Sources

How we write and check

In Veli

The app sets up WireGuard

In Veli: Profiles → Add → “Your own server”, the address and the password from the host, “Add the server”. Choose WireGuard on the server screen; “Customize” changes the port (51820 over UDP by default) and the DNS for devices. The app installs everything, turns on forwarding and NAT, opens the port in the server's firewall and checks it from outside. It works on Ubuntu, Debian, Fedora, CentOS, Rocky, AlmaLinux and Arch Linux, and leaves alone what is already on the server, with one exception: WireGuard and AmneziaWG take one place, and the app warns before one replaces the other.

This computer's profile appears by itself. “Add a device” gives a QR code and the configuration for a phone, each with its own key; you see the traffic of each and can delete one without touching the rest. Devices go out to the internet, but not into the private networks around the server, and mail port 25 is closed to them. “Remove from the server” takes the protocol off; reinstalling makes new keys, so the old files stop working.

FAQ

Which port does a WireGuard server need?

Any free UDP port, 51820 by custom. It has to be open on the server and in the host's panel; TCP isn't needed.

How much memory and CPU does WireGuard need?

Very little: the smallest VPS plan is enough for a family. The limit is usually the server's channel.

How many devices can one WireGuard server have?

In a /24 network up to 253, each with its own key and address. A wider network takes more.

Do I need a domain for a WireGuard server?

No, the IP is enough. A domain helps when the address may change: then you change only the DNS record.

Can I change the WireGuard port later?

Yes: ListenPort on the server, the firewall and Endpoint in every device's file.

A WireGuard server without the terminal

Give Veli the server's address and password: it installs WireGuard over SSH and shows a QR code for the phone. The client is free.