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).
Diagram: Original SVG schematic by Ajay Kumar Chaturvedi.
Prerequisites and Safety Checks
Confirm your host state before turning on packet filtering:
- Shell access with
sudoprivileges and a provider rescue console available if you are configuring a remote VPS. - Exactly one firewall manager chosen for the host (
ufw, nativenftables, orfirewalldon Fedora/RHEL). Never enableufw.service,nftables.service, andiptables.serviceat 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 adropinput policy immediately drops new connections not explicitly allowed. Onufw, add your SSH rule before runningufw enable. Onnftables, back up/etc/nftables.conf, runnft -c -ffirst, 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:
ss -tulnp
systemctl is-active ufw nftables firewalld || trueNote 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):
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:
sudo ufw limit 22/tcp comment 'Rate-limited SSH'
sudo ufw allow 80,443/tcp comment 'HTTP and HTTPS'
sudo ufw enableIf 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:
sudo cp /etc/nftables.conf /etc/nftables.conf.bakWrite 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:
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;
}
}
EOFStep 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):
sudo nft -c -f /etc/nftables.confIf that returns with zero errors, apply the file atomically and enable the boot unit:
sudo nft -f /etc/nftables.conf
sudo systemctl enable --now nftables.serviceNote 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:
docker run -d -p 127.0.0.1:8080:80 nginx:stableIf 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:
# UFW hosts
sudo ufw status verbose
# Native nftables hosts
sudo nft list ruleset
# Test a new SSH session from another terminal
ssh user@server-ipExpected 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 } acceptis included beforepolicy drop, otherwise Neighbor Discovery breaks. - `ufw` rules vanish or conflict on reboot: Run
systemctl is-enabled iptables nftables firewalldand disable any competing firewall service so onlyufwinitializes Netfilter chains at boot. - Accidentally locked out on a local console or rescue shell: On
ufw, runsudo ufw disableto drop rules temporarily and fix the rule list. On nativenftables,sudo nft flush rulesetclears active kernel tables (while preserving/etc/nftables.confon disk so you can repair and re-test withnft -c -f).
