UFW (Uncomplicated Firewall) is the friendliest front end to iptables/nftables on Ubuntu and Debian. It is approachable enough that a junior operator can get a working firewall in five commands, and capable enough that you do not need to drop down to raw rules for most production hosts. This article documents the baseline we apply on every internet-facing Linux host. For the more advanced cases (per-interface rules, mark-based routing, conntrack inspection), see ufw-firewall-deep-dive.
How to verify
Inventory the current state.
sudo ufw status verbose
sudo ufw status numbered
sudo iptables -L -n -v --line-numbers 2>/dev/null | head -30
sudo nft list ruleset 2>/dev/null | head -40
ss -tlnp
ufw status verbose shows the policy direction (deny / allow / reject) for inbound, outbound, and routed. ss -tlnp shows what is actually listening — there is no point in firewalling a port that nothing is bound to, but it is worth knowing.
What’s happening
UFW is a wrapper that generates iptables (legacy) or nftables (modern) rules. The user-facing model is simple: a default policy per direction, then a list of explicit allow/deny rules. The rules are evaluated top-down; first match wins.
The production baseline is “default deny incoming, default allow outgoing.” That means an inbound packet that does not match any allow rule is silently dropped. Outbound is allowed by default because most servers initiate connections (DNS, NTP, package updates, log forwarding) and listing them all explicitly is more burden than security gain. For high-security hosts the outbound side can be locked too, but that is a separate topic.
IPv6 is enabled by default in modern UFW (IPV6=yes in /etc/default/ufw). When you ufw allow 22, UFW writes both an IPv4 and an IPv6 rule. If your host has IPv6 connectivity (every cloud instance now does), the rules cover both.
UFW also supports limit — a rate-limit pattern that uses connection-tracking to throttle repeated incoming connections to a port from the same source. The default policy is six connections per 30 seconds; further connections are dropped. This is the simplest brute-force mitigation for SSH on its default port.
The procedure
-
Install UFW if it is not already present.
sudo apt install -y ufwOn Debian, it is not in the base image. On Ubuntu, it is installed but typically disabled.
-
Set default policies — deny in, allow out, deny routed. Do this before enabling — once enabled with a default-deny inbound and no rules, you would lock yourself out.
sudo ufw default deny incoming sudo ufw default allow outgoing sudo ufw default deny routed -
Allow your SSH access first. Always before
ufw enable.sudo ufw allow OpenSSHOpenSSHis a named application profile that resolves totcp/22. If you have moved SSH to a non-standard port (see ssh-hardening-port-ciphers), use the port number directly:sudo ufw allow 2202/tcp -
Optionally rate-limit SSH.
limitadds a connection rate cap from the same source IP. Useful on default-port SSH, less essential once you have Fail2ban.sudo ufw limit OpenSSH -
Enable the firewall. UFW will prompt for confirmation; you have already verified SSH is in the allow list.
sudo ufw enable sudo ufw status verboseThe status should show
Status: active, default policies as configured, and the SSH rule listed. -
Add application rules. For a typical web host:
sudo ufw allow 80/tcp comment 'HTTP' sudo ufw allow 443/tcp comment 'HTTPS'commentis optional but worth using — it shows up inufw status verboseand makes the rule list readable months later. -
Restrict by source for management ports. For a host that exposes Prometheus metrics or a database admin port, restrict to internal subnets:
sudo ufw allow from 10.0.0.0/8 to any port 9100 proto tcp comment 'node_exporter' sudo ufw allow from 10.0.5.0/24 to any port 5432 proto tcp comment 'postgres internal'The
fromclause is what makes UFW practical for production — without it, every allow is open to the world. -
Verify the rules are doing what you expect from outside. A port-scan from a different host is the truth-teller.
# From your workstation, not the firewalled host nmap -p 22,80,443,5432,9100 server.example.netOpen ports should appear
open, restricted portsfiltered. Thefilteredis UFW silently dropping the packet. -
For a host with multiple interfaces, scope the rule to one.
sudo ufw allow in on eth1 to any port 8080 proto tcp comment 'internal API'in on eth1restricts the rule to traffic arriving on that interface — useful for hosts with a public NIC and an internal management NIC. -
Persist and document. UFW persists automatically — rules in
/etc/ufw/user.rulesand/etc/ufw/user6.rulessurvive reboot. Reflect the rules in configuration management (Ansible, Pulumi, Terraform) so the firewall state is reproducible:- name: Configure UFW community.general.ufw: rule: allow port: "{{ item.port }}" proto: "{{ item.proto }}" from_ip: "{{ item.from | default('any') }}" comment: "{{ item.comment }}" loop: - { port: 22, proto: tcp, comment: SSH } - { port: 80, proto: tcp, comment: HTTP } - { port: 443, proto: tcp, comment: HTTPS } - { port: 9100, proto: tcp, from: 10.0.0.0/8, comment: node_exporter }
Common pitfalls
- Enabling UFW before allowing SSH — instant lockout, recover via cloud console only.
- Forgetting IPv6 rules — UFW handles it automatically only if
IPV6=yesin/etc/default/ufw, and only for rules added after that flag is set. - Adding a rule but not reloading:
ufw allowreloads automatically, but if you edit/etc/ufw/before.rulesmanually you mustsudo ufw reload. - Conflicting with Docker — Docker rewrites iptables on its own, bypassing UFW for published container ports. The fix is to publish containers to
127.0.0.1:portand reverse-proxy in front, or apply the documentedDOCKER-USERchain workaround. - Counting on
ufw statusto show every rule — if you have manually added entries tobefore.rules, those are in effect but invisible tostatus.
In our managed engagements, the firewall on every host is a default-deny baseline plus a small, named set of application rules — defined in code, applied uniformly, and verified by an external port-scan as part of the onboarding handshake. That is the operating posture provided by Managed Operations regardless of whether the host is on AWS, on bare metal, or in a colo rack.