Security

Set up a UFW firewall on Ubuntu and Debian

Enable UFW without locking yourself out, allow SSH and web ports, restrict services to specific IPs, rate-limit SSH and understand how Docker bypasses UFW.

BeginnerLinux VPS3 min read

UFW (Uncomplicated Firewall) is the friendly front end to the Linux firewall on Ubuntu and Debian. A few commands give a VPS a deny-by-default firewall that only opens the ports you need. This UFW firewall setup takes about five minutes.

Before you start

  • Root or sudo access to Ubuntu or Debian (UFW is preinstalled on Ubuntu; on Debian run apt install ufw).
  • Know your SSH port (ss -ltnp | grep ssh).
  • Provider console access in case of a mistake.
  • On cPanel/WHM servers use CSF instead, and on AlmaLinux/Rocky use firewalld; do not run two firewall managers at once.

Set default policies

ufw default deny incoming
ufw default allow outgoing

Incoming connections are blocked unless a rule allows them; the server can still reach the internet for updates.

Allow SSH before enabling

Danger: enabling UFW without an SSH rule cuts off your session. Always add SSH first.

ufw allow OpenSSH

If SSH runs on a custom port: ufw allow 2222/tcp. Better still, allow SSH only from your own IP:

ufw allow from 198.51.100.25 to any port 22 proto tcp

Allow web traffic

ufw allow 80/tcp
ufw allow 443/tcp

Or use an application profile such as ufw allow 'Nginx Full'; list profiles with ufw app list. Add ufw allow 443/udp if you serve HTTP/3.

Enable and check

ufw enable
ufw status verbose

Open a new SSH session to confirm you can still connect before closing the current one.

More rule examples

GoalCommand
Allow MySQL only from an app serverufw allow from 10.0.0.5 to any port 3306 proto tcp
Allow a port rangeufw allow 30000:30100/tcp
Block an IPufw insert 1 deny from 203.0.113.50
Rate-limit SSH (6 tries/30s)ufw limit 22/tcp
Allow on one interfaceufw allow in on wg0

insert 1 puts the deny rule first; UFW processes rules in order and the first match wins.

Delete or change rules

ufw status numbered
ufw delete 3

Disable the firewall entirely with ufw disable, or start over with ufw reset.

Docker and UFW

Docker writes its own iptables rules, so ports published with -p 8080:80 are reachable from the internet even if UFW denies them. Safer options:

  • Publish ports on localhost only: 127.0.0.1:8080:80, and expose services through a reverse proxy.
  • Or add rules to the DOCKER-USER chain (through /etc/ufw/after.rules) to filter container traffic.

Logging

ufw logging low records blocked packets in /var/log/ufw.log (or the journal). Check it when a legitimate connection fails.

Troubleshooting

  • Locked out after enabling: log in through the provider console and run ufw allow OpenSSH or ufw disable.
  • Port still unreachable after allowing it: the service may listen only on 127.0.0.1, or a provider-level firewall blocks it.
  • IPv6 not filtered: make sure IPV6=yes in /etc/default/ufw, then reload.

UFW FAQ

Is UFW secure enough for a production server?

Yes. It manages the same kernel netfilter firewall as hand-written rules; UFW just makes it easier.

UFW or firewalld?

Use UFW on Ubuntu/Debian and firewalld on AlmaLinux/Rocky; each is the default for its family.

Do I need UFW if my provider has a firewall?

A host firewall adds a second layer and protects you even if the provider rules change. Using both is good practice.

Next steps

Was this guide helpful?

Still stuck?

Ask MdmHost AI for a quick answer, or open a ticket and our team will take a look. We are here 24/7.