How-To Guides5 min read

How to Configure a Linux Firewall Safely with ufw and nftables

Configure a host firewall on Ubuntu, Debian, and Arch Linux using ufw or native dual-stack nftables without dropping SSH access or missing Docker's NAT bypass.

Pick one firewall frontend per host. On Ubuntu or simple Debian machines, allow or rate-limit SSH first with sudo ufw limit ssh, test rules with --dry-run, and then run sudo ufw enable. On Debian 12/13 servers or Arch Linux using native nftables, build an inet dual-stack ruleset in /etc/nftables.conf, validate syntax with sudo nft -c -f /etc/nftables.conf, and load it atomically with sudo nft -f /etc/nftables.conf.

Key takeaways

  • Always establish SSH rules and test with --dry-run or nft -c -f before enabling a default-drop firewall.
  • Never assume ufw blocks containers published with Docker's default 0.0.0.0 port binding.
  • Keep only one firewall service enabled at boot on any Linux host.

Quick facts

  • Modern Debian, Ubuntu, Fedora, and Arch kernels run nf_tables underneath; ufw uses the iptables-nft translation layer.
  • Running ufw alongside nftables.service, legacy iptables, or firewalld creates conflicting packet filters.
  • Docker published ports (-p 8080:80) traverse the NAT and FORWARD paths and bypass ufw INPUT chain rules.
  • Native nftables inet tables filter both IPv4 and IPv6 traffic in one ruleset, provided ICMPv6 neighbor discovery remains permitted.
  • nft -c -f parses and validates /etc/nftables.conf syntax without loading rules into the kernel.

Modern Linux distributions all filter packets through the kernel nf_tables subsystem, yet people still lose remote SSH access or expose internal ports because they treat ufw, nftables, and legacy iptables snippets as interchangeable. On Ubuntu, ufw translates rules down to nf_tables via iptables-nft. On Debian 12/13 servers and Arch Linux, writing native nftables rules directly in /etc/nftables.conf gives you atomic reloads and unified IPv4/IPv6 chains. On Fedora and RHEL hosts, firewalld owns that nftables namespace.

Here is the safe workflow I use when locking down a host without locking myself out, covering both ufw (Ubuntu/Debian) and native nftables (Debian/Arch).

Linux Netfilter Packet Path showing ufw, nftables, and Docker NAT Bypass
Linux Netfilter Packet Path showing ufw, nftables, and Docker NAT Bypass

Diagram: Original SVG schematic by Ajay Kumar Chaturvedi.

Prerequisites and Safety Checks

Confirm your host state before turning on packet filtering:

  • Shell access with sudo privileges and a provider rescue console available if you are configuring a remote VPS.
  • Exactly one firewall manager chosen for the host (ufw, native nftables, or firewalld on Fedora/RHEL). Never enable ufw.service, nftables.service, and iptables.service at the same time.
  • Know which TCP/UDP ports actually need external access by inspecting listening sockets first (ss -tulnp).
Backup and Lockout Warning: Enabling a firewall with a drop input policy immediately drops new connections not explicitly allowed. On ufw, add your SSH rule before running ufw enable. On nftables, back up /etc/nftables.conf, run nft -c -f first, and verify SSH connects in a second terminal before ending your active session.

Step-by-Step Procedure

Step 1: Audit Listening Ports and Active Firewall Services

Target OS: Ubuntu, Debian, Arch Linux (plus Fedora/RHEL check).

Check which services are listening on external addresses (0.0.0.0 or [::]) and which firewall unit is currently active:

BASH
ss -tulnp
systemctl is-active ufw nftables firewalld || true

Note the exact port SSH uses (default 22, or your custom port if changed). If a database like PostgreSQL (5432) or Redis (6379) is listening on 0.0.0.0 instead of 127.0.0.1, a working default-deny host firewall will block external scans against it, though binding local-only services to 127.0.0.1 remains good defense in depth.

Step 2: Configure UFW Safely (Ubuntu / Debian Path)

Target OS: Ubuntu 24.04/26.04 LTS, Debian.

If you want Ubuntu's default frontend (ufw), ensure nftables.service is disabled so they do not overwrite each other, then inspect default policies and preview your SSH rule with --dry-run (from the `ufw(8)` manual and Ubuntu's Firewalls guide):

BASH
sudo ufw default deny incoming
sudo ufw default allow outgoing
sudo ufw --dry-run limit 22/tcp comment 'Rate-limited SSH'

Apply SSH rate-limiting (limit drops IPs opening 6 or more connections within 30 seconds, as documented in ArchWiki Uncomplicated Firewall), add any web ports you intentionally serve, and then enable ufw:

