Your own VPN server · WireGuard, Outline, AmneziaWG · 6 cities · Pay in BTC, USDT, USDC, ETH, SOL, LTC, XMR
Language: English
Get a VPN server

Guide · UFW firewall rules for WireGuard

How do I open the WireGuard port in UFW?

Allow SSH before anything else, then the WireGuard UDP port, then a route rule so tunnel traffic can leave the server.

Last updated: · 4 min read

Quick answer

How do I open the WireGuard port in UFW?

To open WireGuard port UFW rules take 3 lines on a TunnelVPS server: ufw allow 22/tcp so SSH stays reachable, ufw allow 51820/udp for the tunnel, and ufw route allow in on wg0 out on eth0 so traffic can leave. Then run ufw enable and check with ufw status verbose. Allow SSH first, always.

In brief
  • Order matters: allow SSH before ufw enable, or the session can drop.
  • Tunnel port: ufw allow 51820/udp, or whatever ListenPort says.
  • Routing: ufw route allow in on wg0 out on eth0 for traffic going out.
  • Forwarding: also set in /etc/ufw/sysctl.conf, per the ufw manual.

What are the 5 steps to open the port?

Opening the WireGuard port in UFW on a TunnelVPS server, 5 steps, from the ufw(8) manual read 16 November 2026
StepWhereWhat to do
1ServerInstall UFW with apt install ufw if it is missing.
2ServerAllow SSH: ufw allow 22/tcp.
3ServerAllow WireGuard: ufw allow 51820/udp.
4ServerAllow routing: ufw route allow in on wg0 out on eth0, and set forwarding in /etc/ufw/sysctl.conf.
5ServerRun ufw enable, then ufw status verbose from a second SSH window.

Do you need UFW at all on a VPN server?

No, but a firewall that allows only SSH and the VPN port shrinks what the internet can reach. Inbound UDP and TCP ports are open on the server; the customer chooses the VPN port and sets the firewall. So nothing is filtered until you add rules.

UFW is the simplest front end for that on Debian and Ubuntu. Searches for open WireGuard port UFW usually come from people whose tunnel stopped after they turned the firewall on. The steps below avoid that.

Why must SSH be allowed before ufw enable?

Because enabling UFW can cut existing connections, the SSH session included. The ufw manual says to add an SSH rule before ufw enable. When you enable it over SSH, ufw asks for confirmation first.

Keep a second SSH window open while you test. If you moved SSH to another port, allow that number instead of 22. The SSH hardening guide covers keys and safe testing.

Allow SSH and WireGuard, then enable UFW
apt install ufw
ufw allow 22/tcp
ufw allow 51820/udp
ufw enable
ufw status verbose

Why is a route rule needed for tunnel traffic?

Because UFW filters packets passing through the server separately from packets addressed to it. The ufw manual uses the route keyword for this, with the example ufw route allow in on eth1 out on eth2. For WireGuard, traffic comes in on wg0 and leaves on the internet interface.

The manual adds that route rules alone are not enough. IP forwarding must also be set in /etc/ufw/sysctl.conf, then UFW restarted with ufw disable and ufw enable. Replace eth0 with the name shown after dev in ip route.

Route rule for tunnel traffic
ufw route allow in on wg0 out on eth0
# set forwarding in /etc/ufw/sysctl.conf, then:
ufw disable
ufw enable

What about the NAT rule and forwarding outside UFW?

Keep the MASQUERADE line in PostUp; UFW does not replace it. The route rule lets packets through, and the NAT rule gives them the server's public address. Both are needed for a full tunnel.

The IP forwarding guide explains the sysctl keys and the NAT lines. The complete wg0.conf sits in the one-command install guide.

How do you change or remove a rule later?

Prefix the original rule with delete, or delete it by number. ufw status numbered lists the rules with numbers, and ufw delete 3 removes the third. ufw delete allow 51820/udp removes the WireGuard rule by its text.

When you add AmneziaWG or Outline, open their ports too. TunnelVPS read the ufw manual on 16 November 2026: a port rule without a protocol, such as ufw allow 53, opens both TCP and UDP. The AmneziaWG guide gives its port advice.

UFW questions

Which port does WireGuard use?

The one in ListenPort of wg0.conf. 51820 is common, but any free UDP port works if the client's Endpoint matches.

What if I locked myself out after ufw enable?

Use the console from the control panel card on the server page, when one shows, and run ufw disable. Otherwise write to support with the server ID.

Is open WireGuard port UFW enough for a working tunnel?

No. The port rule lets devices connect; the route rule, forwarding and NAT let their traffic out to the internet.

Does the WireGuard port need TCP?

No. WireGuard uses UDP only. Opening TCP on the same number adds nothing.

Do I need UFW if TunnelVPS leaves ports open?

It is optional. It limits what the internet can reach to the services you choose.

Verdict
  • SSH first, then 51820/udp, then the route rule: that order avoids a lockout.
  • Forwarding and NAT still apply with UFW on.
Sources (3)
  1. ufw(8) manual page — Ubuntu (read 2026-11-16)
  2. wg-quick(8) manual page — man7.org (read 2026-11-16)
  3. WireGuard: Quick Start — WireGuard (read 2026-11-16)

A server with open ports to start from

You choose the VPN port and the firewall. Home costs $6.37 a month on 12 months.

From $2.25/mo on 12 months · no KYC

Get a server