iptables: a beginner’s guide to the Linux firewall
Understand Netfilter tables, chains and rules, and try basic iptables commands for SSH, filtering and NAT.
iptables is a userspace utility that configures packet filtering and NAT in the Linux kernel’s Netfilter subsystem. It can control access to services, route or translate traffic, and help limit some unwanted connections. It does not by itself stop a large DDoS attack.
How iptables works
Rules are organized in tables and chains. The filter table handles ordinary filtering with INPUT, OUTPUT and FORWARD. The nat table handles address translation, including PREROUTING and POSTROUTING. The mangle table changes selected packet fields; raw runs before connection tracking for its supported hooks.
INPUT handles traffic destined for this host; OUTPUT handles locally generated traffic; FORWARD handles traffic passing through it. PREROUTING acts before routing and POSTROUTING after routing in tables that define these chains.
A rule matches packets and applies a target: ACCEPT allows, DROP silently discards, REJECT refuses with an error, LOG records, and SNAT or DNAT changes source or destination addresses in the nat table. Rule order matters.
Practical examples
List existing filter rules, with line numbers if needed:
sudo iptables -L -v
sudo iptables -L -v --line-numbers
Allow SSH on port 22. On a remote host, verify your access and rule order before changing firewall policy:
sudo iptables -A INPUT -p tcp --dport 22 -j ACCEPT
Drop packets from a specific source address:
sudo iptables -A INPUT -s 192.168.1.100 -j DROP
Masquerade outbound traffic through eth0; replace the interface name, and configure forwarding separately if this host is a router:
sudo iptables -t nat -A POSTROUTING -o eth0 -j MASQUERADE
On Debian or Ubuntu with iptables-persistent/netfilter-persistent installed, save IPv4 rules with privileged redirection. Ensure /etc/iptables exists:
sudo mkdir -p /etc/iptables
sudo sh -c 'iptables-save > /etc/iptables/rules.v4'
Conclusion
iptables offers detailed control of Linux traffic. Check the active firewall manager before editing rules: Ubuntu often uses UFW, while nftables provides a newer rule system. Avoid mixing independent managers for the same rules.
Share
What's Your Reaction?
Like
0
Dislike
0
Love
0
Funny
0
Angry
0
Sad
0
Wow
0