BASH
sudo ufw limit 22/tcp comment 'Rate-limited SSH'
sudo ufw allow 80,443/tcp comment 'HTTP and HTTPS'
sudo ufw enable

If you only need a firewall on an Ubuntu desktop or standard VM without custom routing or nft sets, I would stick with ufw rather than hand-writing nftables.conf. Check the numbered rule table afterward with sudo ufw status numbered.

Step 3: Author an Atomic Dual-Stack nftables Config (Debian Server / Arch Path)

Target OS: Debian 12/13, Arch Linux (when using native nftables without ufw or firewalld).

As the Debian nftables wiki explains, native nftables supports the inet address family to filter IPv4 and IPv6 together in one chain, and nft -f applies files atomically rather than flushing rules into an open window.

Back up your existing /etc/nftables.conf first if present:

BASH
sudo cp /etc/nftables.conf /etc/nftables.conf.bak

Write a clean server ruleset modeled on the nftables wiki simple server ruleset that permits established traffic, loopback, essential IPv6 neighbor discovery (forgetting those icmpv6 types silently breaks IPv6 connectivity!), SSH, and optional HTTP/HTTPS:

BASH
sudo tee /etc/nftables.conf >/dev/null <<'EOF'
#!/usr/sbin/nft -f

flush ruleset

table inet firewall {
    chain inbound {
        type filter hook input priority 0; policy drop;

        ct state vmap { established : accept, related : accept, invalid : drop }
        iifname "lo" accept

        icmp type echo-request limit rate 5/second accept
        icmpv6 type { nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } accept
        icmpv6 type echo-request limit rate 5/second accept

        tcp dport { 22, 80, 443 } accept
    }

    chain forward {
        type filter hook forward priority 0; policy drop;
    }
}
EOF

Step 4: Dry-Run and Load the nftables Ruleset Atomically

Target OS: Debian 12/13, Arch Linux (for native nftables).

Always validate syntax without touching live kernel filtering using -c (--check, verified in the nftables quick reference):

BASH
sudo nft -c -f /etc/nftables.conf

If that returns with zero errors, apply the file atomically and enable the boot unit:

BASH
sudo nft -f /etc/nftables.conf
sudo systemctl enable --now nftables.service

Note for Fedora and RHEL users: Fedora enables firewalld by default, which manages its own firewalld table inside nf_tables. Do not run nft flush ruleset or enable nftables.service alongside firewalld; instead use sudo firewall-cmd --add-service=http --permanent && sudo firewall-cmd --reload as documented in Fedora's firewalld quick docs.

Step 5: Account for Docker Port Publishing Bypass

Target OS: Ubuntu, Debian, Arch, Fedora hosts running Docker Engine.

According to Docker's packet filtering documentation, Docker inserts nat and FORWARD rules that divert traffic bound for published container ports (-p 8080:80) before packets ever reach ufw's INPUT chain. Even with ufw default deny incoming, -p 8080:80 exposes port 8080 to the public internet!

Whenever a container service should stay behind a local reverse proxy (like Nginx or Caddy) or stay private to the machine, bind the published port explicitly to loopback:

BASH
docker run -d -p 127.0.0.1:8080:80 nginx:stable

If you run Fedora/RHEL with firewalld, Docker automatically creates a docker zone and docker-forwarding policy. On Ubuntu servers running Docker alongside ufw, keep internal services bound to 127.0.0.1 or filter in the DOCKER-USER chain rather than trusting ufw deny.

Verification and Expected Result

Verify live rules and test connectivity from a second terminal before closing your primary SSH session:

BASH
# UFW hosts
sudo ufw status verbose

# Native nftables hosts
sudo nft list ruleset

# Test a new SSH session from another terminal
ssh user@server-ip

Expected result: ufw status verbose or nft list ruleset shows a default deny/drop incoming policy with SSH and intended ports open, new SSH connections succeed, and unexpected ports time out or drop cleanly from an external probe.

Troubleshooting

  • IPv6 stops working after loading a custom `nftables.conf`: Verify icmpv6 type { nd-neighbor-solicit, nd-router-advert, nd-neighbor-advert } accept is included before policy drop, otherwise Neighbor Discovery breaks.
  • `ufw` rules vanish or conflict on reboot: Run systemctl is-enabled iptables nftables firewalld and disable any competing firewall service so only ufw initializes Netfilter chains at boot.
  • Accidentally locked out on a local console or rescue shell: On ufw, run sudo ufw disable to drop rules temporarily and fix the rule list. On native nftables, sudo nft flush ruleset clears active kernel tables (while preserving /etc/nftables.conf on disk so you can repair and re-test with nft -c -f).

Sources