iptables is the traditional user-space tool for defining how Netfilter processes packets. Because raw iptables rules can be verbose and error-prone, Ubuntu ships with ufw (Uncomplicated Firewall) — a higher-level frontend that translates simple English-style rules into the correct iptables commands. For most servers and desktops, ufw is the right place to start. For advanced scenarios like NAT, custom chains, or stateful packet inspection, you will want to work directly with iptables.
Managing Firewalls with ufw
Install and Check Status
Enable and Disable
Common Rules
Application Profiles
ufw ships with named application profiles that define standard port sets for common services. Use them to avoid hard-coding port numbers.
Default Policies
Set default policies to define what happens to traffic that does not match any explicit rule. The safest baseline for a server is to deny all incoming and allow all outgoing traffic.Advanced Filtering with iptables
iptables operates on chains — INPUT (packets destined for the local system), OUTPUT (packets leaving the system), and FORWARD (packets routed through the system). Rules are evaluated top to bottom; the first match wins.
Inspecting the Current Ruleset
Building a Basic Ruleset
Allow established connections
Permit traffic for sessions that are already established or related to an existing connection. This prevents the firewall from dropping return traffic.
nftables is the modern successor to
iptables and is the default packet filtering framework in newer kernels (Linux 3.13+). Many distributions are migrating to nftables as the primary tool. iptables commands on those systems are translated through a compatibility layer (iptables-nft). For new production setups on recent Ubuntu releases, consider learning nft syntax as a forward-looking investment.