NewCode CLOUD — 20% off your first month on all cloud VPSClaim

How to Set Up a UFW Firewall on Ubuntu and Debian

UFW (“Uncomplicated Firewall”) is a friendly front end for the Linux kernel firewall. It ships with Ubuntu and installs on Debian with one command. This guide sets up a sensible rule set for a web server and explains the traps that lock people out.

Before you start: keep your current SSH session open until you have confirmed a new login works. If you do lock yourself out, use the VNC console in the server control panel to get back in and run ufw disable.

1. Install UFW

sudo apt update
sudo apt install -y ufw
sudo ufw status verbose

On a new install the status is inactive.

2. Set default policies

Block everything coming in, allow everything going out:

sudo ufw default deny incoming
sudo ufw default allow outgoing

3. Allow SSH first

This is the step that matters. Allow SSH before enabling the firewall:

sudo ufw allow OpenSSH

If you moved SSH to a different port, allow that port instead, for example sudo ufw allow 2222/tcp.

Even better, rate-limit SSH. UFW will block an IP that opens six or more connections within 30 seconds:

sudo ufw limit OpenSSH

4. Allow your services

For a website:

sudo ufw allow 80/tcp
sudo ufw allow 443/tcp

Some other common ports:

Service Rule
HTTP / HTTPS ufw allow 80,443/tcp
HTTP/3 (QUIC) ufw allow 443/udp
MySQL (only if remote access is needed) ufw allow from 203.0.113.10 to any port 3306 proto tcp
WireGuard ufw allow 51820/udp
Minecraft ufw allow 25565/tcp
aaPanel / 1Panel admin the port the installer printed, ideally limited to your IP

5. Restrict admin ports to your IP

Database ports, panel logins and monitoring dashboards should never be open to the world. Allow them only from your office or home IP:

sudo ufw allow from 203.0.113.10 to any port 8888 proto tcp

Replace 203.0.113.10 with your real IP, and 8888 with the port you are protecting.

6. Enable the firewall

sudo ufw enable
sudo ufw status numbered

Now open a new SSH session to confirm you can still log in.

7. Delete or change rules

sudo ufw status numbered     # list rules with numbers
sudo ufw delete 3            # delete rule number 3
sudo ufw delete allow 80/tcp # or delete by specification

To start over: sudo ufw reset.

8. Logging

sudo ufw logging low
sudo tail -f /var/log/ufw.log

low logs blocked packets without filling your disk. Use medium temporarily when debugging.

IPv6

UFW handles IPv4 and IPv6 together as long as IPV6=yes is set in /etc/default/ufw (the default). Each rule you add creates both a v4 and a v6 entry.

The Docker caveat

Docker writes its own iptables rules and bypasses UFW for published container ports. A container started with -p 8080:80 is reachable from the internet even if UFW has no rule for 8080. Two safe options:

  • Publish ports only on localhost, -p 127.0.0.1:8080:80, and put Nginx in front.
  • Or add rules to the DOCKER-USER chain, which Docker leaves for you to manage.

See our Docker VPS guide for a full Docker setup.

UFW vs the network firewall

UFW filters traffic after it reaches your server. It does nothing against volumetric DDoS attacks that saturate the network link. For that you need upstream protection, such as Server.HK’s DDoS-protected IP add-ons.

Quick reference

sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
sudo ufw status verbose

Next: install Nginx with a free Let’s Encrypt certificate.