Guides ยท Oct 5, 2026
UFW Firewall on an Ubuntu VPS: Rules, Rate Limiting and Docker Caveats
Set up the UFW firewall on Ubuntu: default policies, allowing SSH and web ports, rate limiting, IPv6, logging and the Docker bypass problem.
UFW ("Uncomplicated Firewall") is a friendly front end for the Linux packet filter, and it is the easiest way to control which ports on your VPS are reachable from the internet. It is included on Ubuntu. This guide covers the commands you will use every day, a few that most tutorials skip, and the Docker behaviour that surprises people.
What a host firewall does (and does not)
A firewall decides which incoming connections your server accepts. If a database accidentally listens on all interfaces, a firewall rule can keep the world away from it. It does not stop floods that fill your network link before they arrive; that is handled by network-level filtering on a DDoS protected VPS. Use both.
1. Check the current state
sudo ufw status verbose
On a fresh server it says inactive. Do not enable it yet.
2. Set the default policies
sudo ufw default deny incoming
sudo ufw default allow outgoing
This is the standard stance: block everything coming in unless you allow it, and let the server make outgoing connections freely for updates and API calls.
3. Allow SSH before enabling (very important)
sudo ufw allow OpenSSH
If you enable the firewall without this rule, your current SSH session may survive for a moment but you will not be able to reconnect. If you moved SSH to another port, allow that port instead: sudo ufw allow 2222/tcp.
4. Allow the ports your services need
sudo ufw allow 80/tcp # HTTP
sudo ufw allow 443/tcp # HTTPS
sudo ufw allow 25565/tcp # Minecraft Java
sudo ufw allow 51820/udp # WireGuard
sudo ufw allow 30000:30100/udp # a port range
You can also use application profiles. List them with sudo ufw app list, for example sudo ufw allow "Nginx Full". Specify tcp or udp where you know it, since many game servers use UDP only.
5. Enable it
sudo ufw enable
sudo ufw status numbered
The numbered list is useful for deleting rules later. UFW starts at boot automatically once enabled.
6. Restrict by source IP
Admin services should not be open to everyone. If you have a fixed IP, you can allow a port only from it:
sudo ufw allow from 203.0.113.10 to any port 22 proto tcp
sudo ufw allow from 203.0.113.0/24 to any port 3306 proto tcp
The addresses above are documentation examples; use your own. Remember to remove the general SSH rule if you want SSH restricted to your address only, and keep the web console from your dashboard as an emergency way in.
7. Rate-limit SSH
sudo ufw limit OpenSSH
This denies connections from an IP that opens six or more connections within 30 seconds. It is a cheap, effective brake on brute-force scripts. Pair it with the key-only setup in our SSH hardening guide.
8. Delete and reset rules
sudo ufw status numbered
sudo ufw delete 3
sudo ufw delete allow 80/tcp
When the numbers shift after a deletion, list them again before deleting another. To start over, sudo ufw reset removes all rules and disables the firewall, so allow SSH again before re-enabling.
9. IPv6
Make sure IPv6 rules are enabled too, otherwise services are reachable over IPv6 even though your IPv4 rules look strict. In /etc/default/ufw, check for IPV6=yes, then run sudo ufw reload. Test with sudo ss -tulpn to see what is really listening.
10. Logging
sudo ufw logging low
sudo tail -f /var/log/ufw.log
Blocked connection attempts appear in the log. You will see plenty of random scans, which is normal on any public address. If you troubleshoot a service that cannot be reached, look for [UFW BLOCK] lines with its port.
The Docker caveat
Docker manages its own firewall rules. When you publish a port (-p 8080:80), Docker inserts rules that are processed before UFW's, so the port can be open to the internet even though UFW does not allow it. Ways to deal with it:
- Bind published ports to localhost when only a reverse proxy needs them:
-p 127.0.0.1:8080:80, or"127.0.0.1:8080:80"in Compose. - Put a reverse proxy such as Nginx or Caddy in front, and only publish ports 80 and 443. Our Nginx reverse proxy guide shows how.
- Check what is exposed with
sudo ss -tulpnand an external port scan from another machine, not by trustingufw statusalone.
Our Docker installation guide and n8n guide use this localhost-binding pattern.
Temporary rules during OS upgrades
When you run do-release-upgrade over SSH, it opens a fallback SSH daemon on port 1022. Allow it for the duration and delete it afterwards. Details are in the 24.04 to 26.04 upgrade guide.
Handy recipe: a web server
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw limit OpenSSH
sudo ufw allow 80,443/tcp
sudo ufw enable
Check it with sudo ufw status verbose. Three open ports, one of them rate limited, is all a typical website needs.
Frequently asked questions
Is UFW enough to secure a server?
It is a strong first layer for controlling which ports are reachable, but combine it with key-only SSH, updates and sensible service configuration.
I enabled UFW and lost SSH access. What now?
Open the web console from your TidalVPS dashboard and run sudo ufw allow OpenSSH. Always add the SSH rule before enabling the firewall.
Why is my Docker container reachable even though UFW blocks the port?
Docker adds its own firewall rules that are evaluated before UFW. Bind the published port to 127.0.0.1 or use a reverse proxy.
Does UFW protect against DDoS attacks?
Only against small, targeted abuse. Large floods must be filtered upstream before they reach your server.
Related guides
- How to Secure SSH on a New VPS: Keys, Config and Fail2ban
- Ubuntu 26.04 VPS Setup and Hardening Checklist
- How DDoS Protection Works for Game Servers (and What It Cannot Do)
Need a server for this?
Every TidalVPS VPS comes with full root access, unmetered traffic, daily backups and DDoS protection.