ufw on a VPS: rules by IP, SSH rate limit, logs, and why Docker bypasses ufw

ufw rules by IP, an SSH rate limit, logs, and why Docker publishes ports straight past ufw. Two tested fixes, Ubuntu 24.04.

Security

A firewall is only useful if the ports it claims to close are actually closed. This guide builds on the basic ufw setup and covers the next layer: allowing a port for a single address, rate-limiting SSH, reading the log, and finding out why a Docker container is reachable from the whole internet while ufw has no rule for it. Everything below was run on 5 November 2026 on a WeaselCloud VPS: Ubuntu 24.04.4, ufw 0.36.2, Docker 29.1.3 (the docker.io package).

Enable ufw without locking yourself out

Until you run ufw enable the firewall is off (Status: inactive), so you can build the rule set at leisure. Set the defaults, allow SSH, and only then turn it on.

ufw default deny incoming
ufw default allow outgoing
ufw allow 22/tcp
ufw enable
  • default deny incoming: any inbound connection you have not explicitly allowed is dropped.
  • allow 22/tcp: SSH over tcp. If your sshd listens on another port, use that number.
  • Each allow prints Rules updated and then Rules updated (v6). ufw writes every rule for both IPv4 and IPv6 because /etc/default/ufw has IPV6=yes.

If you enabled ufw first and your session died, there is a way back. Open the service page in the client area, press "Console", log in as root and run ufw disable. Your rules stay saved. See the client area overview for the console.

Allow a port for one IP only

A database or admin panel should not be visible to the whole internet. A "from address to port" rule lets in the address or network you name and nobody else.

ufw allow from 203.0.113.10 to any port 5432 proto tcp
ufw allow from 10.0.0.0/24 to any port 3306 proto tcp

The first line lets one host reach PostgreSQL (5432). The second lets the whole 10.0.0.0/24 network, addresses 10.0.0.1 to 10.0.0.254, reach MySQL (3306). to any means "any address of this server".

Two caveats. ufw evaluates rules top to bottom and the first match wins, so a broad "allow 5432 from anywhere" placed above makes the narrow rule pointless. And if your own IP is dynamic, an IP-based rule will eventually lock you out; in that case keep the port closed and reach the database through an SSH tunnel.

What ufw limit does

Bots hammer port 22 around the clock, guessing passwords. limit is allow with a rate check: it lets connections in but temporarily blocks an address that connects too often.

ufw limit 22/tcp

ufw status shows the action as LIMIT for both IPv4 and IPv6. According to the ufw documentation the trigger is six or more connection attempts from one address within 30 seconds. Check ufw status numbered: there should be a single 22/tcp entry marked LIMIT. If an old ALLOW for the same port is still there, delete it (next section), or it will match first.

Keep expectations realistic. limit slows brute force down; it does not replace key-based login with passwords disabled. It is free, though, and only bothers clients opening many sessions a second.

Delete a rule without leaving an IPv6 hole

List the rules with numbers. Every rule appears twice: once for IPv4, once marked (v6).

ufw status numbered

Here is the trap. Deleting by number removes exactly one line. We ran ufw --force delete 2, which removed the IPv4 rule for 80/tcp, and 80/tcp (v6) stayed: port 80 remained open over IPv6. (--force only skips the confirmation prompt.) Numbers shift after each deletion, so re-read the list before the next delete.

The safer way is to delete by the same description you used to create the rule:

ufw delete allow 80/tcp

It answers Rule deleted and Rule deleted (v6): both lines gone. We confirmed it after a reboot of the test server, going from two lines to zero. Use numbers only for long address-based rules you do not want to retype, and then remove the v6 twin too.

ufw logging: reading a UFW BLOCK line

The log answers "who is knocking, and on which port". Level low records blocked packets.

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

The first command replies Logging: on (low). The second follows the file live; leave with Ctrl+C. You will not wait long: on a public IPv4 address we saw scanner blocks within 20 seconds. A line looks like this (documentation addresses, parts elided):

[UFW BLOCK] IN=ens3 OUT= ... SRC=203.0.113.77 DST=203.0.113.10 ... PROTO=TCP SPT=... DPT=...

  • UFW BLOCK: the packet was dropped by ufw.
  • IN=ens3: the interface it arrived on. Find yours with ip -br a.
  • SRC is the sender, DST is your server.
  • PROTO is the protocol, SPT the sender's source port (meaningless), DPT the destination port, i.e. what the scanner is after.

This is ordinary internet background noise and proves the firewall works. Worry only about repeated DPT values matching a real service of yours from unfamiliar addresses. To stop logging: ufw logging off.

Why Docker bypasses ufw

This is the nasty surprise of running ufw with Docker. You publish a container with -p 8080:80, ufw has no rule for 8080, and the port is open anyway.

The reason is how the Linux packet filter is laid out. A packet reaching the server travels through iptables chains: INPUT if it is addressed to the server itself, FORWARD if it is transit traffic heading elsewhere. ufw's inbound rules sit in INPUT. When you publish a port, Docker adds a DNAT rule (rewrite of the destination address) in the nat table:

-A DOCKER ! -i docker0 -p tcp --dport 8080 -j DNAT --to-destination <container address>:80

After the rewrite the packet is addressed to the container, not the server, so the kernel sends it down the FORWARD path. There the order is:

-A FORWARD -j DOCKER-USER
-A FORWARD -j DOCKER-FORWARD
... then ufw-before-forward

