Skip to content
Menu

How do you set up a UFW firewall on a VPS?

Checked by the BuyMoneroVPS deskLast updated on 5 min read

Straight answer

To set up UFW on a VPS, install it, set the default policy to deny incoming and allow outgoing, allow SSH on port 22 before anything else, add one rule per service you run, then run ufw enable and ufw status verbose. On a BuyMoneroVPS Debian or Ubuntu server it takes about 5 minutes and covers IPv4 and IPv6.

In brief
  • Ubuntu's documentation states that ufw is disabled until you enable it.
  • Allow SSH before ufw enable, or the open session may be your last one.
  • With IPv6 on, which is the default, a rule such as ufw allow 22/tcp covers both IPv4 and IPv6.
  • ufw limit blocks an address that opens 6 or more connections within 30 seconds.

Why put a firewall on a VPS that runs few services?

Because the services you forget about are the ones that get found. A database installed for a test, a monitoring agent or an admin panel can listen on a public port without telling you. Every BuyMoneroVPS server has a dedicated IPv4 and a /64 of IPv6, reachable from the whole internet from the moment the IP address appears in the dashboard.

UFW, the uncomplicated firewall, is a front end to the kernel's netfilter rules. You describe what may come in, it writes the rules for both IPv4 and IPv6. The policy in this guide is short: refuse all incoming connections, allow all outgoing ones, then open exactly the ports your services need. It is the sixth task of securing a new Linux VPS.

What are the steps, in a safe order?

  1. Install ufw

    Install it with apt if the ufw command is missing. Nothing is filtered until you enable it.

  2. Set the default policies

    Deny incoming and allow outgoing. Changing a default does not touch rules that already exist.

  3. Allow SSH first

    Run ufw allow 22/tcp, or ufw limit 22/tcp to slow down password guessing as well. If you moved SSH to another port, allow that port instead.

  4. Open the ports of your services

    Add one rule per service: 80 and 443 for a website, 51820/udp for WireGuard, 18080 for a Monero node.

  5. Enable and read the result

    Run ufw enable, confirm, then ufw status verbose to see the defaults and every rule.

  6. Test from outside

    Open a new SSH session before closing the current one. Then check each service from your own computer.

A complete first setup (as root or with sudo)
apt install ufw

ufw default deny incoming
ufw default allow outgoing

ufw limit 22/tcp          # SSH, with rate limiting
ufw allow 80/tcp          # website, HTTP
ufw allow 443/tcp         # website, HTTPS

ufw enable
ufw status verbose

Which rule opens which service?

Common UFW rules on a VPS, with the port each service uses by default, 30 November 2026
ServicePortRuleOpen to
SSH22/tcpufw limit 22/tcpEveryone, rate-limited
Website (nginx)80/tcp, 443/tcpufw allow 80/tcp and ufw allow 443/tcpEveryone
WireGuard51820/udpufw allow 51820/udpEveryone (keys do the filtering)
Monero node, peers18080/tcpufw allow 18080/tcpEveryone
Monero node, restricted RPC18089/tcpufw allow proto tcp from <your IP> to any port 18089Your own address
Tor relayORPort, often 443/tcpufw allow 443/tcpEveryone

How do you open a port to one address only?

Name the source in the rule: ufw allow proto tcp from 198.51.100.7 to any port 18089. Ubuntu's firewall page shows this form for SSH from a single host. It suits services that only you use, such as a node's wallet port, an admin page or a database you reach from home.

If your home address changes often, a source rule will lock you out of that service after each change. Two alternatives work better: an SSH tunnel, which needs no extra port at all, or a WireGuard tunnel with rules that accept the port only from the tunnel subnet, for example from 10.8.0.0/24. For the node ports above, see how to run a Monero node on a VPS.

Read, delete and log
ufw status numbered       # rules with their index
ufw delete 3              # remove rule number 3
ufw delete allow 80/tcp   # or repeat the rule with delete in front
ufw logging on
ufw app list              # application profiles installed packages provide

How do you change or remove a rule safely?

List the rules with numbers, delete by number or by repeating the rule, and check the list again. The ufw manual adds a detail: a rule created for both IPv4 and IPv6 appears twice in the numbered list, and deleting by number removes only one of the two entries. Writing delete in front of the original rule removes both at once, which is usually what you want.

Never delete the SSH rule while you are connected through it, unless another way in is open. When you reorganise rules, add the new one first, test it, then delete the old one. Logging, turned on with ufw logging on, records blocked packets in the system log, useful when a service you expected to reach stays silent.

According to BuyMoneroVPS's survey of 30 November 2026, Hub 4 (2 vCPU / 4 GB / 65 GB) costs $8.18, 57% under the $18.98 mean of comparable plans. Once the firewall is up, hosting a website on the VPS needs only ports 80 and 443.

Frequently asked questions

Will ufw enable cut my SSH session?

Not if port 22, or your SSH port, is allowed first: with that rule in place the open session continues. Still keep a second session open while you test.

Does ufw also protect IPv6?

Yes. IPv6 is on by default in /etc/default/ufw, and a rule like ufw allow 22/tcp applies to both families. Every BuyMoneroVPS server has a /64 of IPv6, so keep it that way.

What does ufw limit do exactly?

It allows the connection but denies an address that opens 6 or more connections within 30 seconds. It is meant for SSH and similar login services.

Do I still need ufw if the network has upstream protection?

Yes. Upstream protection deals with floods of traffic; it does not know which of your ports should be open. Only a firewall on the server decides that.

WireGuard stopped routing after I enabled ufw. Why?

ufw's default forward policy drops routed packets. Set DEFAULT_FORWARD_POLICY="ACCEPT" in /etc/default/ufw and reload, as Ubuntu's firewall page shows for masquerading.

Verdict
  • Deny incoming, allow outgoing, allow SSH, then enable: four lines cover most servers.
  • Open each port for a named service, and to a single address when only you use it.
  • Delete rules by repeating them with delete, so the IPv4 and IPv6 entries go together.
Sources (2)
  1. Firewall — Ubuntu Server documentation (read 2026-11-30)
  2. ufw(8) manual page — Ubuntu Manpages (read 2026-11-30)

A clean server to firewall

Hub 4: 2 vCPU · 4 GB memory · 65 GB NVMe disk · 2 TB monthly transfer in London, Frankfurt, Amsterdam, New York, Dallas or Los Angeles, paid in Monero.

Deploy a Monero VPS from $3.78/mo