# How do I open the WireGuard port in UFW?

> Open WireGuard port UFW rules in 5 steps: allow SSH first, then 51820/udp, a route rule for wg0, forwarding in ufw's sysctl file, then ufw enable.

- URL: https://tunnelvps.com/guides/how-to-open-the-wireguard-port-in-ufw/
- Last updated: 2026-11-16
- Publisher: TunnelVPS (https://tunnelvps.com)

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

**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*

| Step | Where | What to do |
| --- | --- | --- |
| 1 | Server | Install UFW with `apt install ufw` if it is missing. |
| 2 | Server | Allow SSH: `ufw allow 22/tcp`. |
| 3 | Server | Allow WireGuard: `ufw allow 51820/udp`. |
| 4 | Server | Allow routing: `ufw route allow in on wg0 out on eth0`, and set forwarding in `/etc/ufw/sysctl.conf`. |
| 5 | Server | Run `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](https://tunnelvps.com/guides/how-to-secure-ssh-on-a-vpn-server/ "How do I secure SSH on a VPS?") covers keys and safe testing.

*Allow SSH and WireGuard, then enable UFW*

```bash
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*

```bash
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](https://tunnelvps.com/guides/how-to-enable-ip-forwarding-for-wireguard/ "How do I enable IP forwarding for WireGuard?") explains the sysctl keys and the NAT lines. The complete `wg0.conf` sits in the [one-command install guide](https://tunnelvps.com/help/install-wireguard-in-one-command/ "How do I install WireGuard on a TunnelVPS server?").

## 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](https://tunnelvps.com/guides/how-to-install-amneziawg-on-a-vps/ "How do I install AmneziaWG on a VPS?") 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

1. [ufw(8) manual page](https://manpages.ubuntu.com/manpages/noble/en/man8/ufw.8.html) — Ubuntu (read 2026-11-16)
2. [wg-quick(8) manual page](https://man7.org/linux/man-pages/man8/wg-quick.8.html) — man7.org (read 2026-11-16)
3. [WireGuard: Quick Start](https://www.wireguard.com/quickstart/) — 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. [Order Home for 12 months](https://tunnelvps.com/app/checkout/?plan=home&term=12m&image=ubuntu-24.04&from=guides-how-to-open-the-wireguard-port-in-ufw&lang=en) · [IP forwarding guide](https://tunnelvps.com/guides/how-to-enable-ip-forwarding-for-wireguard/ "How do I enable IP forwarding for WireGuard?")

---

TunnelVPS: TunnelVPS rents Linux KVM servers for a personal VPN (WireGuard, Outline or AmneziaWG) in New York, Dallas, Los Angeles, London, Amsterdam and Frankfurt, paid in crypto with no ID document. 5 plans from 2 to 16 GB of RAM and 2 to 6 TB of traffic a month cost the same in all six cities; on the 12-month term each is priced at least 25% under a named comparable plan of a no-KYC crypto host, read on 16 November 2026. The operator is DomaineGoat LLC, Panama.
