Configuring the Linux Firewall with nftables
Control inbound and outbound traffic on a Linux host using nftables and the friendly ufw front-end to secure your network services.
Configuring the Linux Firewall with nftables is a free Linux Networking & TCP/IP for Developers lesson on CoddyKit — lesson 4 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Linux Networking & TCP/IP for Developers learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Why a Host Firewall?
A host firewall decides which packets are allowed in or out of a single machine. It is your last line of defense, protecting services even when the network perimeter is breached.
From iptables to nftables
Modern Linux uses nftables as the kernel packet-filtering framework, replacing the older iptables. It uses tables, chains, and rules but with a cleaner, unified syntax.
Tables, Chains, and Rules
In nftables a table holds chains, a chain holds ordered rules, and each rule matches packets and takes an action like accept or drop. Chains hook into traffic at points such as input and output.
Listing the Ruleset
See the entire active configuration with a single command. This is always your first step before changing anything.
sudo nft list rulesetA Simple Input Policy
Create a table and an input chain with a default drop policy, then allow only what you need. Default-deny is the secure baseline.
sudo nft add table inet filter
sudo nft add chain inet filter input { type filter hook input priority 0 \; policy drop \; }Allowing SSH
Add a rule to accept incoming TCP on port 22 so you do not lock yourself out before allowing anything else.
sudo nft add rule inet filter input tcp dport 22 acceptAllowing Established Traffic
Permit replies to connections you initiated by accepting established and related traffic. Without this, outbound requests get no responses.
sudo nft add rule inet filter input ct state established,related acceptThe Easier Way: ufw
For everyday use, ufw (Uncomplicated Firewall) is a friendly front-end. It manages the underlying rules with simple commands.
sudo ufw allow 22/tcp
sudo ufw enableChecking ufw Status
Verify which rules ufw has applied and whether it is active.
sudo ufw status verbosePersisting Rules
nftables rules added at the command line vanish on reboot. Save them to /etc/nftables.conf and enable the service so they reload automatically.
sudo nft list ruleset | sudo tee /etc/nftables.conf
sudo systemctl enable nftablesLogging Dropped Packets
Add a logging rule before the final drop so you can see what is being blocked. This is invaluable when a service mysteriously cannot be reached.
sudo nft add rule inet filter input log prefix "dropped: "Quick Check
Test your firewall knowledge.
Recap
You learned to configure the Linux firewall.
- nftables uses tables, chains, and rules with a default-deny baseline.
- Always allow SSH and established traffic before locking down.
- ufw simplifies common rules; persist nftables to survive reboots.
Frequently asked questions
Is the “Configuring the Linux Firewall with nftables” lesson free?
Yes — the full text of “Configuring the Linux Firewall with nftables” is free to read here on the web, and the Linux Networking & TCP/IP for Developers course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Linux Networking & TCP/IP for Developers course, upgrade to CoddyKit PRO.
What will I learn in “Configuring the Linux Firewall with nftables”?
Control inbound and outbound traffic on a Linux host using nftables and the friendly ufw front-end to secure your network services. You practise Linux Networking & TCP/IP for Developers with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start Linux Networking & TCP/IP for Developers?
No prior experience is required. Linux Networking & TCP/IP for Developers on CoddyKit is structured for beginners through advanced learners; this is — lesson 4 of 4, so you can start here or from the beginning and move at your own pace.
How long does the “Configuring the Linux Firewall with nftables” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this Linux Networking & TCP/IP for Developers lesson?
Yes. Every Linux Networking & TCP/IP for Developers lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Managing Network Interfaces
- Routing Tables & Gateways
- DNS Configuration & Resolution
- Configuring the Linux Firewall with nftables