Lesson 2 of 10
iptables & nftables Deep Dive
iptables is the standard Linux firewall tool built into the kernel. Understanding its chain-based architecture allows precise control over network traffic. nftables is its modern successor with cleaner syntax.
iptables Architecture
# iptables processes packets through tables and chains
Tables: filter (firewall rules), nat (address translation), mangle (packet modification)
Chains: INPUT (incoming), OUTPUT (outgoing), FORWARD (routed traffic)
# Default policies - set BEFORE adding rules
iptables -P INPUT DROP # Default: block all incoming
iptables -P FORWARD DROP # Default: block all forwarded
iptables -P OUTPUT ACCEPT # Default: allow all outgoing
# Allow established/related connections (stateful - crucial!)
iptables -A INPUT -m state --state ESTABLISHED,RELATED -j ACCEPT
# Allow loopback (localhost communications)
iptables -A INPUT -i lo -j ACCEPT
# Allow specific services
iptables -A INPUT -p tcp --dport 22 -s 203.0.113.0/24 -j ACCEPT # SSH from office only
iptables -A INPUT -p tcp --dport 80 -j ACCEPT # HTTP from anywhere
iptables -A INPUT -p tcp --dport 443 -j ACCEPT # HTTPS from anywhere
# Rate limiting - prevent SSH brute force
iptables -A INPUT -p tcp --dport 22 -m limit --limit 3/min --limit-burst 5 -j ACCEPT
iptables -A INPUT -p tcp --dport 22 -j DROP # Drop after rate limit exceeded
# Log dropped packets (for monitoring)
iptables -A INPUT -j LOG --log-prefix "DROPPED: " --log-level 4
iptables -A INPUT -j DROP
# Save rules (persistent across reboots)
iptables-save > /etc/iptables/rules.v4
# Restore on boot: iptables-restore < /etc/iptables/rules.v4nftables - The Modern Alternative
# nftables replaces iptables, ip6tables, arptables, ebtables
# Cleaner syntax, better performance, atomic rule updates
# Install and start
apt install nftables
systemctl enable nftables
# /etc/nftables.conf - Modern firewall configuration
table inet filter {
chain input {
type filter hook input priority 0; policy drop; # Default deny
# Allow established/related connections
ct state established,related accept
# Allow loopback
iifname "lo" accept
# Allow ICMP (ping) - rate limited
icmp type echo-request limit rate 5/second accept
# SSH - restricted to specific source
tcp dport 22 ip saddr 203.0.113.0/24 accept
# Web traffic
tcp dport { 80, 443 } accept
# Log and drop everything else
log prefix "nft-drop: " drop
}
chain output {
type filter hook output priority 0; policy accept; # Allow outbound
}
chain forward {
type filter hook forward priority 0; policy drop; # No routing
}
}
# Apply: nft -f /etc/nftables.conf
# Verify: nft list rulesetCommon Mistakes
- Setting default policy to DROP before adding the ESTABLISHED,RELATED rule - you will immediately lose your own connection (test in a VM first)
- Not adding rate limiting on SSH - without rate limiting, brute force is unrestricted. Add fail2ban as a complementary control
- Not saving rules - iptables rules are lost on reboot unless saved to /etc/iptables/rules.v4; always persist your rules after testing
Tip
Tip
Practice iptables nftables Deep Dive in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
The foundational security model: Confidentiality (privacy), Integrity (trust), and Availability (uptime).
Practice Task
Note
Practice Task - (1) Write a working example of iptables nftables Deep Dive from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with iptables nftables Deep Dive is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready cybersecurity code.
Key Takeaways
- iptables is the standard Linux firewall tool built into the kernel.
- Setting default policy to DROP before adding the ESTABLISHED,RELATED rule - you will immediately lose your own connection (test in a VM first)
- Not adding rate limiting on SSH - without rate limiting, brute force is unrestricted. Add fail2ban as a complementary control
- Not saving rules - iptables rules are lost on reboot unless saved to /etc/iptables/rules.v4; always persist your rules after testing
Finished this lesson?