basforge

Search titles, text and the “If it doesn’t work” cases.

    RU

    The ufw firewall: everything closed except SSH and web

    Turn on ufw with a deny-incoming policy and three open ports: 22, 80 and 443. The order is strict: the SSH rule goes in before ufw enable. ufw doesn’t close ports that Docker publishes — close those by publishing on 127.0.0.1 or with the provider’s firewall.

    How ufw works

    ufw is a wrapper around the kernel firewall. It writes its rules to iptables (on Ubuntu 24.04 through the iptables-nft compatibility layer, so in practice into nftables). Rules added by command live in /etc/ufw/user.rules and survive reboots; the default policy is in /etc/default/ufw.

    incoming packet
      ├─ addressed to the server ─────→ input   → ufw rules → sshd, Traefik on :80 and :443
      └─ to a container's published
         port (after DNAT) ───────────→ forward → Docker's rules, ufw doesn't look here

    Hence the main limitation: ufw closes what the server itself listens on, but not the ports from ports: in compose. Details — Docker ports bypass the input chain.

    CrowdSec with crowdsec-firewall-bouncer doesn’t conflict with ufw: the bouncer keeps its own nftables tables with a higher priority and drops banned addresses before ufw’s and Docker’s rules (CrowdSec against SSH brute force).

    What to close ports with

    With What it closes When to use
    ufw the server’s own ports: SSH, Traefik on the host, services outside Docker always, as the base layer
    publishing on 127.0.0.1 a container port visible only to the server admin panels and databases in Docker (A host-only port goes on 127.0.0.1)
    the provider’s firewall (Cloud Firewall and the like) everything, Docker’s ports included: packets are dropped before they reach the server if the provider has one, turn it on as a second layer with the same 22, 80, 443
    nftables by hand anything, in any chain complex rules ufw doesn’t have: geo blocking, your own sets

    Setup

    1. See what the server listens on

      sudo ss -tulpn
      # Netid State  Local Address:Port  Process
      # udp   UNCONN 127.0.0.54:53       users:(("systemd-resolve",…))
      # tcp   LISTEN 0.0.0.0:22          users:(("sshd",…),("systemd",pid=1,…))
      # tcp   LISTEN 127.0.0.53%lo:53    users:(("systemd-resolve",…))

      127.0.0.x and [::1] aren’t visible from outside; leave them be. Anything on 0.0.0.0 or * is a candidate: either allow it or turn the service off. On a fresh server it’s usually just SSH.

    2. Set the default policy

      sudo ufw default deny incoming
      sudo ufw default allow outgoing

      While ufw is off, this changes nothing: the policy takes effect on enable.

    3. Allow SSH — before enabling

      sudo ufw allow OpenSSH
      # Rules updated
      # Rules updated (v6)

      OpenSSH is a ready-made profile from /etc/ufw/applications.d/, i.e. port 22/tcp. If SSH runs on another port (Port 22): sudo ufw allow 48731/tcp.

      ufw limit OpenSSH instead of allow drops an address after 6 connections in 30 seconds. With key-only login that’s unnecessary and gets in the way of scripts; CrowdSec stops the guessing (CrowdSec against SSH brute force).

    4. Allow web

      sudo ufw allow 80,443/tcp

      You need 80 even with HTTPS: Let’s Encrypt comes there for the HTTP-01 challenge, and Traefik redirects from it to 443. If Traefik has HTTP/3 on, also sudo ufw allow 443/udp.

    5. Enable it

      Keep the SSH session open and test a login from a second window (Keep the old session open).

      sudo ufw enable
      # Command may disrupt existing ssh connections. Proceed with operation (y|n)? y
      # Firewall is active and enabled on system startup
    6. Look at the result

      sudo ufw status verbose
      # Status: active
      # Logging: on (low)
      # Default: deny (incoming), allow (outgoing), deny (routed)
      # New profiles: skip
      #
      # To                         Action      From
      # --                         ------      ----
      # 22/tcp (OpenSSH)           ALLOW IN    Anywhere
      # 80,443/tcp                 ALLOW IN    Anywhere
      # 22/tcp (OpenSSH (v6))      ALLOW IN    Anywhere (v6)
      # 80,443/tcp (v6)            ALLOW IN    Anywhere (v6)

      deny (routed) is about forwarding packets between networks. It doesn’t affect Docker traffic: Docker writes its own forward rules, ahead of ufw.

    Everyday rules

    Command What it does
    sudo ufw status numbered rules with numbers — for deleting and ordering
    sudo ufw delete 3 delete a rule by number; the numbers after it shift
    sudo ufw delete allow 8080/tcp delete a rule by its text
    sudo ufw allow from 198.51.100.7 to any port 22 proto tcp SSH from one address only
    sudo ufw insert 1 deny from 198.51.100.66 a deny that fires before every allow
    sudo ufw allow in on eth1 from 10.0.0.0/24 everything from the provider’s private network
    sudo ufw reload reread rules after editing files in /etc/ufw/
    sudo tail -f /var/log/ufw.log what gets dropped, [UFW BLOCK] lines

    SSH from your home address only is strong protection, but most ISPs change home addresses. Without a fallback rule (a second address, a provider firewall you can edit from the panel, the console), a new address locks you out.

    If it doesn’t work

    After ufw enable, new SSH sessions hang and time out

    Cause
    There was no SSH rule: the deny incoming policy closed port 22 too.
    Fix
    In a still-open session: sudo ufw allow OpenSSH. No session — the provider console (Provider console) and the same command.

    ufw deny 8080, but the container’s port is open from outside

    Cause
    Docker publishes the port via DNAT, packets go to the forward chain, and ufw’s rules sit on input (Docker ports bypass the input chain).
    Fix
    Publish the port on localhost — 127.0.0.1:8080:8080 (A host-only port goes on 127.0.0.1) — or don’t publish it at all and go through Traefik. To block it from outside, use the provider’s firewall.

    Blocked an address, but it still gets in

    Cause
    ufw checks rules top to bottom until the first match: allow OpenSSH sits higher and fires before the new deny.
    Fix
    Put the deny first: sudo ufw insert 1 deny from 198.51.100.66. See the order with sudo ufw status numbered.

    The port is closed over IPv4 but open over IPv6

    Cause
    /etc/default/ufw has IPV6=no, so no IPv6 rules are created.
    Fix
    IPV6=yes (the default), then sudo ufw disable && sudo ufw enable; ufw status gains (v6) lines.

    With ufw limit OpenSSH, a deploy script or rsync in a loop can no longer connect

    Cause
    limit drops an address that opened 6 connections in 30 seconds.
    Fix
    Go back to ufw allow OpenSSH: with key-only login, guessing is useless anyway, and CrowdSec does the banning (CrowdSec against SSH brute force). Or use ControlMaster in ~/.ssh/config (~/.ssh/config).

    How to check

    Reading ufw status isn’t a test. Test from outside: from your computer or from a phone on mobile data.

    # SSH and web are open
    nc -vz 203.0.113.10 22
    # Connection to 203.0.113.10 22 port [tcp/ssh] succeeded!
    
    # any other port times out instead of refusing: the packet was dropped
    nc -vz -w 5 203.0.113.10 5432
    # nc: connect to 203.0.113.10 port 5432 (tcp) timed out: Operation now in progress

    If the server already runs Docker, check each port from docker ps --format '{{.Ports}}' separately. 0.0.0.0:8080->8080/tcp in that list is open to the whole internet, whatever ufw says.

    The full picture from outside — nmap from another machine:

    nmap -Pn --top-ports 1000 203.0.113.10
    # PORT    STATE SERVICE
    # 22/tcp  open  ssh
    # 80/tcp  open  http
    # 443/tcp open  https