Docker's chains come before ufw's and accept traffic to the container, so ufw never gets a say. We also grepped /etc/ufw/after.rules: no mention of Docker. This is why ufw deny 8080 will not help either, since the rule sits in a chain the packet never visits.

Test it from another machine

Testing from the server itself proves nothing, because local traffic takes a different path. Use a second machine. On the server, with ufw allowing only ports 22 and 80, start a test container:

docker run -d -p 8080:80 nginx:alpine

From the other machine, ask the port for an HTTP status (-s hides progress, -o /dev/null discards the body, -w prints just the code):

curl -s -o /dev/null -w '%{http_code}\n' http://203.0.113.10:8080

We got 200: nginx answered although ufw had no rule for 8080. A closed port prints 000, meaning curl could not connect. Repeat this test after each fix below.

Fix A: publish on 127.0.0.1 only

This is the default choice. The -p flag lets you pick which server address to listen on. 127.0.0.1 is the server itself and is unreachable from outside.

docker rm -f <container-name>
docker run -d -p 127.0.0.1:8080:80 nginx:alpine
ss -tlnp | grep 8080

A running container cannot be re-published, so you recreate it: the first command removes the old one. ss should show LISTEN on 127.0.0.1:8080, not 0.0.0.0:8080. On the server curl http://127.0.0.1:8080 returns 200; from the outside machine you get 000. In Compose, the equivalent is "127.0.0.1:8080:80" under ports (we have not tested this, but the format is described in the Docker documentation).

If the site must be public, put a reverse proxy (nginx, Caddy) on the host. It listens on 80 and 443, which ufw controls, and talks to the container on 127.0.0.1:8080. Only the proxy faces the internet.

Fix B: a rule in DOCKER-USER

Use this when the port must be reachable from outside, but only by chosen addresses. Docker reserves the DOCKER-USER chain for your rules: it is first in FORWARD and Docker does not overwrite it. The catch: by the time a packet gets there, the port has already been rewritten to 80 (the container's port), so --dport 8080 never matches. You need the port the client originally asked for, which the kernel's connection tracking table (conntrack) remembers.

iptables -I DOCKER-USER -i ens3 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j DROP
iptables -I DOCKER-USER -i ens3 -s 203.0.113.10 -p tcp -m conntrack --ctorigdstport 8080 --ctdir ORIGINAL -j ACCEPT
  • -I DOCKER-USER inserts at the top of the chain, so the second rule ends up above the first: allow your address, then drop everyone.
  • -i ens3 matches only packets arriving from the external network. Check your interface name with ip -br a.
  • --ctorigdstport 8080 is the original destination port, before the rewrite.
  • --ctdir ORIGINAL restricts the match to the request direction, not replies.

After the first command the port returned 000 from outside; after the second, 200 again from the allowed address. Inspect the chain with iptables -S DOCKER-USER.

Now the limitation we ran into. These rules do not survive a reboot. After reboot the chain held only its empty declaration -N DOCKER-USER, the container (started with --restart unless-stopped) came back by itself, and port 8080 answered 200 to the whole internet. A silent hole. We have not tested saving these rules via iptables-persistent or a systemd unit, so we give no ready-made recipe. Pick B only if you are prepared to set that up and re-test after a reboot.

Which fix to choose

ApproachReachable from outside bySurvives rebootUse when
Do nothingEveryonen/aNever, unless the port is meant to be public
A: -p 127.0.0.1:8080:80Nobody (only the server and a proxy on it)Yes: it is container configuration, not an iptables ruleMost cases: databases, admin tools, sites behind a proxy
B: DOCKER-USER + conntrackOnly the listed addressesNo, persistence neededPort must be outside-facing, but not for everyone

Final checklist

  • ufw status verbose: active, incoming deny, outgoing allow.
  • One 22/tcp entry marked LIMIT, in both v4 and v6.
  • No leftover (v6) lines from deleted rules.
  • docker ps: no 0.0.0.0: or [::]: in the ports column for anything that should be private.
  • ss -tlnp: internal services listen on 127.0.0.1.
  • curl from an outside machine returns 000 for each of those ports.
  • The last two checks repeated after a server reboot.

FAQ

Why does Docker open a port that ufw never allowed?

When you publish a port, Docker rewrites the destination address (DNAT) and the packet goes to the container through the FORWARD chain. Docker's chains sit ahead of ufw's there, and ufw's inbound rules apply in INPUT, which this packet never reaches.

How do I keep a Docker port off the internet?

Publish it as -p 127.0.0.1:port:port so only the server itself can reach it. If chosen addresses need access, add a rule to the DOCKER-USER chain matching --ctorigdstport, and arrange for it to be restored after a reboot.

How do I delete a ufw rule together with its IPv6 line?

Run ufw delete allow 80/tcp, repeating the description you created the rule with. ufw removes both the IPv4 and the IPv6 line. Deleting by number removes one line only.

Where are the ufw logs?

In /var/log/ufw.log once you run ufw logging low. Blocked packets are tagged [UFW BLOCK], and the targeted port is in the DPT field.

Takeaways

  • Allow 22 first, enable after. Fallback: "Console" in the client area plus ufw disable.
  • One address or subnet per port: ufw allow from <IP> to any port <port> proto tcp.
  • ufw limit 22/tcp slows password guessing; it does not replace SSH keys.
  • Delete with ufw delete allow ... so the v6 line goes too.
  • Docker bypasses ufw: container traffic goes through FORWARD, where Docker's chains come first.
  • Default fix: publish on 127.0.0.1. DOCKER-USER rules vanish after a reboot.

Next

Didn't find an answer?

The support team will help.