In short
To set up UFW on Ubuntu, install it with sudo apt install ufw, deny incoming traffic and allow outgoing traffic by default, allow SSH with ufw allow OpenSSH, then turn it on with ufw enable. Open ports 80 and 443 for the web and restrict admin ports to your own IP. If you use Docker, published ports bypass UFW, so you need to fix that separately.
In this guide
Setting up UFW takes three commands: deny incoming traffic by default, allow SSH and enable the firewall. After that, you open only the services you really need (such as 80 and 443 for the web) and restrict admin ports to your own IP address. This guide covers setting up rules in the right order, deleting rules, restricting by IP and a problem many people notice too late: ports published with Docker bypass UFW rules.
What Is UFW?
UFW (Uncomplicated Firewall) is a tool that lets you manage the Linux kernel's packet filtering framework (iptables/nftables) with simple commands. It is Ubuntu's default firewall tool and can also be used on Debian-based distributions. Instead of writing raw iptables rules, you work with readable commands like ufw allow 443/tcp.
UFW does not replace network-level DDoS protection; they serve different purposes. DDoS protection filters high-volume attack traffic before it reaches the server, while UFW decides which ports on your server are open and to whom.
1. Installation and Default Policies
On Ubuntu, UFW usually comes installed but inactive. Check its status and install it if it is missing:
sudo apt update
sudo apt install ufw -y
sudo ufw statusA safe starting policy: deny every incoming connection and allow outgoing connections from the server.
sudo ufw default deny incoming
sudo ufw default allow outgoing2. Allow SSH, Then Enable the Firewall
The order is critical: if you enable UFW without allowing SSH, your connection will drop and you may not be able to get back into the server.
sudo ufw allow OpenSSH # or: sudo ufw allow 22/tcp
sudo ufw enable
sudo ufw status verboseIf you changed the SSH port, open the new port instead of the OpenSSH profile, for example sudo ufw allow 2222/tcp. After enabling, test connecting from a second terminal without closing your current session.
3. Allowing Services
UFW recognizes application profiles defined by installed packages. To see the available profiles:
sudo ufw app list
sudo ufw app info "Nginx Full"Common rules:
# Web server (HTTP + HTTPS)
sudo ufw allow "Nginx Full" # or: sudo ufw allow 80,443/tcp
# Open a custom port with a comment
sudo ufw allow 5678/tcp comment 'n8n gecici'
# Port range (a protocol is required for ranges)
sudo ufw allow 30000:30100/udpOpen only the ports that truly need to be reachable from outside. Services such as the database, Redis and the app's internal port should not be exposed if a reverse proxy sits in front of them.
4. Restrict by IP: Keep Admin Ports to Yourself
If you have a static IP address or an office network, opening SSH and admin panels only to that address shrinks your attack surface dramatically:
# SSH from a single IP
sudo ufw allow from 203.0.113.25 to any port 22 proto tcp
# Database access from a network block
sudo ufw allow from 10.10.0.0/24 to any port 3306 proto tcp
# Remove the public SSH rule open to everyone
sudo ufw delete allow OpenSSHAdd the IP-specific rule first, test it, then delete the general rule. If your IP address changes, as with most home internet connections, key-based login plus Fail2ban is more practical than locking SSH to an IP.
Slowing Down Brute-Force Attempts
The limit rule denies new connections from an IP address that has attempted 6 or more connections in the last 30 seconds:
sudo ufw limit 22/tcpFor more detailed blocking, see the Fail2ban setup guide.
5. Listing, Deleting and Ordering Rules
UFW evaluates rules from top to bottom, and the first matching rule wins. So if you want to block a specific IP, you need to insert that rule above your general allow rules:
sudo ufw status numbered # numbered list
sudo ufw delete 3 # delete rule number 3
sudo ufw insert 1 deny from 198.51.100.7 # add a block rule at the very top
sudo ufw reloadTo wipe everything and start over, use sudo ufw reset; this command also disables UFW, so remember to add the SSH rule again afterward.
6. IPv6 and Logging
If your server has an IPv6 address, make sure the rules apply to IPv6 too. The /etc/default/ufw file should contain this line:
IPV6=yesTurn on logging to see blocked connections; the low level is enough for most servers:
sudo ufw logging low
sudo tail -f /var/log/ufw.log7. Docker Bypassing UFW
This is the most common security hole on Docker servers that use UFW. When you publish a port with docker run -p 8080:80 or with ports: - "8080:80" in a Compose file, Docker adds its own NAT and forwarding rules to iptables. Because this traffic passes through the FORWARD chain rather than the server's INPUT chain, your UFW rules do not apply. The result: the port is open to the internet even though ufw status does not list port 8080.
Fix 1: Publish the Port to Localhost Only (Recommended)
If a reverse proxy such as Nginx, Caddy or Traefik sits in front of the container, do not expose the container port at all:
services:
app:
image: ornek/uygulama
ports:
- "127.0.0.1:8080:80"The reverse proxy connects to 127.0.0.1:8080; only ports 80 and 443, which you opened in UFW, are visible from outside. For services used only by other containers, such as a database, remove the ports line entirely and access them over the Docker network.
Fix 2: Hook the DOCKER-USER Chain into UFW
If you want to manage container ports with UFW, you can hook the DOCKER-USER chain, which Docker reserves for user rules, into UFW. Add this block to the end of the /etc/ufw/after.rules file:
# BEGIN UFW AND DOCKER
*filter
:ufw-user-forward - [0:0]
:ufw-docker-logging-deny - [0:0]
:DOCKER-USER - [0:0]
-A DOCKER-USER -j ufw-user-forward
-A DOCKER-USER -j RETURN -s 10.0.0.0/8
-A DOCKER-USER -j RETURN -s 172.16.0.0/12
-A DOCKER-USER -j RETURN -s 192.168.0.0/16
-A DOCKER-USER -p udp -m udp --sport 53 --dport 1024:65535 -j RETURN
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 192.168.0.0/16
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 10.0.0.0/8
-A DOCKER-USER -j ufw-docker-logging-deny -p tcp -m tcp --tcp-flags FIN,SYN,RST,ACK SYN -d 172.16.0.0/12
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 192.168.0.0/16
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 10.0.0.0/8
-A DOCKER-USER -j ufw-docker-logging-deny -p udp -m udp --dport 0:32767 -d 172.16.0.0/12
-A DOCKER-USER -j RETURN
-A ufw-docker-logging-deny -m limit --limit 3/min --limit-burst 10 -j LOG --log-prefix "[UFW DOCKER BLOCK] "
-A ufw-docker-logging-deny -j DROP
COMMIT
# END UFW AND DOCKERsudo systemctl restart ufw
# Allow outside access to a container (use the container's INTERNAL port)
sudo ufw route allow proto tcp from any to any port 80With this method, note that ufw route rules use the container's internal port, not the host port. Turning off Docker's iptables management entirely with "iptables": false is not recommended, because it can break container networking.
If you are installing Docker for the first time, apply these settings together with the How to Install Docker on Ubuntu and Run Your First Container guide.
Quick Reference Table
| Goal | Command |
|---|---|
| Status and defaults | sudo ufw status verbose |
| Allow SSH | sudo ufw allow OpenSSH |
| Web ports | sudo ufw allow 80,443/tcp |
| Open a port to a single IP | sudo ufw allow from IP to any port 22 proto tcp |
| Rate-limit brute force | sudo ufw limit 22/tcp |
| Delete a rule | sudo ufw status numbered, sudo ufw delete N |
| Docker container access | sudo ufw route allow proto tcp from any to any port N |
| Turn off temporarily | sudo ufw disable |
Next Step
A firewall is only one part of server hardening; for the full sequence, including updates, SSH keys and automatic patches, follow the First 10 Security Steps for a New Ubuntu Server checklist.
If you are looking for a server where you write your own rules from scratch, Sunucu Atlas's Intel Premium VDS plans in our Istanbul data center come with KVM virtualization, NVMe storage, layered DDoS protection and full root access; if you will run Docker and several services, SA-XEON-4, with 4 vCPUs and 8 GB of RAM, is a good starting point.
If you want to run a personal AI assistant, OpenClaw Server plans come with OpenClaw preinstalled, Node.js 24 and the UFW firewall; you can tighten the rules to fit your own use with the commands in this guide.
Frequently asked questions
Will enabling UFW drop my SSH connection?
Not if you allowed SSH with sudo ufw allow OpenSSH before enabling it. If you run ufw enable without that rule, new connections are blocked and you can lose access to the server.
How do I delete a rule in UFW?
List the rules with numbers using sudo ufw status numbered, then run sudo ufw delete NUMBER. Alternatively, delete a rule using the same form you added it with, such as sudo ufw delete allow 8080/tcp.
Why are my Docker ports open even with UFW enabled?
Docker adds its own iptables rules for published ports, and that traffic passes through the FORWARD chain instead of the INPUT chain UFW filters. Publishing the port on 127.0.0.1 or hooking the DOCKER-USER chain into UFW fixes the problem.
How do I block a specific IP address with UFW?
Use sudo ufw insert 1 deny from IP_ADDRESS. insert 1 places the rule at the top of the list; since UFW applies the first matching rule, the block takes effect before any general allow rules.
Should I use UFW or iptables?
UFW already uses the iptables/nftables framework; it just offers a more readable interface. UFW is enough for most servers; use iptables or nftables directly when you need custom NAT or complex routing rules.